Git & GitHub Complete Guide › Lesson 6 of 8 · about 20 minutes
Lesson 6
Undo anything safely: amend, reset, revert, reflog and leaked secrets
Fix the last commit message, take back a commit you have not shared, undo a commit that is already on GitHub, recover "lost" work with the reflog, and what to do when a password is committed.
In plain words. Git almost never loses work. Before you share commits, you can rewrite them freely. After you share them, you add a new commit that undoes the old one, so nobody's copy breaks.
Key ideas
git commit --amendReplaces the last commit (message or content). Only for commits not yet pushed.
git reset --soft / --mixed / --hardMoves the branch back; soft keeps changes staged, mixed keeps them in files, hard discards them.
git revertCreates a new commit that undoes an earlier one. Safe for shared history.
git reflogGit's diary of where HEAD has been; finds commits after a bad reset.
Force pushOverwrites the remote branch. Use --force-with-lease, and never on shared branches.
How it works
Pushed already?→No → amend / reset→Yes → revert→Lost something? → reflog→Secret leaked → rotate first
1. Which undo do I need?
| Situation | Command | Safe after push? |
|---|---|---|
| Typo in the last commit message | git commit --amend -m "..." | No |
| Forgot a file in the last commit | git add file && git commit --amend --no-edit | No |
| Undo the last commit but keep the work | git reset --soft HEAD~1 | No |
| Undo a commit that is on GitHub | git revert <commit> | Yes |
| Get back a commit after a bad reset | git reflog, then git reset --hard HEAD@{n} | Local fix |
| Discard all local changes | git restore . (or git reset --hard) | Local only; cannot be undone |
2. Fix the last commit message
Real terminal output (captured when this guide was built)
$ echo 'tag = a name for one commit' >> glossary.md && git commit -q -am "Add tag defnition"
$ git log --oneline -1
dbb1203 Add tag defnition
$ git commit --amend -q -m "Add tag definition"
$ git log --oneline -1
7800ea1 Add tag definition3. Reset: soft, mixed and hard
Real terminal output (captured when this guide was built)
$ echo 'temp line' >> quiz.md && git commit -q -am "Temporary commit"
$ git log --oneline -2
e528718 Temporary commit
7800ea1 Add tag definition
$ git reset --soft HEAD~1
$ git status --short
M quiz.md
$ git reset --mixed HEAD
Unstaged changes after reset:
M quiz.md
$ git status --short
M quiz.md
$ git restore quiz.md && git status --short && echo clean
clean- --soft: the commit is gone, its changes stay staged.
- --mixed (default): changes stay in your files but are unstaged.
- --hard: changes are discarded. Check git status and git stash first.
4. Undo a pushed commit with revert
Real terminal output (captured when this guide was built)
$ echo 'wrong fact: git is a GitHub product' >> glossary.md && git commit -q -am "Add a wrong fact" && git push -q
$ git revert --no-edit HEAD
[main 1ee8249] Revert "Add a wrong fact"
Date: Mon Nov 2 10:37:00 2026 +0530
1 file changed, 1 deletion(-)
$ git log --oneline -3
1ee8249 Revert "Add a wrong fact"
d586c73 Add a wrong fact
7800ea1 Add tag definitionRevert adds a new commit, so teammates simply pull it. History stays honest: the mistake and its fix are both visible.
5. Recover "lost" commits with reflog
Real terminal output (captured when this guide was built)
$ git reset --hard HEAD~2
HEAD is now at 7800ea1 Add tag definition
$ git log --oneline -1
7800ea1 Add tag definition
$ git reflog -4
7800ea1 HEAD@{0}: reset: moving to HEAD~2
1ee8249 HEAD@{1}: revert: Revert "Add a wrong fact"
d586c73 HEAD@{2}: commit: Add a wrong fact
7800ea1 HEAD@{3}: reset: moving to HEAD
$ git reset --hard HEAD@{1}
HEAD is now at 1ee8249 Revert "Add a wrong fact"
$ git log --oneline -1
1ee8249 Revert "Add a wrong fact"Good to know. The reflog is local and entries expire after weeks or months. Recover soon after the mistake.
6. You committed a password or API key
- Revoke and rotate the secret first (create a new key, delete the old one). Assume it is already copied.
- Remove it from the code, move it to an environment variable or secret store, and add the file to .gitignore.
- If it reached GitHub, clean the history with
git filter-repoor the BFG tool, force-push with care, and ask collaborators to re-clone. - Turn on GitHub secret scanning and push protection so pushes containing known secret formats are blocked.
Common mistakes and fixes
| Mistake | Fix |
|---|---|
| Amending or resetting commits already pushed to a shared branch | Use git revert instead. |
| git reset --hard with uncommitted work you wanted | Run git status first; stash if unsure. |
| Thinking deleting a secret in a new commit is enough | It stays in history; rotate it and clean history. |
| git push --force on main | Protect main; if you must overwrite your own branch, use --force-with-lease. |
Practice
- Make a commit with a typo in the message and fix it with --amend.
- Make a commit, reset --soft, then recommit it differently.
- Do a reset --hard on purpose in a test repo and recover with reflog.
Quick check
Which undo is safe for commits already on GitHub?
git revert, because it adds a new commit instead of rewriting history.
What is the difference between reset --soft and --hard?
--soft keeps the changes staged; --hard discards them.
What is the first step after committing a secret?
Revoke and rotate the secret, because it may already be copied.