How to undo the last git commit

There are a couple of options for undoing the git commit. Also, there are two scenarios where you may need to undo the last git commit. Such as undo commit that you did not push yet (locally) and the last commit that you already pushed to the remote origin.

I will show you all these options.

Undoing git commit that has not been pushed yet

Type the following command and hit enter to undo the last commit:

git reset HEAD~1

The above command will reset the branch to the previous commit. It will also keep the changes you made last time.

undoing last git commit locally

From there, you can modify & recommit the changes. For example, I removed the commit “commit number 123” and recommit with “Added new line after undoing the last commit.” After pushing it to the remote origin (GitHub Repository), I don’t see the deleted commit and see the revised commit (as you see in the screenshot below).

undo commit and recommit that I pushed to the remote origin (GitHub Repository)

This is how to do it.

Undo the last commit and go back to the previous commit with discard changes

If you need to discard all the changes along with undoing the last git commit, use the following command:

git reset --hard HEAD~1

This will perform a hard reset and take the branch to the last commit as you see in the screenshot below.

performed a hard reset that went back to the last commit

That’s it! Learn more git commands.

If you want to see the list of recent changes, use git reflog. It will show you the log of the recent changes (as you see below).

list of recent changes in commit, git reflog

It’s not required but good to know this command. However, if you performed the git reflog command, it will take you to a prompt. To skip the git reflog prompt, hit the “Esc” key and then “:q” (colon, letter ‘q’). This will exit the prompt and make your command line (terminal) normal.

Undo the git commit that has been pushed to the remote origin

To remove or undo the last git commit that you already pushed to the remote origin, type the following command and hit enter.

git reset --soft HEAD^

git reset --soft HEAD^, remove commit that has been pushed to the remote origin

To demonstrate the purpose, I have the following commit that I already pushed to my GitHub repository (as you see in the screenshot below). Also, notice the total number of commits (21).

A commit that I already pushed to the GitHub repository and that I want to remove now

After I reset the commit and push it to the remote origin, the previous commit has been replaced with this latest one. See the latest screenshot below.

Replaced the last commit with new one in the remote origin

The previous commit has been replaced with the latest one. And if you notice, you’ll see the total number of the commits (21) have not been changed.

Learn more about Git

Conclusion

In this post, I showed you how to undo a git commit in a few different situations. It’s a bit hassle but this is how to do it.