git am 部分发生冲突的处理

1.背景
一般而言,为了保留git log的记录,我们在做patch的时候会使用git format patch的命令来生成一个patch,在应用patch的时候会选择git am来打上patch.一般的patch会包含N个文件的补丁,假设 
有其中一个文件发生了conflict,那么am的过程就会停止,这时候需要我们手动去解决冲突,然后才能继续.

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是别人给你的

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".
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)
4.>git apply --reject 0001-modify-contents.patch
    出现下面的log,说明file1,file2,file3发生了冲突,无法自动合并:
    Checking patch file1.c...
    error: while searching for:

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

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

    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.这些文件的内容就是发生冲突的地方, 手动修正就好.
5.将修正好的fileX.c add到缓冲区里去.
  >git add . 
  假设本文件夹下还有一些是untracked的文件的话,还是建议用git add 特定的文件比较好
6.>git am --resolved 
    然后就会显示:
    Applying: XXXXX.
    XXXXX就是那笔patch本身的log信息
    然后git log 就能看到那笔patch打上去了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值