SkillNavora
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.

Checked against official sources in November 2026. Lessons build on each other.
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

1. Which undo do I need?

SituationCommandSafe after push?
Typo in the last commit messagegit commit --amend -m "..."No
Forgot a file in the last commitgit add file && git commit --amend --no-editNo
Undo the last commit but keep the workgit reset --soft HEAD~1No
Undo a commit that is on GitHubgit revert <commit>Yes
Get back a commit after a bad resetgit reflog, then git reset --hard HEAD@{n}Local fix
Discard all local changesgit 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 definition

3. 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 definition

Revert 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

  1. Revoke and rotate the secret first (create a new key, delete the old one). Assume it is already copied.
  2. Remove it from the code, move it to an environment variable or secret store, and add the file to .gitignore.
  3. If it reached GitHub, clean the history with git filter-repo or the BFG tool, force-push with care, and ask collaborators to re-clone.
  4. Turn on GitHub secret scanning and push protection so pushes containing known secret formats are blocked.

Common mistakes and fixes

MistakeFix
Amending or resetting commits already pushed to a shared branchUse git revert instead.
git reset --hard with uncommitted work you wantedRun git status first; stash if unsure.
Thinking deleting a secret in a new commit is enoughIt stays in history; rotate it and clean history.
git push --force on mainProtect main; if you must overwrite your own branch, use --force-with-lease.

Practice

  1. Make a commit with a typo in the message and fix it with --amend.
  2. Make a commit, reset --soft, then recommit it differently.
  3. 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.

Go further (official sources)

Refer & earn