Managing Merge Conflicts

The most common cause of merge conflicts happens when another user changes the same file that you just modified. It can happen during pull from a remote repository (or when merging branches).

  1. If you know for sure what file version you want to keep:
  • keep the remote file: git checkout --theirs conflicted_file.txt
  • keep the local file: git checkout --ours conflicted_file.txt

=> You still have to git add and git commit after this

  1. If you do not know why there is a conflict: Dig into the files, looking for:
<<<<<<< HEAD
local version (ours)
remote version (theirs)
>>>>>>> [remote version (commit#)]

=> You still have to git add and git commit after this

During this process, if you want to roll back to the situation before you started the merge: git merge --abort

NOTE: By doing a pull before committing, you can avoid a lot of git conflicts. Your git workflow should therefore be:

  1. git add
  2. git pull
  3. commit -m "descriptive message"
  4. git pull
  5. git push


Continuing with the dessert example we used in the first part of this tutorial, we now have on our local machine