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.
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).
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.
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).
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^
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).
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.
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
- What is Git and why it is used?
- How to install Git on Windows & Mac?
- How to use Git with Visual Studio Code?
- How to use Git and GitHub?
- What is the difference between Git and GitHub?
- How to connect local Git to a remote server?
- How to change Git remote origin URL?
- How to create a Git repository on GitHub?
- Git add all (stage all changes)
- How to undo the git add -A command?
- How to undo the last Git commit?
- How to change the git commit message?
- How to remove or unstage a file from Git commit?
- How to git push after rebase?
- Git switch branches: How to checkout the git branch?
- How to merge a git branch to master?
- Git branch: create, checkout, merge, list, command, delete & push
- How to clone a Git repository?
- How to git pull to override the local project?
- How to remove file from Git?
- When should you use git rebase?
- Git commands & explanation (downloadable cheatsheet included)
- How to create GitHub Pages?
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.