Git: Merge conflicts on Mac

Branches? Merges? It can be complex.

The use of Git is essential when creating software. As you use it you’ll realise how much of a flexible system it!

Image for post
Image for post
Photo by Yancy Min on Unsplash

Prerequisites:

  • Some knowledge of the terminal.
  • Some previous use of VIM would be advantageous to create, edit and merge files.

Terminology

Git: Git is a distributed version-control system for tracking changes in source code during software development

The following process assumes that you are using a Mac, and you are using the terminal. Terminal commands are prefixed with the dollar symbol ($) but is not typed into the command line itself.

Choosing which mergetool to use(this has to be done for each Git repository you initialise!)

$ git config merge.tool vimdiff

By default git displays the two files to be merged. It is usually a good idea to use diff3 to set the editor to display the common ancestor BASE while examining the changes in LOCAL and REMOTE through

$ git config merge.conflictstyle diff3

Setting up a Git repository

In a new folder

Through the command line we set up a git repository

$ git init

Add a file of animals

$ vim animals.txt

then within vim add the following animals (entering insert mode in vim first):

Image for post
Image for post

and of course save the file through vim with :wq (remembering to exit insert mode first)

Add to git, commit

$ git add .

$ git commit -m “initial commit”

Create a new branch

$ git branch animalsbranch

Switch to the new branch

$ git checkout animalsbranch

Alter the text file on the new branch

$ vim animals.txt

using vim, remove “dave” since (obviously) Dave is not an animal. Save as before (:wq)

Commit the changes to the branch (the a switch stages the file for you here)

$ git commit -am “removed Dave”

Switch to the master branch

$ git checkout master

Alter the text file on the main branch

$ vim animals.txt

Image for post
Image for post

Commit the file (again the am switch is wise to use)

$ git commit -am “Updated the animals to plurals”

Commit the file (remember we are on the animals branch. This can be confirmed with a git status command, or view the branches through git branch)

$ git merge animalsbranch

As instructed, we will fix the conflicts (here using merge tool), and then commit the result on the master banch.

Image for post
Image for post
The errors when trying to merge the branches

Merge into the master branch

$ git mergetool

If you have selected the right mergetool, vim will open

Now you will need to navigate vim to choose which window to change. The merged split is shown at the bottom (and by default this is where you change the file).

Image for post
Image for post

The cursor keys (shown above) care combined with control to select the appropriate window. However you should know that

Control w then h

Control w then j

Control w then k

Control w then l

You can choose to use vim shortcuts for the REMOTE, BASE or LOCAL (respectively, as below)

:diffg RE

:diffg BA

:diffg LO

To close all of the windows at once, you use the command

:wqa

$ git commit -am “merged”

Confirming your commits

$ git log — — oneline

Stashing changes

Sometimes you want to switch branches, not losing your work but equally not committing half-completed work. The solution is stashing.

$ git stash save mystash

this can be followed by listing all stashes (since they are stored in a stack)

$ git stash list

meaning that stashes simply work like a stack

Image for post
Image for post
Two existing stashes are joined by stash 3 on the stack

It often makes sense to create a branch from a stash, so then it can be merged afterwards.

$ git stash branch testchanges

which puts you on the branch testchanges. As before, you can switch to master and then merge back to master with any changes.

Tips and tricks

Reverting your merge

<<<<<<< HEAD

=======

>>>>>>>

To revert you can abort

$ git merge — abort

but you will be left with a series of backup and orig files:

Removing *.orig files

These can be removed through the command

$git clean -f *.orig

or remove all untracked files (use with care! The first option gives you a list, the second deletes)

$git clean -n

$git clean -f

You resolve the merge conflicts using merge tool, and then

$git push

Want to get in contact? Use Twitter:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store