Last Updated: July 11, 2019
· Christoffer Niska

Undoing an accidental commit in a git repository

Have you ever commit something by mistake into a git repository that? Fear not, here's what you need to know to undo that commit.

If you haven't pushed you can open the terminal and run:

git reset --soft HEAD~1

HEAD~1 is a shorthand for the change set previous to HEAD and the --soft flag preserves the file changes and moves them to your working copy.

NOTE! If you already pushed the commit, you cannot use git reset but you can run git revert instead:

git revert HEAD

This will create a new commit with the reverse changes for the change set previous to HEAD.