我如何在我的 Git 存储库中解决合并冲突?


当前回答

下面是可能的使用案例,从顶部:

你会拖一些变化,但哦,你没有到目前为止:

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.

是的!

其他回答

对于使用 Visual Studio 的用户(Visual Studio 2015 在我的情况下)

关闭您的项目在Visual Studio. 特别是在大型项目,Visual Studio倾向于在使用UI合并时脱离。 立即在命令中进行合并. git checkout target_branch git merge source_branch 然后在Visual Studio中打开项目,然后转到Team Explorer → Branch。

下面是可能的使用案例,从顶部:

你会拖一些变化,但哦,你没有到目前为止:

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.

是的!

CoolAJ86的答案相当于一切. 如果您在同一代码中有两个分支的变化,您将不得不进行手动合并. 在任何文本编辑器中打开冲突中的文件,您应该看到下列结构。

(Code not in Conflict)
>>>>>>>>>>>
(first alternative for conflict starts here)
Multiple code lines here
===========
(second alternative for conflict starts here)
Multiple code lines here too    
<<<<<<<<<<<
(Code not in conflict here)

选择一个替代品或两者的组合,以一种方式,你想要新的代码是,同时删除平等的标志和角膜。

git commit -a -m "commit message"
git push origin master

使用耐心

对于一个大合并冲突,使用耐心为我提供了良好的结果,它会试图匹配区块而不是个别的线条。

例如,如果您更改您的程序的定位,默认 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>

如果你想从分支测试到大师合并,你可以遵循以下步骤:

步骤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:然后测试和主人之间没有冲突,你可以直接使用合并。