Reset

Previously we've looked at reverting a change using git revert. However, what revert does is to apply reverse changes as a new commit. Why if we want to delete a commit like it never happened before? This is what git reset is for.

A reset moves the HEAD pointer to a new commit. Hence it changes the commit history. There are three types of resets that you can apply on a target commit:

  • --hard: the most radical, truncating the following commits like never happen. Any changes from the target commit won't be kept.

  • --mixed: the default option. It's equivalent to --hard but then apply all changes (since the target commit) on the working tree.

  • --soft: the most gentle, equivalent to --mixed and add the changes to the staging area.

Only use reset if others' work is not based on these commits.

You can still get all changes back right after a reset. But you need to know the original commit HASH. This is because Git still has the original commit but the HEAD is not pointing to it.

git reset --hard <commit-hash>

If you wait too long and the commit is garbage collected, then you won't be able to restore it.

Last updated