How to resolve merge conflicts in Git

2018-12-31 00:06发布

Is there a good way to explain how to resolve merge conflicts in Git?

32条回答
浪荡孟婆
2楼-- · 2018-12-31 00:26
git log --merge -p [[--] path]

Does not seem to always work for me and usually ends up displaying every commit that was different between the two branches, this happens even when using -- to separate the path from the command.

What I do to work around this issue is open up two command lines and in one run

git log ..$MERGED_IN_BRANCH --pretty=full -p [path]

and in the other

git log $MERGED_IN_BRANCH.. --pretty=full -p [path]

Replacing $MERGED_IN_BRANCH with the branch I merged in and [path] with the file that is conflicting. This command will log all the commits, in patch form, between (..) two commits. If you leave one side empty like in the commands above git will automatically use HEAD (the branch you are merging into in this case).

This will allow you to see what commits went into the file in the two branches after they diverged. It usually makes it much easier to solve conflicts.

查看更多
情到深处是孤独
3楼-- · 2018-12-31 00:28

For those who are using Visual Studio (2015 in my case)

  1. Close your project in VS. Especially in big projects VS tends to freak out when merging using the UI.

  2. Do the merge in command prompt.

    git checkout target_branch

    git merge source_branch

  3. Then open the project in VS and go to Team Explorer -> Branch. Now there is a message that says Merge is pending and conflicting files are listed right below the message.

  4. Click the conflicting file and you will have the option to Merge, Compare, Take Source, Take Target. The merge tool in VS is very easy to use.

查看更多
长期被迫恋爱
4楼-- · 2018-12-31 00:28

Using patience

I'm surprised no one else spoke about resolving conflict using patience with the merge recursive strategy. For a big merge conflict, using patience provided good results for me. The idea is that it will try to match blocks rather than individual lines.

If you change the indentation of your program for instance, the default Git merge strategy sometimes matches single braces { which belongs to different functions. This is avoided with patience:

git merge -s recursive -X patience other-branch

From the documentation:

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.

Comparison with the common ancestor

If you have a merge conflict and want to see what others had in mind when modifying their branch, it's sometimes easier to compare their branch directly with the common ancestor (instead of our branch). For that you can use merge-base:

git diff $(git merge-base <our-branch> <their-branch>) <their-branch>

Usually, you only want to see the changes for a particular file:

git diff $(git merge-base <our-branch> <their-branch>) <their-branch> <file>
查看更多
裙下三千臣
5楼-- · 2018-12-31 00:29

Merge conflicts could occur in different situations:

  • When running "git fetch" and then "git merge"
  • When running "git fetch" and then "git rebase"
  • When running "git pull" (which is actually equal to one of the above-mentioned conditions)
  • When running "git stash pop"
  • When you're applying git patches (commits that are exported to files to be transferred, for example, by email)

You need to install a merge tool which is compatible with Git to resolve the conflicts. I personally use KDiff3, and I've found it nice and handy. You can download its Windows version here:

https://sourceforge.net/projects/kdiff3/files/

BTW if you install Git Extensions there is an option in its setup wizard to install Kdiff3.

Then setup git configs to use Kdiff as its 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

(Remember to replace the path with the actual path of Kdiff exe file.)

Then every time you come across a merge conflict you just need to run this command:

$git mergetool

Then it opens the Kdiff3, and first tries to resolve the merge conflicts automatically. Most of the conflicts would be resolved spontaneously and you need to fix the rest manually.

Here's what Kdiff3 looks like:

Enter image description here

Then once you're done, save the file and it goes to the next file with conflict and you do the same thing again until all the conflicts are resolved.

To check if everything is merged successfully, just run the mergetool command again, you should get this result:

$git mergetool
No files need merging
查看更多
不再属于我。
6楼-- · 2018-12-31 00:30

I follow the below process.

The process to fix merge conflict:

  1. First, pull the latest from the destination branch to which you want to merge git pull origin develop

  2. As you get the latest from the destination, now resolve the conflict manually in IDE by deleting those extra characters.

  3. Do a git add to add these edited files to the git queue so that it can be commit and push to the same branch you are working on.

  4. As git add is done, do a git commit to commit the changes.

  5. Now push the changes to your working branch by git push origin HEAD

This is it and you will see it resolved in your pull request if you are using Bitbucket or GitHub.

查看更多
公子世无双
7楼-- · 2018-12-31 00:30

Here's a probable use-case, from the top:

You're going to pull some changes, but oops, you're not up to date:

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.

So you get up-to-date and try again, but have a conflict:

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.

So you decide to take a look at the changes:

git mergetool

Oh me, oh my, upstream changed some things, but just to use my changes...no...their changes...

git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"

And then we try a final time

git pull origin master

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Already up-to-date.

Ta-da!

查看更多
登录 后发表回答