How to undo last commit using git reset?
Use git reset with HEAD~1, which means “one commit before the current one”. The option decides what happens to your changes.
Terminal output
$ git reset –soft HEAD~1
$ git status -s
M file.txt
$ git log –oneline
3b28266 (HEAD -> main) First
| Command | Result |
|---|---|
git reset --soft HEAD~1 | Commit is removed, changes stay staged |
git reset HEAD~1 | Commit is removed, changes stay in files, not staged |
git reset --hard HEAD~1 | Commit is removed and the changes are deleted |
Use this only for commits which are not pushed. For pushed commits use git revert. See Git Reset.
Learn more about the similar topics:
| Tutorials |
|---|
| Git Restore |
| Git Commit Amend |
| Git Reset |
| Git Revert |
| Git Stash |
| Git Reflog |