Skip to the relevant sections if needed.
- Concepts for resolving
Git conflicts - Setting up different editors / tool for using
git mergetool mergetoolsimple code example- Other great references and tutorials
For using mergetool in git, we need to understand the following terminology to understand what is being merged:
LOCAL- the file(s) from the current branch on the machine that you are using.REMOTE- the files(s) from a remote location that you are trying to merge into yourLOCALbranch.BASE- the common ancestor(s) ofLOCALandBASE.MERGED- the tag /HEADobject after the merge - this is saved as a new commit.
Common mergetool from editors will display both LOCAL, REMOTE and BASE so you can decide which changes to keep.
We have to change the git config to set a default mergetool.
In this example, we will use vimdiff:
$ git config merge.tool vimdiff We can also set the editor to display the common ancestor BASE while we examine what changes are in LOCAL and REMOTE with the following setting:
$ git config merge.conflictstyle diff3
$ git mergetool --tool-help
And we list a few of them:
gvimdiff- almost identical tovimdiffbut uses the Linux GUI forVim, please refer tovimdiffif you still use the keyboard commands forGVim.kdiff3meldtortoisemerge
Or consult the community of your favorite editor to see how to do the equivalent operations for your editor.
Do not prompt before launching the merge resolution tool
$ git config mergetool.prompt false
$ mkdir galaxyZoo
$ cd galaxyZoo
$ git init
$ vim astrophy_obj.txtAdd some galaxy types into astrophy_obj.txt then save the file.
# content of astrophy_obj.txt
spiral
ellipitcal
bar
irregular
save then commit the file.
$ git add astrophy_obj.txt
$ git commit -m 'Initial commit'
$ git branch astrophy_objects # create a new branch
$ git checkout astrophy_objects # change to new branch
$ vim astrophy_obj.txt # make changes to file
Change bar to barred in the file.
$ git commit -am 'changed bar to barred'
$ git checkout master # change back to master branch
$ vim astrophy_obj.txt
# add the word `galaxy` to the end of each line using Vim REGEX
# type `:%s/$/ galaxy/g` in Vim then press enter and save `:wq`
$ git commit -am 'added galaxy to each line'
# merge from the astrophy_objects branch to current branch with is master
$ git merge astrophy_objects
Then you will see some error messages:
Auto-merging astrophy_obj.txt
CONFLICT (content): Merge conflict in astrophy_obj.txt
Automatic merge failed; fix conflicts and then commit the result.
We can bring up the mergetool:
$ git mergetool
Then it will bring up the different versions of the file in different Vim splits panels.
From top left split is the LOCAL, top middle split is BASE and top right split is REMOTE.
The bottom split refers to the MERGED version.
You can find this info in the bottom bar of each split (I have put 3 red rectangles to highlight that info).
As you can see form the below image, my Vim has highlighted the differences in red for me.
Now if your terminal has any GUI capability and you have compiled Vim correctly with GUI support, you can use your mouse to click on the bottom split to edit it.
Or you are a Vim ninja, you can use the keyboard shortcut to move to different splits.
Ctrl w + h # move to the split on the left
Ctrl w + j # move to the split below
Ctrl w + k # move to the split on top
Ctrl w + l # move to the split on the right
git mergetool page on git-scm.com
ref: tutorial about branching and merging from Charles Duan
