Git Emergency
I pulled and my code disappeared
Severity Serious
What just happened
Most often you're on another branch than you think, a merge or rebase changed files, or a teammate deleted code that is now merged in. Your commits are still in the reflog.
Potential risks
- Panic commands (reset --hard, clean) are what really lose work.
Before you run anything
- Don't run anything destructive. Look first.
- Golden rule: before any destructive command, create a safety branch with git branch backup/before-fix โ it costs nothing.
Recovery steps
- 1
Where am I?
Check the branch and whether a merge/rebase is in progress.
Safe$ git status$ git branch --show-current - 2
Find your state before the pull
ORIG_HEAD and the reflog show where you were.
Safe$ git log -1 ORIG_HEAD$ git reflog -n 15 - 3
Who removed the code?
Shows the commits that changed that file.
Safe$ git log -p --follow -- path/to/file - 4
Go back to before the pull (not pushed)
Only if the pull itself is the problem and you have nothing uncommitted.
Destructive$ git reset --hard ORIG_HEAD
These procedures are provided for information only. Read each command and adapt it to your situation before running it. Disclaimer
Other disasters
๐ช I deleted my branch๐จ I pushed directly to main๐ฅ I ran git reset --hard๐งจ I overwrote a colleague's commits๐ I pushed my .env file to GitHub๐ I made a bad merge๐ป I lost uncommitted changes๐ฏ I committed on the wrong branch๐ I'm in โdetached HEADโ stateโ๏ธ I'm stuck in a merge conflict๐ซ My push is rejected (non-fast-forward)๐ My rebase went wrong๐ GitHub rejects a file over 100 MB๐ฅธ I committed with the wrong name or email๐๏ธ I deleted a file and committed itโช I want to undo my last commitโ๏ธ I amended a commit that was already pushed๐งน Too many messy commits to clean up๐ฎ I pushed to the wrong branch๐งญ I rebased onto the wrong branch๐ I need back the changes I reverted๐ฆ git stash pop gave me a conflict๐ I committed a file that should be ignored๐ Git doesn't see my file rename (upper/lower case)๐งจ I ran git clean -fd by mistake๐ฉป My repository is corrupted๐ I forgot to pull before pushing๐ฏ Duplicated lines of code after a mergeโ I added a file I didn't want to (git add)โ๏ธ I made a typo in a commit messageโ๏ธ My commit does too many things๐งบ I lost a stash (drop, clear or pop gone wrong)๐ I cherry-picked the wrong commitโ๏ธ I deleted a branch on GitHub / GitLab๐ฅ I pushed with the wrong GitHub account๐ The repository was renamed or moved๐ท๏ธ I tagged the wrong commit๐ Git says every file is modified, but I changed nothing๐ฆ I committed node_modules (or a build folder)๐ I ran git init in the wrong folder๐งจ My pull request has conflictsโณ My pull request is behind main๐ I have to apply review changes to my PR๐งณ My PR shows commits that aren't mine๐ฏ I opened my PR against the wrong branch๐จ A merged PR broke main๐ช I closed my PR (or deleted its branch) by mistake๐ด The checks of my PR are failing๐ My PR is too big to review๐ด My fork is behind the original repository