Friedrich Ewald My Personal Website

Resetting master branch to origin master

Sometimes your local master branch gets into a state where it has diverged from origin/master. This can happen when you accidentally commit to master instead of a feature branch, or when a rebase goes wrong. To reset your local master branch to match origin/master, first fetch the latest state from the remote:

git fetch origin
Then reset your local branch to match the remote:
git reset --hard origin/master
This moves your local master pointer to the same commit as origin/master and updates your working directory to match. Warning: git reset --hard will discard all uncommitted changes and local commits that are not on the remote. If you have work you want to keep, stash it first with git stash or create a backup branch:
git branch backup-master
git reset --hard origin/master
This way, your local commits are preserved on the backup-master branch in case you need them later.

About the author

is a Staff Software Engineer with a Master's degree in Computer Science. He started this website in late 2015, mostly as a digital business card. He is interested in Go, Python, Ruby, SQL- and NoSQL-databases, machine learning and AI and is experienced in building scalable, distributed systems and micro-services at multiple larger and smaller companies.