Adrian Andreias

Sep 01, 2014

Git cancel pull merge and overwrite local files

I'm usually writing code on two machines: the Intel NUC running Arch Linux at the office and the Windows 8.1 HP Ultrabook at home (the Microsoft Office package keeps me on Windows. I tried Evolution with the Exchange plugin a few days ago on my Arch Linux and it was stalling the machine with my 5GB of mail and I had to cold reboot a few times a day). Good thing PyCharm allows me to use my license on multiple machines.

I have a personal repository on our Gitlab server and I use it to copy code from one machine to another:

  1. I'm amending the last commit and push it to server from the machine I've last used:
git add . git commit --amend --no-edit git pus
  1. On the second machine I usually just pull:
git pull

This might cause a merge conflict if I happen to have some small and unimportant changes on the second machine. This will always cause a conflict when you're actually overwriting the local history. If I am sure that I can dump the changes on the local machine I can force the pull to overwrote local changes:

# cancel the current merge that was initiated by git pull:
git reset --hard HEAD
# not normally needed since git pull already fetched:
git fetch --all
# use remote code and overwrite local code:
git reset --hard origin

And this little article should save me the googling I do each time I do a git pull. :)