patch合入
git am
git am會直接將patch的所有信息打上去,而且不用重新git add和git commit,author也是patch的author而不是打patch的人
常用命令
git am 0001-limit-log-function.patch # 將名字為0001-limit-log-function.patch的patch打上
git am --signoff 0001-limit-log-function.patch # 添加-s或者--signoff,還可以把自己的名字添加為signed off by信息,作用是注明打patch的人是誰,因為有時打patch的人并不是patch的作者
git am ~/patch-set/*.patch # 將路徑~/patch-set/*.patch 按照先后順序打上
git am --abort # 當git am失敗時,用以將已經在am過程中打上的patch廢棄掉(比如有三個patch,打到第三個patch時有沖突,那么這條命令會把打上的前兩個patch丟棄掉,返回沒有打patch的狀態(tài))
git am --resolved # 當git am失敗,解決完沖突后,這條命令會接著打patch
如有提示“patch does not apply”,表示patch沖突,手動解決完沖突后,繼續(xù)合入
git am --continue
或者忽略
git am --skip
或者停止合入
git am --abort
git apply
git apply是將補丁文件應用到代碼庫中,但不會自動創(chuàng)建提交記錄。而且使用git apply可以快速地測試一個補丁,檢查它是否會導致任何問題或沖突,但是需要手動創(chuàng)建提交記錄來記錄這個補丁被應用了。但是git apply并不會將commit message等打上去,打完patch后需要重新git add和git commit。
常用命令
git apply --stat 0001-limit-log-function.patch # 查看patch的情況
git apply --check 0001-limit-log-function.patch # 檢查patch是否能夠打上,如果沒有任何輸出,則說明無沖突,可以打上
打入patch
git apply xxx.patch
如果git與需要打patch的文件不在一個目錄(git 在framework下,patch要打入到frameworks/base/下)
git apply --check --directory=base/ xxx.patch
git apply --directory=base/ xxx.patch
如果有沖突,可以先導出沖突
git apply --reject xxxx.patch
此時在代碼工程路徑下生成.rej結尾的沖突文件,手動修改完沖突點后
git add 修改文件
然后解決沖突后合入
git am --resolved
patch生成
git format-patch
常用命令
git format-patch HEAD^ #生成最近的1次commit的patch
git format-patch HEAD^^ #生成最近的2次commit的patch
git format-patch HEAD^^^ #生成最近的3次commit的patch
git format-patch HEAD^^^^ #生成最近的4次commit的patch
git format-patch <r1>..<r2> #生成兩個commit間的修改的patch(包含兩個commit. <r1>和<r2>都是具體的commit號)
git format-patch -1 <r1> #生成單個commit的patch
git format-patch <r1> #生成某commit以來的修改patch(不包含該commit)
git format-patch --root <r1> #生成從根到r1提交的所有patch
git diff
將所有修改文件打包成patch
git diff > test.patch
只生成一個文件的patch
git diff test.c > test.patch
使用實例
當需要將一筆commit的修改打入到新的代碼倉庫時:
1.生成那筆提交的patch包
git format-patch -1 commit_id
2.將生成的patch包放入代碼倉庫中,然后檢測能否正常合入
git apply --check xxxx.patch
如果有這樣的類似報錯,說明有沖突
3.使用am命令合入patch,有沖突的文件則會生成.rej文件
git am XXX.patch --reject
4.手動處理完沖突后,將待提交完成add到暫存區(qū),然后am continue就可以將這一筆提交的全部信息(包括原提交的作者信息和描述)都合入到新代碼倉庫中了
git add xxxx/xxxx
git am --continue
參考文章:文章來源:http://www.zghlxwxcb.cn/news/detail-761731.html
https://zhuanlan.zhihu.com/p/104055075文章來源地址http://www.zghlxwxcb.cn/news/detail-761731.html
到了這里,關于【git】代碼patch包生成和合入的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!