前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Git冲突解决: git checkout高级用法

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

原创
作者头像
叉叉敌
发布2019-04-29 21:10:57
3.9K0
发布2019-04-29 21:10:57
举报
文章被收录于专栏:ChasaysChasays

背景

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

解决方法

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

举个栗子

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

代码语言:javascript
复制
# 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

查看当前仓库的状态

代码语言:javascript
复制
# 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

用提示的命令执行

代码语言:javascript
复制
# 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

代码语言:javascript
复制
git checkout --theirs file1.txt

然后添加到本地仓库

代码语言:javascript
复制
git add -A

最后继续cherry-pick「复制」

代码语言:javascript
复制
# 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还是有区别的。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 解决方法
  • 举个栗子
  • 小结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com