Git 提供了两种补丁方案,一是用git diff生成的UNIX标准补丁.diff文件,二是git format-patch生成的Git专用.patch 文件。 .diff文件只是记录文件改变的内容,不带有commit记录信息,多个commit能够合并成一个diff文件。 .patch文件带有记录文件改变的内容,也带有commit记录信息,每一个commit对应一个patch文件。html
git format-patch 【commit sha1 id】-n
复制代码
n指从sha1 id对应的commit开始算起n个提交。 eg:git
git format-patch 2a2fb4539925bfa4a141fe492d9828d030f7c8a8 -2
复制代码
git format-patch 【commit sha1 id】 -1
复制代码
eg:bash
git format-patch 2a2fb4539925bfa4a141fe492d9828d030f7c8a8 -1
复制代码
git format-patch 【commit sha1 id】..【commit sha1 id】
复制代码
eg:app
git format-patch 2a2fb4539925bfa4a141fe492d9828d030f7c8a8..89aebfcc73bdac8054be1a242598610d8ed5f3c8
复制代码
git diff 【commit sha1 id】 【commit sha1 id】 > 【diff文件名】
复制代码
eg:spa
git diff 2a2fb4539925bfa4a141fe492d9828d030f7c8a8 89aebfcc73bdac8054be1a242598610d8ed5f3c8 > patch.diff
复制代码
选中要目标commit ,右击,选择create patch .net
git 中的每一个commit都有对应的一个sha1 id,咱们能够经过在终端输入git log
,而后找到对应的commit sha1 id: 命令行
2a2fb4539925bfa4a141fe492d9828d030f7c8a8
即是sha1 id
若是用Sourcetree的话也很方便,右击对应的commit,选择copy SHA-1 toclipboard便复制sha1 id到剪切板中:code
git apply --check 【path/to/xxx.patch】
git apply --check 【path/to/xxx.diff】
复制代码
git apply 【path/to/xxx.patch】
git apply 【path/to/xxx.diff】
复制代码
或者orm
git am 【path/to/xxx.patch】
复制代码
选择SourceTree,在屏幕顶部选择Aciotn-Apply patch cdn
在打补丁过程当中有时候会出现冲突的状况,有冲突时会打入失败,如图:
此时须要解决冲突: 一、首先使用 如下命令行,自动合入 patch 中不冲突的代码改动,同时保留冲突的部分:
git apply --reject xxxx.patch
复制代码
能够在终端中显示出冲突的大体代码:
git add.
添加改动到暂存区. 三、接着执行
git am --resolved
或者
git am --continue
git am --skip
跳过这次冲突,也能够执行git am --abort
回退打入patch的动做,还原到操做前的状态。关于冲突解决详情能够参考git am冲突解决
https://blog.csdn.net/liuhaomatou/article/details/54410361 https://blog.csdn.net/maybe_windleave/article/details/8703778 https://www.cnblogs.com/y041039/articles/2411600.html