How to undo most recent commit in GIT?

Sundaram Dubey
2 min readJan 8, 2022

I’ll show you the 3 different ways you can undo a commit.

option 1: git reset --hard

Say you have this, where C is your HEAD and (F) is the state of your files.

(F)
A-B-C

master

You want to nuke commit C and never see it again and lose all the changes in locally modified files. You do this:

git reset --hard HEAD~1

The result is:

(F)
A-B

master

Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

option 2: git reset

Ah, but suppose commit C wasn’t a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:

(F)
A-B-C

master

You can do this, leaving off the --hard:

git reset HEAD~1

In this case the result is:

(F)
A-B-C

master

In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven't lost a thing!

option 3: git reset --soft

For the lightest touch, you can even undo your commit but leave your files and your index:

git reset --soft HEAD~1

This not only leaves your files alone, it even leaves your index alone. When you do git status, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had.

Thank You.

--

--

Sundaram Dubey

SDE @Swiggy | Ex-Paytm | HBTU CSE’21 | GSoC ’21 @DIAL | Google CodeIn mentor’20