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 //強制打補丁