What you can do after this lesson
You will have a calm routine for when something goes wrong, plus tested recipes for the most common slips. Beginners often fear Git because one wrong command seems to erase work. The truth is the opposite: Git is very conservative about committed work. What is genuinely fragile is uncommitted work and shared history. Everything in this lesson was run in practice repositories, with fictional hashes that differ on your machine.
The first minute
When you realise you have made a mistake:
- Stop. Do not run more commands in a panic. A second "fix" on top of the first often makes things harder to read.
- Run
git statusto see the state of the three areas. - Run
git log --oneline --graph --allandgit reflogto see where things are and where they have been. - Decide whether the commits involved have been pushed. Unpushed history is yours to rewrite. Pushed history should be repaired with new commits.
Then pick the recipe below.
Committed on the wrong branch
You made a commit on main that belonged on a feature branch, and have not pushed:
git branch feature-b
git reset --hard HEAD~1
git log --oneline --all --decorateHEAD is now at df95d05 One
2e65799 (feature-b) Meant for a feature branch
df95d05 (HEAD -> main) OneFirst git branch feature-b puts a new label on the mistaken commit, so it is safe. Then git reset --hard HEAD~1 moves main back. The work now lives on feature-b and main is as it was. (--hard is safe here only because your working tree was clean. Check with git status first.) If you have already pushed main, do not reset it: use git revert and put the work on a branch separately.
A merge you regret
You ran git merge feature-b and immediately see it was the wrong thing, before pushing. The reference ORIG_HEAD remembers where your branch was before risky operations such as merge, rebase and reset:
git merge feature-b --no-edit
git reset --hard ORIG_HEAD
git log --oneline --graph --allMerge made by the 'ort' strategy.
b.txt | 1 +
c.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 b.txt
create mode 100644 c.txt
HEAD is now at db5e131 Main work
* 76ba896 More on feature
* 2e65799 Meant for a feature branch
| * db5e131 Main work
|/
* df95d05 OneThe merge commit is gone from main; the branches are as before. If the merge is still in progress because of conflicts, git merge --abort is the right command instead. After a bad rebase, git rebase --abort while it runs, or git reset --hard ORIG_HEAD once it finished.
A deleted branch or a lost commit
You ran git branch -D on the wrong branch, or a hard reset swallowed commits. The reflog knows:
git reflog -3
git branch recovered dffb7e1
git log --oneline recovered -1db5e131 HEAD@{0}: checkout: moving from scratch to main
dffb7e1 HEAD@{1}: commit: Scratch idea
db5e131 HEAD@{2}: checkout: moving from main to scratch
dffb7e1 Scratch ideaFind the commit in the reflog, then label it with a new branch. If you do not remember when it happened or the reflog is long, git reflog with grep helps, and git log -g shows the same information in the log format.
When even the reflog does not help, for example in a fresh clone, ask Git for unreachable commits:
git fsck --lost-founddangling commit dffb7e1bd4e2db91debc2186730536c2f9749bfd
dangling commit 2fbbbf235a644c6b93a7c23ae1dc8a8c70d98d01Each line is a commit that nothing points to. Inspect with git show <hash> and rescue with git branch recovered <hash>. These commits exist only until Git's periodic cleanup removes them, so recover promptly. Our test shows how the output looks; it is not a guarantee about timing.
A commit that is already pushed
If other people may have the commit, do not rewrite history. Undo it with a new commit:
git revert --no-edit HEAD[main 4ecab94] Revert "Main work"
Date: Tue Nov 10 10:00:00 2026 +0000
1 file changed, 1 deletion(-)
delete mode 100644 d.txtThen push normally. For a wrong commit message on pushed work, leave it and write better ones in future.
The wrong message or a forgotten file in the last commit
If the commit is local, git commit --amend fixes the message, and staging a file first then git commit --amend --no-edit adds it, as the reset lesson showed.
Lost uncommitted work
Honesty matters here. If you discarded edits with git restore, git reset --hard or git clean, and never staged or committed them, Git has no copy. Check your editor's local history, backups, or trash; Git cannot help. The best protection is to commit early, even in small "WIP" commits on a branch, and to stash before risky commands. One partial exception: if you had run git add before losing the edits, the staged content was stored in Git's object database and may show up in git fsck --lost-found as a dangling blob, a file's content with no commit, which you can print with git show <hash>.
Stuck in a conflict or a detached HEAD
- Mid-merge and confused:
git merge --abort. - Mid-rebase and confused:
git rebase --abort. - Mid-cherry-pick:
git cherry-pick --abort. - Detached HEAD with work you want:
git switch -c rescue-namecreates a branch at that commit. Without that step, the commits are only in the reflog.
A secret pushed to a remote
If a password, key or token was committed and pushed, treat it as compromised:
- Revoke or rotate it immediately at the service that issued it. This is the only step that actually fixes the exposure.
- Remove it from the current files and add the file name to
.gitignore. - Consider rewriting history to remove it, which is an advanced operation involving specialised tools. The hosting service's documentation describes the options. Rewriting does not undo exposure that already happened, since others may have copied the history.
- Tell the people who need to know, according to your team's policy.
A one-page recovery table
| Mistake | Recovery |
|---|---|
| Edited the wrong file, not committed | git restore file (cannot be undone) |
| Staged the wrong file | git restore --staged file |
| Typo in the last unpushed commit message | git commit --amend -m "..." |
| Committed to the wrong branch, unpushed | git branch new, then git reset --hard HEAD~1 |
| Regretted merge, unpushed | git reset --hard ORIG_HEAD |
| Deleted a branch | find it with git reflog, then git branch name <hash> |
| Bad commit already pushed | git revert <commit> |
| Secret pushed | revoke it first, clean up afterwards |
Common mistakes
- Reaching for
--forceto fix everything. Force pushes rewrite shared history. - Running several experimental commands in a row. Read the reflog before the next step.
- Deleting the repository and re-cloning. You may throw away unpushed work.
- Hard reset with a dirty tree. It destroys uncommitted edits.
- Believing deleting a file removes a leaked secret. It remains in history.
Exercise
- In a scratch repository, commit on
mainby mistake and move the commit to a new branch. - Merge a branch, then undo the merge with
ORIG_HEAD. - Create a branch with one commit, force-delete it, and bring it back using the reflog.
- Write the three commands you would run first in any Git emergency.
Show one possible answer
echo x > mistake.txt && git add . && git commit -m "Wrong place"
git branch fix-it && git reset --hard HEAD~1
git merge fix-it --no-edit
git reset --hard ORIG_HEAD
git branch -D fix-it
git reflog -4
git branch fix-it <hash-from-reflog>The three emergency commands: git status, git log --oneline --graph --all, git reflog.
Key points
- First rule: stop, run git status and git reflog, and do not run more destructive commands; committed work is almost always recoverable.
- A commit on the wrong branch is fixed by creating a branch at it and resetting the original branch back.
- git reset --hard ORIG_HEAD undoes a merge, rebase or pull that you have not shared, and reflog finds work after a deleted branch.
- git fsck --lost-found lists unreachable commits when you no longer know a hash.
- A bad pushed commit is undone with git revert; a leaked secret must be revoked, not just deleted.