Git Mergetool
Overview
Git Mergetool is a GUI tool to help developers to resolve conflicts efficiently. In the GUI the current branch version, the remote branch, the common ancestor, and the final merged branch are visible.
Pre-requisites
Before learning about Git setup mergetool the developer should already know about merge conflicts and branches in git.
Introduction to Resolving Git Conflicts
Git conflicts occur when the same line of code in a file is changed in more than one branch and then those branches are merged. Every developer faces the situation of Git conflict and has their way of resolution. There are many ways to resolve Git conflicts and every developer prefers their way of resolving Git conflicts.
In case of conflict, Git does not overdo stuff and lets the developer decide which changes to keep and which changes to discard. Git only affects the person performing the merge, the rest of the team is unaware of the conflict. Git will mark the file as conflicted and halt the merging process. It is then the responsibility of the developer to resolve the conflicts.
What is Git Mergetool?
Git mergetool is a tool that helps developers to resolve Git conflicts efficiently. Git mergetool is a GUI tool that shows the current branch, remote branch, and the ancestor branch of both the current and remote branch and also shows the final merged branch.
To use Git setup mergetool you should know about the following terminologies :
- LOCAL - The Head of the file(s) for the current branch that you are using in your local.
- REMOTE - The Head of the file(s) for the remote location branch which you are trying to merge in your local.
- BASE - The common ancestor of both local and remote.
- MERGED - The Head object after the merge is done.
Git setup mergetool syntax :
Further in this article, we will see an example of how the remote branch will be merged with the local branch using Git mergetool.
Options
You can configure the Git setup mergetool of your choice with the help of different options that git Git setup mergetool provides.
Some of the Options that can be used to configure Git setup mergetool are :
- -t
or --tool= - You can use a different editor to run the merge resolution Program of Git mergetool. some valid editors that can be used to run merge resolution programs are emerging, gvimdiff, kdiff3, meld, vimdiff, and tortoisemerge.
you can also see the available editor supported by Git setup mergetool with the help of the command
Output:
You can also set up a different editor(let's say kdiff3) for Git mergetool to run the merge resolution program by using merge. a tool or --tool==tool or -t tool :
Before setting up any editor for Git setup mergetool make sure it is installed on your computer and the PATH variable contains the path to that executable editor.
You can set a different path to the kdiff3 executable, by using the command:
If the path is not given then mergetool assumes the tool to be available in the PATH variable.
- trustExitCode - If your custom configured mergetool indicates that the merge conflict is resolved successfully then you can set the trustExitCode to true. Otherwise, Git setup mergetool will prompt to indicate whether the merge conflict was resolved or not.
command to set trustExitCode to true is
- prompt - If you want to be prompted each time the mergetool editor is invoked, use the following command :
By default, mergetool is set to no prompt i.e you are not prompted each time mergetool is invoked. You can also set the mergetool to no prompt by using --no-prompt or -y :
- GUI tool - To read the default mergetool configured in the merge.GUI tool we have to invoke the Git setup mergetool using -g or --GUI tool command. If merge.GUI tool is not configured then the default merge. the tool will be used.
To remove the read from a merge.GUI tool you have to invoke the mergetool using -no--GUI. This will read the mergetool from the default merge. tool variable.
For more options, you can refer to the
Configuration
You can set the different configurations of Git setup mergetool using the following commands :
-
mergetool.
.path This command is used to override the path for a tool. If the tool is not specified any path then this command can be used to set the path of the tool. -
mergetool.
.hideResolved This command overrides the global value of mergetool.hideResolved for a specific tool. -
mergetool.hideResolved This command is used to only show the non-merged conflict in the git mergetool. Its default value is false. It can be overwritten by mergetool.
.hideResolved for specific tools. -
mergetool.keepBackup When the conflict is resolved the original file which contained the conflicts can be saved as a backup with a .orig extension. The default value of mergetool.keepBackup is true.
-
mergetool.keepTemporaries This command is used to keep the temporary files that are used by custom-configured git mergetool. Whenever a custom mergetool is invoked git passes some temporary files to the tool. If the keepTemporaries variable is set to true then these files will be preserved even if the mergetool exits with an error.
-
mergetool.prompt This command is used to prompt every time the Git mergetool is invoked.
Backend Specific Hints
If you have configured the vimdiff tool for mergetool then Git will open the vim editor with a 4-window layout as shown below.
LOCAL, BASE, and REMOTE are the commit you are merging into, the common ancestor commit, and the commit you are merging from respectively. These are read-only buffers and MERGED is the writeable buffer where you have to resolve the conflict using the read-only buffer LOCAL and REMOTE.
Once the conflict is resolved in the MERGED buffer then you can exit vim by the command
Mergetool Simple Code Example
We will create the file and introduce a conflict situation in the file and try to resolve the merge conflict in this section.
Let's first create a git repo named "names" and initialize git in it.
Once git is initialized in the repo create a file called ActorNames and open it in the vim editor using the vim command.
Now add some actor names in the ActorNames file and save and exit vim by wq. You can see the content of the text file by using more commands.
Now we will commit the changes made to the ActorNames.txt file.
Now we will create a new branch from the current branch and checkout that branch and try to modify the file ActorNames.txt.
We edited the names of the actor to their full name in the ActorNames.txt file. The contents of the ActorNames file are now :
Now let's commit our changes in a feature branch.
Now we will switch to our master branch and then add some movie names of actors as suffixes to the name and then try to merge our feature branch to the master branch.
When trying to merge it will throw an error saying "Merge Conflict in ActorNames.txt file" as shown in the image above.
How to Fix a Merge Conflict with a Git Mergetool?
To resolve the conflict we can bring up the mergetool by the command
It will show up a vim layout as :
The top left split panel is the LOCAL, the top middle split is BASE and the top right split is REMOTE. The bottom split refers to the MERGED version.
Now you can directly edit the changes that you want to keep in the merged split section or use the command to access the different split sections.
You can manually edit in the merged split section or pull from the Local, Remote, or Base split section by using the command.
Save the changes and then commit the merge to resolve the conflict.
Conclusion
- Git setup mergetool provides an efficient way to resolve git conflict.
- Git setup mergetool provides a GUI which shows the current branch version, the remote branch, the common ancestor, and the final merged branch.
- Git merge tool has several options to configure a custom Git setup mergetool.
- Git setup mergetool can be started(by the Git mergetool command) after the merge operation throws the conflict error.
- Git setup mergetool needs some editor to run the conflict resolution program. The command git setup mergetool --tool-help shows the different tools supported by git setup mergetool in your system.