git am 部分發生衝突的處理

1.背景
通常而言,爲了保留git log的記錄,咱們在作patch的時候會使用git format patch的命令來生成一個patch,在應用patch的時候會選擇git am來打上patch.通常的patch會包含N個文件的補丁,假設 
有其中一個文件發生了conflict,那麼am的過程就會中止,這時候須要咱們手動去解決衝突,而後才能繼續.git

2.用到的命令
git format-patch -N   //製做一個補丁,N表示生成幾個patch,默認是一筆commit一個patch
git am (--continue | --skip | --abort) PATCH_NAME  //打補丁
git apply --reject  PATCH_NAME  //強制應用補丁
git add FILE_NAME //添加到緩衝區

3.過程
1.>git format-patch -1 
生成一個patch:0001-modify-contents.patch,又或許這個patch是別人給你的session

2.>git am 0001-modify-contents.patch
    出現下面的log,說明am的過程發生了錯誤
    Applying: modify contents
    error: file1.c: does not match index
    error: patch failed: file2.c:0
    error: file2.c: patch does not apply
    error: patch failed: file3.c:0
    error: file3.c: patch does not apply
    Patch failed at 0001 modify contents
    The copy of the patch that failed is found in: .git/rebase-apply/patch
    When you have resolved this problem, run "git am --continue".
    If you prefer to skip this patch, run "git am --skip" instead.
    To restore the original branch and stop patching, run "git am --abort".app


3.>git status 能夠看到下面的log.說明am過程暫時中止了,可是還處在am的對話中.
       You are in the middle of an am session.
      (fix conflicts and then run "git am --continue")
      (use "git am --skip" to skip this patch)
      (use "git am --abort" to restore the original branch)this


4.>git apply --reject 0001-modify-contents.patch
    出現下面的log,說明file1,file2,file3發生了衝突,沒法自動合併:
    Checking patch file1.c...
    error: while searching for:rest

    error: patch failed: file1.c:0
    Checking patch file2.c...
    error: while searching for:orm

    error: patch failed: file2.c:0
    Checking patch file3.c...
    error: while searching for:ip

    error: patch failed: file3.c:0
    Applying patch file1.c with 1 reject...
    Rejected hunk #1.
    Applying patch file2.c with 1 reject...
    Rejected hunk #1.
    Applying patch file3.c with 1 reject...
    這時候在當前的目錄下針對patch fail的文件會生成fileX.rej.這些文件的內容就是發生衝突的地方, 手動修正就好.
 it


5.將修正好的fileX.c add到緩衝區裏去.
  >git add . 
  假設本文件夾下還有一些是untracked的文件的話,仍是建議用git add 特定的文件比較好

6.>git am --resolved 
    而後就會顯示:
    Applying: XXXXX.
    XXXXX就是那筆patch自己的log信息
    而後git log 就能看到那筆patch打上去了
--------------------- 
 io

相關文章
相關標籤/搜索