How do I resolve merge conflicts in a Git repository?
git, git-merge, git-merge-conflict, merge-conflict-resolution
Solution
Try:
git mergetool
It opens a GUI that steps you through each conflict, and you get to choose how to merge. Sometimes it requires a bit of hand editing afterwards, but usually it's enough by itself. It is much better than doing the whole thing by hand certainly.
As per Josh Glover's comment:
[This command] doesn't necessarily open a GUI unless you install one. Running `git mergetool` for me resulted in `vimdiff` being used. You can install one of the following tools to use it instead: `meld`, `opendiff`, `kdiff3`, `tkdiff`, `xxdiff`, `tortoisemerge`, `gvimdiff`, `diffuse`, `ecmerge`, `p4merge`, `araxis`, `vimdiff`, `emerge`.
Below is a sample procedure using `vimdiff` to resolve merge conflicts, based on this link.
Run the following commands in your terminal
git config merge.tool vimdiff
git config merge.conflictstyle diff3
git config mergetool.prompt false
This will set `vimdiff` as the default merge tool.
Run the following command in your terminal
git mergetool
You will see a `vimdiff` display in the following format:
╔═══════╦══════╦════════╗
║ ║ ║ ║
║ LOCAL ║ BASE ║ REMOTE ║
║ ║ ║ ║
╠═══════╩══════╩════════╣
║ ║
║ MERGED ║
║ ║
╚═══════════════════════╝
These 4 views are
- LOCAL: this is the file from the current branch
- BASE: the common ancestor, how this file looked before both changes
- REMOTE: the file you are merging into your branch
- MERGED: the merge result; this is what gets saved in the merge commit and used in the future
You can navigate among these views using ctrl+w. You can directly reach the MERGED view using ctrl+w followed by j.
More information about `vimdiff` navigation is here and here.
You can edit the MERGED view like this:
If you want to get changes from REMOTE
:diffg RE
If you want to get changes from BASE
:diffg BA
If you want to get changes from LOCAL
:diffg LO
Save, Exit, Commit, and Clean up
`:wqa` save and exit from vi
`git commit -m "message"`
`git clean` Remove extra files (e.g. `*.orig`). Warning: It will remove all untracked files, if you won't pass any arguments.
Problem
How do I resolve merge conflicts in my Git repository?
Related problems
- Vim: Move window left/right?
- What is the precise meaning of "ours" and "theirs" in git?
- Should diff3 be default conflictstyle on git?
- Why is the meaning of “ours” and “theirs” reversed with git-svn
- What are the differences between git branch, fork, fetch, merge, rebase and clone?
- master branch and 'origin/master' have diverged, how to 'undiverge' branches'?
- I ran into a merge conflict. How do I abort the merge?
- How to resolve git merge conflict, from command line, using a given strategy, for just one file?