我如何在我的 Git 存储库中解决合并冲突?
当前回答
你可以以一些方式解决合并冲突,因为其他人已经详细说明了。
我认为真正的关键是知道如何在本地和远程存储库中流动的变化。 关键是理解跟踪分支. 我发现我认为跟踪分支是我之间的“中间失落的片段”我的本地,实际的文件目录和远程定义为起源。
而不是:
git add .
git commit -m"some msg"
相反,我做:
git add file,file2,file3...
git commit # Then type the files in the editor and save-quit.
因此,你更有意识到哪些文件被添加,你也可以查看列表,并在使用编辑器为消息时思考更多。
[更新 - 随着时间的流逝,我已经更换到:
git status # Make sure I know whats going on
git add .
git commit # Then use the editor
)
此外(更重要的是你的情况),我试图避免:
git pull
或
git pull origin master.
git checkout master
git fetch
git rebase --hard origin/master # or whatever branch I want.
你也可能會發現這有用:
git 分支, fork, fetch, merge, rebase 和 clone,什么是差异?
其他回答
下面是可能的使用案例,从顶部:
你会拖一些变化,但哦,你没有到目前为止:
git fetch origin
git pull origin master
From ssh://gitosis@example.com:22/projectname
* branch master -> FETCH_HEAD
Updating a030c3a..ee25213
error: Entry 'filename.c' not uptodate. Cannot merge.
所以你更新并尝试一次,但有冲突:
git add filename.c
git commit -m "made some wild and crazy changes"
git pull origin master
From ssh://gitosis@example.com:22/projectname
* branch master -> FETCH_HEAD
Auto-merging filename.c
CONFLICT (content): Merge conflict in filename.c
Automatic merge failed; fix conflicts and then commit the result.
所以你决定看看这些变化:
git mergetool
哦,我的,哦,我的,上流改变了一些事情,但只是用我的变化......不......他们的变化......
git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"
然后,我们试着最后一次。
git pull origin master
From ssh://gitosis@example.com:22/projectname
* branch master -> FETCH_HEAD
Already up-to-date.
是的!
查看 Stack Overflow 问题中的答案 堕胎在 Git 中的合并,特别是 Charles Bailey 的答案,显示如何查看不同版本的文件有问题,例如,
# Common base version of the file.
git show :1:some_file.cpp
# 'Ours' version of the file.
git show :2:some_file.cpp
# 'Theirs' version of the file.
git show :3:some_file.cpp
如果您不使用工具合并,首先复制您的代码:
- `checkout master`
- `git pull` / get new commit
- `git checkout` to your branch
- `git rebase master`
它解决冲突,你可以复制你的代码。
对于使用 Visual Studio 的用户(Visual Studio 2015 在我的情况下)
关闭您的项目在Visual Studio. 特别是在大型项目,Visual Studio倾向于在使用UI合并时脱离。 立即在命令中进行合并. git checkout target_branch git merge source_branch 然后在Visual Studio中打开项目,然后转到Team Explorer → Branch。
试试:
git mergetool
它打开一个GUI,你通过每个冲突,你得到选择如何合并。有时它需要一些手编辑后,但通常它是足够的本身。
根据Josh Glover的评论:
[此命令] 不必打开 GUI 除非你安装一个. 运行 git mergetool 为我导致 vimdiff 被使用. 您可以安装下列工具之一来使用它: meld, opendiff, kdiff3, tkdiff, xxdiff, tortoisemerge, gvimdiff, diffuse, ecmerge, p4merge, araxis, vimdiff, emerge。
下面是使用vimdiff解决合并冲突的样本程序,基于此链接。
推荐文章
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 跟踪所有远程git分支作为本地分支
- 自定义SSH端口上的Git
- git如何显示不存在于.gitignore中的未跟踪文件
- Git错误:遇到7个文件应该是指针,但不是
- GitHub克隆与OAuth访问令牌
- 移动(或“撤销”)最后一个git提交到非暂存区域