相关技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > 相关技巧 > Git Commit生成与合入Patch

Git Commit生成与合入Patch指南分享

作者:龙之叶

文章介绍了生成和应用补丁的两种方法:diff命令对比目录生成补丁(需路径一致或使用软链接),及Git format-patch生成特定提交的补丁,同时说明了使用patch命令合入补丁的步骤,包括忽略目录层级和撤销操作

1. 生成 Patch 文件

1.1 使用 Diff 命令生成 Patch

通过 diff 命令,可以为两个目录生成差异补丁,这对于文件夹内文件的修改非常有用,可以生成一个包含所有修改操作的补丁文件。

前提条件:

示例:

假设有两个目录:

可以通过以下命令创建软链接并生成补丁:

mkdir patch
ln -s /media/data2/quectel/r12/cn_kj_r12a07/ql-ol-kernel/ patch/new 
ln -s /tmp/sdk/cn_kj_r12a07/ql-ol-kernel/ patch/old

若只修改了部分目录(如 arch/arm/boot/dts/qcom,drivers,sound),则只需对比这些路径:

cd patch 
diff -Naur old/arch/arm/boot/dts/qcom new/arch/arm/boot/dts/qcom > gpio-export.diff
diff -Naur old/drivers new/drivers >> gpio-export.diff
diff -Naur old/sound new/sound >> gpio-export.diff

参数说明:

注意:

1.2 使用 Git Format-Patch 生成 Patch

在 Git 中,可以使用 git format-patch 命令生成特定 commit 的补丁文件。

步骤:

  1. 确保已提交想要生成补丁的 commit。
  2. 使用 git format-patch 命令生成补丁文件。

示例:

git format-patch -1 HEAD
git format-patch -1 <commit-hash>
git format-patch <commit-hash1>..<commit-hash2>

常用命令:

$ git format-patch HEAD^ # 生成最近的1次commit的patch
$ git format-patch HEAD^^ # 生成最近的2次commit的patch
$ git format-patch HEAD^^^ # 生成最近的3次commit的patch
$ git format-patch <r1>..<r2> # 生成两个commit间的修改的patch
$ git format-patch -1 <r1> # 生成单个commit的patch
$ git format-patch --root <r1> # 生成从根到r1提交的所有patch

2. 合入 Patch 文件

2.1 使用 Patch 命令合入

将生成的 .diff 或 .patch 文件复制到目标目录,然后使用 patch 命令合入。

示例:

cp gpio-export.diff /tmp/sdk/cn_kj_r12a07/ql-ol-kernel/
patch -p1 < gpio-export.diff

参数说明:

撤销补丁:

patch -p1 -RE < /media/data2/quectel/r12/patch/gpio-export.diff

参数说明:

通过以上步骤,您可以轻松地生成并合入 Git commit 的 patch 文件,从而方便地进行代码的审查和合并。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文