将 github 上的指定 commit 转换为 patch 文件

文章目录

    为啥会有这个需求

    因为目前正在用的一个开源项目已经停止为何 N 多年了,很多新功能都不支持。
    然后有不少三方开发者提交了 patch,但是这些 patch 并没有合并到项目中。
    所以,我 fork 了一份主项目,然后需要将这些 patch 一一打补丁进去。

    生成 patch 文件

    将 github commit 转换成 patch

    Add .patch (or .diff) at the end of the commit URL:

    例如,假如一个 commit 的 github 地址是:

    https://github.com/genieacs/genieacs-sim/pull/18/commits/5953cc04a610317d925bcc2537bed2cef0d15a92

    那么其对应的 patch url 就是:

    https://github.com/genieacs/genieacs-sim/commit/5953cc04a610317d925bcc2537bed2cef0d15a92.patch

    将这个 patch 文件下载到本地,重命名为 patch 后缀的文件:

    mv 5953cc04a610317d925bcc2537bed2cef0d15a92.patch.txt bootstrap.patch
    

    同时,取一个方便识别的文件名,如这个 patch 是为了给 tr069 模拟器增加 bootstrap 事件支持,那么就起名为 bootstrap.patch.

    应用 patch

    git apply bootstrap.patch
    

    确认本地修改

    > git status
    On branch bootstrap
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   data_model_202BC1-BM632w-8KA8WA1151100043.csv
            modified:   genieacs-sim
            modified:   methods.js
            modified:   simulator.js
    

    代码合并策略

    除了这个 patch,实际上我还有其他几个没有合入的 request commit 需要自己手动合并进来。

    我担心这几个 commit 是存在冲突的,所以需要一个保险的策略。

    • 备份 patch 文件到一个独立的目录
    • 每个 patch 打到一个独立的 branch
    • 这几个 branch 测试之后再合并入 master 分支

    参考

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式