1. Install Meld#
winget install -e --id Meld.Meld2. Set up Meld as my Git difftool#
It makes more sense to configure it in your .gitconfig file.
# Add the following to your .gitconfig file.
[diff]
tool = meld
[difftool]
prompt = false
[difftool "meld"]
cmd = meld "$LOCAL" "$REMOTE"(Note: These settings will not alter the behaviour of git diff which will continue to function as usual.)
You use git difftool in exactly the same way as you use git diff. E.g.,
git difftool <COMMIT_HASH> file_name
git difftool <BRANCH_NAME> file_name
git difftool <COMMIT_HASH_1> <COMMIT_HASH_2> file_nameIf properly configured, a Meld window will open displaying the diff using a GUI interface.
The order of the Meld GUI window panes can be controlled by the order of $LOCAL and $REMOTE in cmd, that is to say which file is shown in the left pane and which in the right pane. If you want them the other way around simply swap them around like this:
cmd = meld “$REMOTE” “$LOCAL” Finally, the prompt = false line simply stops Git from prompting you as to whether you want to launch Meld or not, by default Git will issue a prompt.
3. Set up Meld as my Git mergetool#
Like difftool, it makes more sense to configure it in your .gitconfig file.
# Add the following to your .gitconfig file.
[merge]
tool = meld
[mergetool "meld"]
# Choose one of these two lines (not both!) explained below.
cmd = meld "$LOCAL" "$MERGED" "$REMOTE" --output "$MERGED"
cmd = meld "$LOCAL" "$BASE" "$REMOTE" --output "$MERGED"Firstly here is what the parameters mean:
$LOCALis the file in the current branch (e.g., master).$REMOTEis the file in the branch being merged (e.g., branch_name).$MERGEDis the partially merged file with the merge conflict information in it.$BASEis the shared commit ancestor of $LOCAL and $REMOTE, this is to say the file as it was when the branch containing $REMOTE was originally created.
I suggest you use either:
[mergetool "meld"]
cmd = meld "$LOCAL" "$MERGED" "$REMOTE" --output "$MERGED"or:
[mergetool "meld"]
cmd = meld "$LOCAL" "$BASE" "$REMOTE" --output "$MERGED"
# See 'Note On Output File' which explains --output "$MERGED".The choice is whether to use $MERGED or $BASE in between $LOCAL and $REMOTE.
Either way Meld will display three panes with $LOCAL and $REMOTE in the left and right panes and either $MERGED or $BASE in the middle pane.
In both cases the middle pane is the file that you should edit to resolve the merge conflicts. The difference is just in which starting edit position you’d prefer; $MERGED for the file which contains the partially merged file with the merge conflict information or $BASE for the shared commit ancestor of $LOCAL and $REMOTE. [Since both cmd lines can be useful I keep them both in my .gitconfig file. Most of the time I use the $MERGED line and the $BASE line is commented out, but the commenting out can be swapped over if I want to use the $BASE line instead.]
Note On Output File: Do not worry that --output "$MERGED" is used in cmd regardless of whether $MERGED or $BASE was used earlier in the cmd line. The --output option simply tells Meld what filename Git wants the conflict resolution file to be saved in. Meld will save your conflict edits in that file regardless of whether you use $MERGED or $BASE as your starting edit point.
4. Use Meld as my Git mergetool#
You do not use git mergetool to perform an actual merge. Before using git mergetool you perform a merge in the usual way with Git. E.g.,
git checkout master
git merge branch_nameIf there is a merge conflict, Git will display something like this:
git merge branch_nameOutput:
Auto-merging file_name
CONFLICT (content): Merge conflict in file_name
Automatic merge failed; fix conflicts and then commit the result.At this point file_name will contain the partially merged file with the merge conflict information (that’s the file with all the »»»> and «««< entries in it).
Mergetool can now be used to resolve the merge conflicts. You start it very easily with:
git mergetoolIf properly configured, a Meld window will open displaying three files. Each file will be contained in a separate pane of its GUI interface.
In the example .gitconfig entry above, two lines are suggested as the [mergetool “meld”] cmd line. In fact there are all kinds of ways for advanced users to configure the cmd line, but that is beyond the scope of this answer.
This answer has two alternative cmd lines which, between them, will cater for most users, and will be a good starting point for advanced users who wish to take the tool to the next level of complexity.

