To undo the last commit and keep your work, run git reset --soft HEAD~1. The commit disappears from the history and the changes are still there, staged and ready to commit again. If the commit has already been pushed and other people may have it, use git revert HEAD instead, which adds a new commit that cancels the old one.
Run these in the repository folder. HEAD~1 means “the commit before the latest one”; change the number to go back further.
Keep the Changes, Commit Again
# undo the commit, keep the changes staged git reset --soft HEAD~1 # your files are untouched, so check and commit again git status git commit -m "A better message"

This is the one for a wrong message or a forgotten file. If you only need to fix the message or add a file to the last commit, --amend does it in one step:
# change the message of the last commit git commit --amend -m "The corrected message" # add a forgotten file to the last commit, keep the message git add forgotten.txt git commit --amend --no-edit
Keep the Changes, Unstage Them
# undo the commit, keep the changes in your files but not staged git reset HEAD~1
git reset with no option is --mixed. Use it when you want to split one big commit into several: the files are as you left them, nothing is staged, and you pick what goes into each new commit with git add.
Throw the Commit and the Changes Away
# undo the commit AND delete its changes from your files git reset --hard HEAD~1
This is the only one that destroys work. It also wipes any other uncommitted changes to tracked files. Untracked files are left alone. If you run it by mistake, the commit is not gone yet; Git keeps it for a while and git reflog shows where it was:
git reflog
# find the line before the reset, for example HEAD@{1}, then:
git reset --hard HEAD@{1}Already Pushed: Use revert
# add a new commit that undoes the last one git revert HEAD # or without opening an editor for the message git revert --no-edit HEAD
The old commit stays in the history and a new one reverses it, so nobody who already pulled has a problem. Use this on any shared branch.
You can still use reset after a push if the branch is only yours. Git then refuses a normal push, because your branch is behind the remote copy. The safer force is --force-with-lease, which only overwrites the remote if nobody else has pushed to it since you last fetched:
git push --force-with-lease origin main
How It Works
A branch is just a pointer to its latest commit. git reset moves that pointer back, and the three options only differ in what happens to your files: --soft leaves the index and files alone, --mixed resets the index, --hard resets both. git revert never moves the pointer back. It builds a new commit going forward, which is why it is safe on shared history.
Common Questions
What is the difference between –soft and –hard?
Both remove the commit. --soft keeps the changes staged, --hard deletes them from your files. If you are not sure, start with --soft; you can always throw the changes away afterwards.
Can I get a commit back after reset –hard?
Usually, yes. git reflog lists where HEAD has been, and git reset --hard to that entry brings the commit back. It only works for commits that were made; changes you never committed are not recoverable.
How do I undo more than one commit?
Change the number: git reset --soft HEAD~3 undoes the last three commits and leaves their combined changes staged. To undo several pushed commits, git revert each one, newest first.
Related
If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.
Leave a Reply