Git 101: Peace of mind with git reset --hard


3 min read

This post is one of the Git 101: Conquer the GIT POWER series.


  1. Intro
  2. Reset everything from scratch
  3. Hotfix with a clean state
  4. Develop to a new feature
  5. Outro

1 - Intro

Sometimes you need to start from scratch, switch to master to hotfix something, or start a new branch with the updated develop code. The git reset --hard operation can speed up reverting the changes in those situations.

2 - Reset everything from scratch

Why not use the git checkout . command instead?

The results are pretty much the same. But git checkout does some operations regardless the working tree integrity and it can significantly slow down the the revert process.

Git reset does not care about those that and that is a good thing, because we don't care either.

We just want to nuke everything and get back to the start.

git reset --hard head

And like magic you branch is brand new.

3 - Hotfix with a clean state

You are in the middle of something and need to switch to master to solve something. Let's do it the smart way.

First we need to save our current code. You can use git stash but a more safe approach is just commit the incomplete. We will come back to that later.

git add .
git commit -m "chore: ncomplete commit"

The we go to the master branch.

git checkout master

Like before, why just not use git pull? Well the same rules apply here. Git pull will split into two commands, git fetch and git merge. The git fetch only updates the remote commit references inside your machine, but the git merge operation will do all that shenanigans to your local branch.

And we don't want that. We just want our local master branch to have exactly the same commits the remote branch has. And that's the job that git reset does best.

git fetch --all
git reset --hard origin/master

And at a brazing speed your master branch is ready to the Hotfix.

After we pushed the Hotfix it's time to go back to our branch.

git checkout my-brach

Now we have to roll back our incomplete code to the stage phase, and guess what command we will use? That's right, git reset!

git --soft  head~1

Why --soft instead of --hard? Because the --hard option erases everything that isn't inside the commit reference, in that case head~1 means the commit before the head commit. Our last commit is the chore: incomplete commit and using --soft we will erase this commit from existence but preserve the changes it did.

We just did a safer stack operation, switching between two branches and guaranteed it's integrities.

4 - Develop to a new feature

Everytime you want to start from develop it's the same slow commands.

git checkout develop
git pull
git checkout -B my-branch

Let's speed the things up with git reset --hard

git checkout -B my-feature
git fetch --all
git reset --hard origin/develop

It doesn't matter the branch were you from, that commit will guarantee that you will have your branch my-branch with the same commits as the remote branch develop.

5 - Outro

It's interesting how a such useful command is barely used by the majority of developers. Maybe the reset nature can be scary, but with you know what your doing it's gonna be just peace and tranquility.