TExcelTAD
Home › Git › Git Recovery

Git Recovery

Git rarely loses anything that was committed. This lesson is a rescue handbook: for each common accident, the commands that get your work back. Keep it bookmarked.

Accident Rescue
Deleted or broke a file, not committed git restore <file>
Deleted a file and committed that git restore --source=<commit>~1 <file>
Reset/rebased away commits git reflog → git reset --hard HEAD@{n}
Deleted a branch git reflog / the hash from the delete message → git branch <name> <hash>
Dropped a stash git fsck --unreachable → git stash apply <hash>
Bad merge just made git reset --hard ORIG_HEAD (unpushed) or git revert -m 1 (pushed)
Committed on the wrong branch git branch right-branch + git reset --hard HEAD~1

Restoring deleted files

bash · try it yourself
mkdir site && cd site && git init -q
echo "<h1>Home</h1>" > index.html && echo "<h1>About</h1>" > about.html
git add . && git commit -q -m "Add pages"
rm index.html
echo "garbage" > about.html
git status -s
git restore index.html about.html
git status -s
echo "(clean again)"
echo "--- deleted in a commit:"
git rm -q about.html && git commit -q -m "Remove about page"
git log --oneline --diff-filter=D -- about.html
git restore --source=HEAD~1 about.html
cat about.html
 M about.html
 D index.html
(clean again)
--- deleted in a commit:
559cec2 Remove about page
<h1>About</h1>

git log --diff-filter=D -- <path> finds the commit that deleted a file; its parent (~1) still has it.

Recovering a deleted branch

When you delete a branch, Git prints the hash it pointed to. Recreate the branch from that hash (or find it in git reflog):

bash · try it yourself
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Home"
git switch -q -c redesign
echo "new look" > theme.css && git add . && git commit -q -m "New theme"
git switch -q main
git branch -D redesign
git reflog | grep "redesign\|New theme"
git branch redesign HEAD@{1}
git log --oneline redesign
Deleted branch redesign (was 4997230).
1ea0acf HEAD@{0}: checkout: moving from redesign to main
4997230 HEAD@{1}: commit: New theme
1ea0acf HEAD@{2}: checkout: moving from main to redesign
4997230 New theme
1ea0acf Home

Committed on the wrong branch

You made two commits on main that should have been on a feature branch. Create the branch where you are (it keeps the commits), then move main back:

bash · try it yourself
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Home"
echo "shop" > shop.html && git add . && git commit -q -m "Start shop page"
echo "cart" >> shop.html && git commit -q -am "Add cart to shop"
git branch feature/shop
git reset -q --hard HEAD~2
git switch -q feature/shop
git log --oneline --graph --all
* 3658b43 Add cart to shop
* fe456a8 Start shop page
* 1ea0acf Home

(If the commits were already pushed to main, revert them there instead.)

Recovering a dropped stash

A stash is a commit. When you drop it, it becomes unreachable, but stays in the object database until garbage collection. git fsck finds unreachable commits; stash commits are recognisable by their "WIP on"/"On" messages:

bash · try it yourself
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Home"
echo "half-finished footer" >> index.html
git stash push -q -m "footer work"
git stash drop -q
git stash list
echo "(stash list is empty)"
for c in $(git fsck --no-reflogs --unreachable 2>/dev/null | awk '/commit/ {print $3}'); do
  git show -s --format="%h %s" "$c"
done | grep "footer work"
git stash apply -q "$(git fsck --no-reflogs --unreachable 2>/dev/null | awk '/commit/ {print $3}' |
  while read c; do git show -s --format='%H %s' "$c"; done | awk '/footer work/ {print $1}')"
cat index.html
(stash list is empty)
c7c17b5 On main: footer work
home
half-finished footer

Undoing a merge you just made

ORIG_HEAD points to where the branch was before the merge, so an unpushed merge is undone with one reset:

bash · try it yourself
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Home"
git switch -q -c experiment && echo "x" > x.txt && git add . && git commit -q -m "Experiment"
git switch -q main && echo "footer" >> index.html && git commit -q -am "Footer"
git merge -q --no-edit experiment
git log --oneline --graph
git reset -q --hard ORIG_HEAD
echo "--- after undo:"
git log --oneline --graph
*   a7f475e Merge branch 'experiment'
|\
| * ea65000 Experiment
* | a4b1170 Footer
|/
* 1ea0acf Home
--- after undo:
* a4b1170 Footer
* 1ea0acf Home

Prevention beats recovery

  • Commit early, even as "WIP" on a branch: committed work is recoverable, uncommitted work often isn't.
  • Push your branches regularly: the server is a second copy.
  • Before reset --hard, clean -fd or checkout -- ., run git status and consider git stash.
  • git branch backup-before-rebase costs nothing and makes any experiment reversible.

Test yourself

You deleted config.js in a commit three commits ago. How do you get it back?

  1. git revert config.js
  2. Find the deleting commit (git log --diff-filter=D -- config.js) and git restore --source=<that commit>~1 config.js
  3. git stash pop
  4. It's gone forever

You made two commits on main that belong on a new branch, and haven't pushed. What is the fix?

  1. git revert both commits
  2. git branch new-branch, then git reset --hard HEAD~2 on main
  3. git push --force
  4. git stash

What does ORIG_HEAD point to after a merge?

  1. The merged branch
  2. Where your branch was before the merge
  3. origin/HEAD
  4. The first commit