Git Conflicts

Updated on 28 Dec 2018

So we want to update our working code with the latest changes from the repo. Unfortunately when we do that, we are faced with a conflict issue.

Reset

One technique we can employ is the resetting our workspace. I.e. take our workspace back to the latest commit. The --hard flag means that our source files will be updated!

git reset --hard HEAD

Stash

Stash allows us to stash our changes and revert back to the latest commit. This is usually done where we want to practice something or try out a new piece of code; and maybe go back to it later.

However as we can see in this case, we cant escape the conflict.

The main stash commands I use are:

  • git stash - put your latest changes into stash, and revert your workspace back to the latest commit
  • git stash apply - take your latest stash and reapply it back to your workspace
  • git stash drop - drop the latest stash from the stash memory.

It is important to note that git stash apply does not remove the stash from storage. It will remain there indefinitely until you git stash drop it. apply and drop usually go hand in hand.

MergeTool

If we wish to merge the changes, then we could use the mergetool kdiff3 to help us. The final steps in this process are:

  • git mergetool
  • git clean -i
  • git commit

This will then open the kdiff3 UI which displays the code in 3 windows. We don’t actually want the code shown in Window A, so we can just resize the other 2 windows over the top.

  • Window B - Local Repo
  • Window C - Remote Repo

Kdiff will then highlight the lines of code that have a conflict, and we just need to click the button B or C to indicate which line of code we want in our final output.

Once we have cycled thru each of the conflicts in that file, we’ll be able to save and exit. kdiff will then open up the next file with conflicts and you repeat the cycle until all conflicts are resolved. Once that is done you can clean up the temporary files and commit the changes.