If you made a commit to your git local repository and you want to undo it, you should make use of the git reset command.
There are three flags or options you may use depending upon what your use case is.
We have done two commits:$ touch File1.txt File2.txt
$ git add File1.txt
$ git commit -m "Commit 1"
$ git add File2.txt
$ git commit -m "Commit 2"
$ git log --oneline
341ab47 (HEAD -> main) Commit 2
3291fc9 Commit 1
1. Undo the Last Commit completely with a hard reset
If you do not want the files that were committed in the last commit to your local repository make use of the --hard option.
$ git reset --hard HEAD^1
HEAD is now at 3291fc9 Commit 1
Note this case you will lose the files that were committed as the modifications you made to the existing file.
$ ls
File1.txt
$ git status
On branch main
nothing to commit, working tree clean
2. Undo Last Commit with a soft reset
If you want to undo your last commit, but want to retain the files that were committed in the last commit make use of the --soft flag.
$ git reset --soft HEAD^1
$ ls
File1.txt File2.txt
$ git status
On branch main
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: File2.txt
Note that changes you made or the new files added will remain in the staged area.
3. Undo Last Commit with a mixed reset
If you want to undo your last commit, but want to retain the files that were committed in the last commit in the working area make use of the --mixed flag.
$ git reset --mixed HEAD^1
$ ls
File1.txt File2.txt
$ git status
On branch main
Untracked files:
(use "git add ..." to include in what will be committed)
File2.txt
nothing was added to commit but untracked files are present (use "git add" to track)
As you can see the new file that was committed is now moved to the working area and is untracked.
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Change the default git branch name from master to main
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- How to Install Git on Windows
- Setup Git + Visual Studio Code Tutorial
- How to undo last Git Commit on Local Repository?
- Install GitHub Command Line Tool on Mac
- How to Create a Git Branch in 6 Different ways
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows
- Git Config Command - A Deep Dive
- Step-by-Step: How to delete a git branch from local as well as remote origin
- How to Add Git Bash Option to Windows Terminal List
- Command to Clone Repository Using Git Bash
- How to remove or unstage a file from git staged area
- Get the file location of git config values
- GitHub: How to Search Code
- How to check your installed version of Git
- Fix: error: src refspec master does not match any failed to push some refs to Git
- Git: Step-by-Step - How to Push Local Brach to GitHub
- How to Merge Branch into Master Branch
- fix fatal: --local can only be used inside a git repository error
- Fix: Git Pull Error: unable to resolve reference refs/remotes/origin/master: reference broken
- Change the default diff or commit editor for git
- Git Fix: fatal: refusing to merge unrelated histories Error
- Android AlertDialog with 3 buttons example - Android
- How to get Mac Computer Name using Terminal - MacOS
- How to Get File Size using Python in Bytes/KB/MB or GB - Python
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure - Java
- Fix: SharePoint Online Tasks App Missing - SharePoint
- Check SSH/OpenSSH version Command - Linux
- Convert String Date to Date Object in Java - Java
- 5 ways to pop out a Chat in Microsoft Teams - Teams