Git冲突解决: git checkout高级用法

背景

Git冲突的原因,一般是修改了同一个文件导致的,这个文件有可能是别人提交到远程仓库里面,还有就是需要合并这个文件导致的。

解决方法

  1. 你确定你需要的是哪个仓库的文件
git checkout --theirs conflicted_file.txt  # 保留远端的
git checkout --ours conflicted_file.txt # 保留本地的
  1. 然后执行add和commit
git add -A
git commit -m "update conflict

举个栗子

获取远端服务器上的文件,提示冲突了需要合并

# git cherry-pick FETCH_HEAD
* branch            refs/changes/85/12385/3 -> FETCH_HEAD
error: 'cherry-pick' is not possible because you have unmerged files.
hint: Fix them up in the work tree,
hint: and then use 'git add/rm <file>' as
hint: appropriate to mark resolution and make a commit,
hint: or use 'git commit -a'.
fatal: cherry-pick failed

查看当前仓库的状态

# git status
Not currently on any branch.
You are currently cherry-picking commit 53e5374.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
    both modified: file1.txt
    

用提示的命令执行

# git cherry-pick --continue

U   file1.txt

error: 'commit' is not possible because you have unmerged files.
hint: Fix them up in the work tree,
hint: and then use 'git add/rm <file>' as
hint: appropriate to mark resolution and make a commit,
hint: or use 'git commit -a'.
fatal: Exiting because of an unresolved conflict.

提示file1.txt有更新,确定替换为远程仓库的文件。这里用theirs

git checkout --theirs file1.txt

然后添加到本地仓库

git add -A

最后继续cherry-pick「复制」

# git cherry-pick --continue
[detached HEAD 8f26ce8] Summary : test git checkout --theirs
 Author: Rik
 2 files changed, 0 insertions(+), 0 deletions(-)

小结

git ckeckout 和 带参数的--ours和 --theirs还是有区别的。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、基本概念: 注:对于git的分布式概念及其优点,不重复说明,自己百度或谷歌。本文中涉及到指令前面有$的,在cm...
    大厂offer阅读 1,489评论 0 3
  • (预警:因为详细,所以行文有些长,新手边看边操作效果出乎你的预料) 一:Git是什么? Git是目前世界上最先进的...
    axiaochao阅读 1,971评论 1 8
  • 昨天在同事电脑上操作了一把cherry-pick代码,发现很多功能不用,就慢慢忘记了,梳理了下流程图: git c...
    gogoingmonkey阅读 708评论 0 0
  • 第一部分是文档,为了日后查看方便放到了前边[可能敲错了,详见阮一峰老师的文章]http://www.ruanyif...
    白璞1024阅读 1,094评论 0 49
  • 三月回家,心里的期望如天上的太阳 满怀希望的站在大街上 看见人很多,车很多 心里咯噔一下,偌大的城市没有容留我的地...
    导演张升志阅读 252评论 0 0