我如何在我的 Git 存储库中解决合并冲突?
当前回答
当运行 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 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.
是的!
如果你想从分支测试到大师合并,你可以遵循以下步骤:
步骤1:进入分支
git checkout test
步骤2:
git pull --rebase origin master
步骤3:如果有任何冲突,请转到这些文件来修改它。
步骤4:添加这些变化
git add #your_changes_files
步骤5:
git rebase --continue
步骤6:如果仍然存在冲突,再回到步骤3;如果没有冲突,就如下:
git push origin +test
步骤7:然后测试和主人之间没有冲突,你可以直接使用合并。
我正在使用Microsoft的Visual Studio代码来解决冲突. 它很容易使用. 我在工作场所保持我的项目开放. 它检测和突出冲突. 此外,它提供了GUI选项,以选择我想要从头部或进入的任何变化。
此分類上一篇
我明白什么是合并冲突,但当我看到Git diff的输出时,这对我一开始就看起来很荒谬:
git diff
++<<<<<<< HEAD
+ display full last name boolean in star table
++=======
+ users viewer.id/star.id, and conversation uses user.id
+
++>>>>>>> feat/rspec-tests-for-cancancan
但是,这里是帮助我的一点:
<<<<<和 =======之间的一切都是在一个文件中的一切,而在 =======和 >>>>>之间的一切都是在另一个文件中的一切,所以你必须做的就是打开与合并冲突的文件,并从任何分支中删除这些线条(或者只是让它们相同),合并将立即成功。
使用耐心
对于一个大合并冲突,使用耐心为我提供了良好的结果,它会试图匹配区块而不是个别的线条。
例如,如果您更改您的程序的定位,默认 Git 合并策略有时匹配单手 { 属于不同的功能。
git merge -s recursive -X patience other-branch
从文档中:
With this option, merge-recursive spends a little extra time to avoid
mismerges that sometimes occur due to unimportant matching lines
(e.g., braces from distinct functions). Use this when the branches to
be merged have diverged wildly.
与共同祖先相比
如果你有一个合并冲突,想要看到其他人在修改分支时想知道什么,有时更容易将他们的分支直接与普通祖先(而不是我们的分支)进行比较。
git diff $(git merge-base <our-branch> <their-branch>) <their-branch>
通常,您只希望看到特定文件的更改:
git diff $(git merge-base <our-branch> <their-branch>) <their-branch> <file>
推荐文章
- VS 2017 Git本地提交数据库。每次提交时锁定错误
- 如何在过去的一些任意提交之间注入一个提交?
- 从GitHub克隆项目后拉git子模块
- GitHub上的分叉和克隆有什么区别?
- 递归地按模式添加文件
- 我如何使用notepad++(或其他)与msysgit?
- 如何将现有的解决方案从Visual Studio 2013添加到GitHub
- Git存储库中的悬垂提交和blob是什么?它们来自哪里?
- 我如何简单地从我最新的git提交创建一个补丁?
- Git显示“警告:永久添加到已知主机列表”
- 我如何检索一个回购的远程git地址?
- 如何列出提交,因为某些提交?
- 如何在不位于存储库的情况下执行Git命令?
- 为什么git在Windows下记不住我的密码
- 我如何得到bash完成工作与别名?