Resolve Merge Conflict with 'git mergetool' and meld

If you work with your friends on the same git repository, you might edit the same file. If you edit the bottom and your friend edits the top, git merges the changes automatically.
If two users edit the same line, git notices merge conflict. A human has to decide which line stays.
This article describes an easy way to solve merge conflicts using graphical merge tool meld.

Prequisites: command line basics, git basics
I have written this article from memory.

Enjoying Automatic Merge

Git can merge most changes automatically. To enjoy this feature,

  • Pull before push.
  • ‘git pull && git push’ often

A simple workflow for this is described in Publish Your Project with GitHub:

$ git add . && git commit; git pull && git push

If your merge was automatically resolved (like most are), you don’t have to do anything. Just stop reading this article and keep on coding.

Resolve Merge Conflict

If two users have edited the same line at the same time, the last one gets a merge conflict.
Install meld

$ sudo apt-get update
$ sudo apt-get -y install meld

Tell git mergetool not to scatter .orig files around. This is a one-time setting.

$ git config --global mergetool.keepBackup false

When you have a merge conflict, you can run mergetool.
(If you want to cause a merge conflict to test this, create a repository with two users and edit the same line, commit both changes and only pull&push after that.)

$ git mergetool

Choose meld.
A windows with three panes open. Usually, LOCAL (yours) is on the left, REMOTE (theirs) is on the right, and the final merged file is in the center. Choose lines by clicking on the left and right. When you are happy with the file, save.
And finally, commit your merged files.

$ git add . && git commit; git pull && git push

Well done, you can now resolve merge conflicts. Pull and push often, and git will automatically merge most of the time. Then you won’t need to resolve that many merge conflicts by hand.

Discussion

Command line is the easiest way to use git. If you have any trouble with “helpful” graphical tools, the usual answer is to return to regular command line git.
Merge conflicts are often easiest to resolve with a graphical tool. If you want to learn to do it on the command line, it might be the easiest to try it with meld first.
Update: fixed typo.

Posted in Uncategorized | Tagged , , , , , , | 1 Comment

One Response to Resolve Merge Conflict with 'git mergetool' and meld

  1. olidev says:

    I didn’t know about Git merging automatically. Nice tip. Up until now I was using tool to resolve merge conflicts. The other two methods (as mentioned here: https://www.cloudways.com/blog/manage-branches-and-resolve-conflicts-git/ ) take longer for me to do the same thing.