$ run as a normal user# run as root (or with sudo)
The quick answer
| Case | Command |
|---|---|
| Not pushed — undo the commit, keep the changes | git reset --soft HEAD~1 |
| Not pushed — only fix the message or add a file | git commit --amend |
| Already pushed — undo it for everyone | git revert HEAD |
| Something went wrong — find the lost commit | git reflog |
Step by step
- Check where you are — branch, last commits, and if they are pushed:
If the first line of git status -sb ends with [ahead 1], one commit is not pushed yet.
- Not pushed: undo the commit. Choose what happens to the changes:
| Command | The commit | Your changes |
|---|---|---|
git reset --soft HEAD~1 | Gone | Still staged — ready to commit again. |
git reset HEAD~1 (mixed) | Gone | In the files, not staged. |
git reset --hard HEAD~1 | Gone | Deleted — careful. |
- Not pushed: fix the last commit (forgot a file, typo in the message):
git add forgotten-file.py
git commit --amend --no-edit
git commit --amend -m "Add rate limit to the login API"- Commit on the wrong branch (not pushed): move it to a new branch, then remove it from main:
- Already pushed: revert. This makes a new commit that does the opposite — safe for shared branches:
-m 1 is needed to revert a merge commit (keep the first parent = main).
- Lost something?
refloglists every place HEAD was, also after areset --hard:
git reflog -5
a1f8ed1 HEAD@{0}: reset: moving to HEAD~1
7c14206 HEAD@{1}: commit: Add rate limit to the login API
git reset --hard 7c14206- Undo changes that are not committed (in files, or staged):
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
rejected … non-fast-forward after a reset/amend | You rewrote history that was already pushed. | Use git revert instead; only on your own branch: git push --force-with-lease. |
reset --hard deleted work | Uncommitted changes are not in the reflog. | Committed work: git reflog. Never-committed changes are lost — commit or git stash first. |
error: commit … is a merge but no -m option was given | Reverting a merge commit. | git revert -m 1 HASH. |
| Revert of a revert is needed later | You want the change back. | git revert the revert commit. |
| A secret was in the commit | Reset or revert does not remove it from the remote history. | Rotate the secret, then clean the history. |
| Teammates have the old history after your force-push | They pulled before. | Avoid force-push on shared branches; they must reset to the new remote. |
Go further
- Command pages: git.
- Next how-tos: Resolve a Git merge conflict, Remove a secret from Git history, Sign Git commits, Park unfinished work with git stash.
- Learn it: Git — reset, Git — revert, Git — reflog, Git — amend.
- Full guide: Install and set up Git.
1The commit is already pushed to main. Reset or revert?
Revert — it adds a new commit and does not rewrite shared history.
2You ran git reset --hard and lost a commit. How do you get it back?
git reflog shows the old commit hash; git reset --hard HASH (or git branch rescue HASH).