Git 打补丁----基于源码改动生成 patch 包的方法

转载自:https://www.cnblogs.com/developer-qin/p/12357411.html
Git 提供了两种补丁方案,
一是用git diff生成的UNIX标准补丁.diff文件,
二是git format-patch生成的Git专用.patch 文件。

.diff文件只是记录文件改变的内容,不带有commit记录信息,多个commit可以合并成一个diff文件。
.patch文件带有记录文件改变的内容,也带有commit记录信息,每个commit对应一个patch文件。

git diff 生成补丁方法:

git diff 本次提交前一次commit的序列号 本次提交commit的序列号 > xxx.patch   #使用diff方法也可以将多次修改打出一个patch包来

git apply XXX.path

git diff 【commit sha1 id】 【commit sha1 id】 > 【diff文件名】

git format-patch

git format-patch 【commit sha1 id】 -1 # 某个提交的patch:

git format-patch 【commit sha1 id】-n # 某次提交(含)之前的几次提交:

$ git format-patch HEAD^        //生成最近的1次commit的patch; git format-patch -1 同作用
$ git format-patch HEAD^^       //生成最近的2次commit的patch ;有几个^就会打几个patch,从最近一次打起
$ git format-patch -2 <r1>           //生成两个commit的patch;

$ git format-patch <r1>..<r2>       //生成两个commit间的修改的patch(包含两个commit. <r1>和<r2>都是具体的commit号)

$ git format-patch <r1>             //生成某commit以来的修改patch(不包含该commit)
$ git format-patch --root <r1>   //生成从根到r1提交的所有patch

git am

git am
$ git apply --stat xxxx.patch     //查看patch的情况
$ git apply --check xxxx.patch    //检查patch是否能够打上,如果没有任何输出,则说明无冲突,可以打上
(注:git apply并不会将commit message等打上去,打完patch后需要重新git add和git commit,
而git am会直接将patch的所有信息打上去,而且不用重新git add和git commit,author也是patch的author而不是打patch的人)
$ git am xxxx.patch               //将名字为xxxx.patch的patch打上
$ git am --signoff xxxx.patch     //添加-s或者--signoff,还可以把自己的名字添加为signed off by信息,作用是注明打patch的人是谁,因为有时打patch的人并不是patch的作者
$ git am ~/patch-set/*.patch     // 将路径~/patch-set/*.patch 按照先后顺序打上
$ git am --abort               //当git am失败,解决完冲突后,这条命令会接着打patch
$ git apply --check xxx.patch   //測补丁有无问题
$ git apply --reject xxx.patch   //强制打补丁
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值