我如何在我的 Git 存储库中解决合并冲突?
当前回答
我发现的最好的东西是使用“diff3”合并冲突风格:
<<<<<<<
Changes made on the branch that is being merged into. In most cases,
this is the branch that I have currently checked out (i.e. HEAD).
|||||||
The common ancestor version.
=======
Changes made on the branch that is being merged in. This is often a
feature/topic branch.
>>>>>>>
如果冲突更长,那么我会把三个部分切成三个单独的文件,如“小”,“普通”和“天花板”。
diff common mine
diff common theirs
提示2
git log --merge -p <name of file>
如果你有自动测试,运行这些,如果你有绳子,运行它,如果这是一个可构建的项目,然后在你承诺之前建造它,等等,在所有情况下,你需要做一些测试,以确保你的变化没有打破任何东西。
例如,如果你知道你和另一个人都在不同的重复工作,这两者都会影响相同的文件集,你应该提前彼此谈话,并更好地了解你每个人都在做什么类型的变化。
如果你不确定合并,不要强迫它。
其他回答
下面是可能的使用案例,从顶部:
你会拖一些变化,但哦,你没有到目前为止:
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
解决冲突的更安全的方式是使用 git-mediate (这里提出的常见解决方案是相当错误的 prone imho)。
查看此帖子,以便快速介绍如何使用它。
如果你只是想恢复远程主机,那么
git reset --hard origin/master
警告:所有本地变更将被丢失,请参见 https://stackoverflow.com/a/8476004/11769765.
当运行 git fetch 然后 git merge 当运行 git fetch 然后 git rebase 当运行 git pull (实际上相当于上述条件之一) 当运行 git stash pop 当应用 git patches (以电子邮件传输的文件出口的命令)
你需要安装一个合并工具,它是兼容的Git解决冲突. 我个人使用KDiff3,我发现它很好和方便. 你可以下载它的Windows版本在这里:
https://sourceforge.net/项目/kdiff3/文件/
然后设置 Git 配置以使用 KDiff3 作为其 mergetool:
$ git config --global --add merge.tool kdiff3
$ git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add mergetool.kdiff3.trustExitCode false
$ git config --global --add diff.guitool kdiff3
$ git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add difftool.kdiff3.trustExitCode false
(请记住将路径替换为 KDiff3 EXE 文件的实际路径。
$ git mergetool
然后它打开Kdiff3,并首先试图自动解决合并冲突。
下面是Kdiff3看起来怎么样:
$ git mergetool
No files need merging
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别