Pick the Right Command
The right command depends on two questions: has the commit been pushed, and do you want to keep its changes? Check first:
git status
git log --oneline -3
If git status says your branch is ahead of its remote branch, the last commit exists only in your clone. Then pick the line that fits your case.
Warning: the --hard line deletes the commit's changes and every uncommitted change in tracked files. Read its section before you run it.
| Case | Command |
|---|---|
| Not pushed, keep staged | git reset --soft HEAD~1 |
| Not pushed, keep unstaged | git reset HEAD~1 |
| Not pushed, drop changes | git reset --hard HEAD~1 |
| Already pushed | git revert HEAD |
| Fix message or add a file | git commit --amend |
Keep the Changes Staged
git reset --soft HEAD~1
HEAD~1 means the commit before the current one. The branch moves back by one commit, and nothing else changes: your files keep their content, and the changes of the removed commit stay staged. git status shows them as ready to commit, so you can commit them again with a better message or after adding more work.
Keep the Changes Unstaged
git reset HEAD~1
Without an option, reset uses --mixed. The commit is removed and its changes stay in your files, but they are no longer staged. This is the form to use when you want to split one commit into several: stage parts of the work with git add -p and commit each part.
Throw the Changes Away
Warning: --hard resets your files as well. The changes of the removed commit disappear from the working tree, and so does every uncommitted change in tracked files. Uncommitted work cannot be recovered afterward. Check git status first and stash anything you want to keep:
git status
git stash
git reset --hard HEAD~1
The removed commit itself can still be found for a while, as shown in the reflog section below. The three reset modes are compared in git reset: soft, mixed, and hard.
The Commit Is Already Pushed
Once others may have pulled the commit, do not remove it. Add a commit that undoes it instead:
git revert HEAD
git push
History stays intact and a normal push is enough. Details, ranges, and merge commits are in Git revert: undo a commit safely. Removing a pushed commit with reset would need a force push, which rewrites the branch for everyone; see how to force push safely before you choose that road.
Only the Message or a Forgotten File
If the commit is right but its message is wrong, or a file is missing, amend it:
git commit --amend -m "Add login form validation"
git add forgotten-file.js
git commit --amend --no-edit
Amending replaces the last commit with a new one. The same rule applies: amend without second thoughts only before you push. More in Git commit --amend.
Undo More Than One Commit
Use a larger number or a commit hash from git log --oneline:
git reset --soft HEAD~3
git reset --soft a1b2c3d
The first line removes the last three commits and keeps all their changes staged together. The second moves the branch back to the commit a1b2c3d.
Get a Commit Back with the Reflog
Git records every position of HEAD in your clone. If you reset too far, list those positions:
git reflog
a1b2c3d HEAD@{0}: reset: moving to HEAD~1
f6e5d4c HEAD@{1}: commit: Add login form
The commit f6e5d4c still exists. The safest way back is a new branch that points to it:
git branch recovered f6e5d4c
The reflog is local to your clone, and old entries expire. By default, entries for commits that are no longer on any branch, like the one you just removed, are kept for 30 days and other entries for 90 days, so recover lost commits soon.