Git mergetool: merging three files.

Sometimes you don't wand to merge folders, you just want to merge files. In fact, handling a Git merge is the main thing you would use BC for in Git.

Why merge with a GUI mergetool?

In Git, if you edit, and commit a file from your laptop, but also edit and commit the same file in your remote repository (e.g. Gitlab), you might not be allowed to push or pull. Git may warn you that you have committed twice, and that the code is different in the two commits.

Before trying the following example, make sure your Git knows you have installed other software to help it: installation instructions link.

Step by step: how to fix a merge conflict with a Git mergetool

  • In a browser like Firefox or Brave, log into your remote repository on Github or similar. GitHub allows you to directly edit files in the browser; change some lines in your ReadMe.md. Save & Commit. Switch to your local version of that same repository and, change some different lines of ReadMe.md. Save & commit.

  • Do a git pull myRepo master . Git will complain.

  • Type git mergetool -- myFileName (assuming you already set up BC, per the link above)

  • You will see Beyond Compare open up, with not two but three versions of your conflicted file. The middle is the oldest-version, called base. This is the commit that Git has determined is the old common ancestor of your two conflicted files. It is a reference useful to both you and recently to Git's failed attempt (see screenshot above) to do an automatic merge.

  • In the BC pulldown menus: turn on View->LineNumbers to make things clearer.

  • Choose your lines by mouse clicking the blocky arrows of the versions you want. BC will suggest which version it likes by giving a different colored arrow but you are the boss, so click anything you want

  • The merge interface can get pretty crowded: to see detail, in context, make good use of the stacked racetrack in the middle.

  • Once you are happy with how the bottom pane looks hit the save key.

  • Close Beyond Compare.

  • You should now be back in the terminal. At the command line check to see if everything is okay

  • Don't forget to commit your change. Best practice says to use the imperative tense, not past tense in your commit message.

Review

Merge is neccessary when your remote repo gets ahead of your local repo, sowing seeds of confusion for you as well as for Git. For trivial conflicts, Git will kindly automerge them when you type git pull thatRepo master. If the changes are more significant, however, you will need to tell Git, line-by-line which code you want to keep. Here is the sequence of steps.

  1. Git sends you a message: "CONFLICT (content): Merge conflict in yourfile.py"

  2. git mergetool -- yourfile.py

  3. when Beyond Compare opens up, press blocky arrows to choose lines

  4. hit the save button in Beyond Compare

  5. close BC

  6. important: git commit -m "Fix merge conflict."

More help

‚ÄčGitGuys have a quick tutorial invoking git mergetool using the old, free tools Kdiff3 and Meld.

NOTE: The $30 version of BC does not have this feature, it is only in the $60 version. If you are cheap, like me, just realize that this license is a lifetime license and the program is similar to having MS Word: you almost can't live without it once you learn it. Git merges are only about 10% of what I use BC for.