Git Recovery: Getting Lost Work Back

Updated · 11 min read
hardGit 2.53gitrecoveryfsckdangling-commitgcinterview

Recovering lost work in Git means finding objects (commits, or file contents) that still exist in the repository but that nothing points to anymore, and giving them a name again. Git rarely deletes anything at once. A deleted branch, a dropped stash, an amended commit or a hard reset only removes a name. The data stays in .git/objects until garbage collection removes it, usually weeks later. The tools are the reflog (see "Git Reflog") and git fsck, which lists objects that no reference reaches.

A library that removes a book from its catalog doesn't burn the book at once. It goes to a storeroom until the next clearance. If you ask soon enough, and know what to look for, it can be put back on the shelf.

Reachable and Unreachable

TermMeaning
Reachable objectCan be found by starting at a branch, tag, HEAD, the stash or a reflog entry, and following parents and trees. It is safe.
Unreachable objectNothing points to it anymore. It still exists, until garbage collection removes it.
Dangling objectAn unreachable object that isn't even part of another unreachable object: the "top" of what was lost. git fsck reports these.
Reachable commits, and the ones Git will clean up

The Starting Point

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git switch --quiet -c feature/discount
echo "Sibling discount: 10%" > discount.txt
git add discount.txt
git commit --quiet -m "Add the sibling discount"
echo "Staff discount: 20%" >> discount.txt
git commit --quiet -am "Add the staff discount"
git switch --quiet main
git log --oneline --all
Output
6ffe219 (feature/discount) Add the staff discount
16b0d3a Add the sibling discount
6b651a0 (HEAD -> main) Add the term fee

Recovery 1: A Deleted Branch

Navika deletes the discount branch, thinking it was merged. It wasn't, so -d would have refused, but she used -D:

Terminal
git branch -D feature/discount
Output
Deleted branch feature/discount (was 6ffe219).

The quickest way back is the ID in that message: git branch feature/discount <id>. If the message is gone (another terminal, a GUI, days later), the HEAD reflog still remembers the branch's last commit, because she was on it:

Terminal
git reflog -3
Output
6b651a0 (HEAD -> main) HEAD@{0}: checkout: moving from feature/discount to main
6ffe219 HEAD@{1}: commit: Add the staff discount
16b0d3a HEAD@{2}: commit: Add the sibling discount

HEAD@{1} is the last commit made on feature/discount, right before she switched to main. A new branch there restores it:

Terminal
git branch feature/discount HEAD@{1}
git log --oneline --all
Output
6ffe219 (feature/discount) Add the staff discount
16b0d3a Add the sibling discount
6b651a0 (HEAD -> main) Add the term fee

Explanation: deleting a branch removes its name and its own reflog, but not its commits, and HEAD's reflog still lists the commits she worked on. If you never checked the branch out (for example, a teammate's branch you only fetched), HEAD's reflog has no trace of it, and git fsck (below) is the way to find it.

Recovery 2: A Dropped Stash

Navika stashes some work, then drops it by mistake:

Terminal
echo 'Hostel fee: $9,000' >> fees.txt
git stash push --quiet -m "Hostel fee draft"
git stash drop
Output
Dropped refs/stash@{0} (69a4ea31526fe4676d14483bfb01b6f02c97fea5)

Again, the message shows the ID, and git stash apply <id> would bring it back. Without it, stashes are harder to find than branches: the stash's own reflog entry is gone. git fsck lists the objects that nothing points to:

Terminal
git fsck --no-reflogs | grep "dangling commit"
Output
dangling commit 69a4ea31526fe4676d14483bfb01b6f02c97fea5

--no-reflogs makes fsck ignore the reflog, so that commits reachable only from it count as dangling too. A dangling commit created by git stash has a recognizable message. Check it, then apply it:

Terminal
id=$(git fsck --no-reflogs | awk '/dangling commit/ {print $3}')
git log -1 --format="%h %s" "$id"
git stash apply --quiet "$id"
git status -s
Output
69a4ea3 On main: Hostel fee draft
 M fees.txt

Explanation: the dangling commit's message, On main: Hostel fee draft, identifies the stash. git stash apply accepts any stash commit by ID, so the work is back in the folder. With several dangling commits, look at each one with git log -1 or git show --stat before applying.

Recovery 3: Staged Files Lost to reset --hard

Work that was staged but never committed isn't a commit, so the reflog can't help. But git add stored the file's content as a blob object, and that blob survives a reset:

Terminal
git add fees.txt
echo 'Exam fee: $800' > exam.txt
git add exam.txt
git reset --quiet --hard
ls
git fsck --lost-found
Output
fees.txt
dangling commit 69a4ea31526fe4676d14483bfb01b6f02c97fea5
dangling blob 4f1e154b40b5da0aef777292439fd309af7b7cf3

git reset --hard removed exam.txt from the folder and the index. git fsck --lost-found found its blob, dangling, and wrote a copy to .git/lost-found/other/, named by its ID. (The dangling commit is the stash from Recovery 2: applying a stash doesn't give it a name again. The staged fees.txt isn't listed, because the same content is already inside that stash commit.) Navika finds the one she wants by its content:

Terminal
grep -l "Exam fee" .git/lost-found/other/*
cat $(grep -l "Exam fee" .git/lost-found/other/*)
Output
.git/lost-found/other/4f1e154b40b5da0aef777292439fd309af7b7cf3
Exam fee: $800

The file name is lost (a blob is only content), but the content is back, and she can copy it to exam.txt again.

What Can't Be Recovered

Navika edits fees.txt without staging, then resets:

Terminal
echo 'Sports fee: $700' >> fees.txt
git reset --quiet --hard
git fsck --lost-found > /dev/null
grep -l "Sports" .git/lost-found/other/* || echo "No trace of the unstaged edit"
Output
No trace of the unstaged edit

Nothing. The edit was never given to Git, so Git never had a copy. That is the best argument for committing often, even with "WIP" messages that you clean up later.

How Long You Have

Unreachable objects are deleted by garbage collection (git gc), which Git runs automatically from time to time. Two settings decide how long lost work survives:

SettingDefaultMeaning
gc.reflogExpire90 daysReflog entries older than this are removed
gc.reflogExpireUnreachable30 daysReflog entries pointing to commits no branch reaches are removed sooner
gc.pruneExpire2 weeksUnreachable objects older than this are deleted by git gc

So a commit lost from a branch is usually safe for about a month through the reflog, and loose objects at least two weeks after they became unreachable. To show what an early cleanup does, Navika creates a commit, removes it from the branch, and forces an immediate cleanup:

Terminal
echo "Draft" > draft.txt
git add draft.txt
git commit --quiet -m "A draft to lose"
lost=$(git rev-parse HEAD)
git reset --quiet --hard HEAD~1
git cat-file -t "$lost"
git reflog expire --expire-unreachable=now --all
git gc --quiet --prune=now
git cat-file -t "$lost"
Output
commit
fatal: git cat-file: could not get object info

Explanation: before the cleanup, git cat-file -t still finds the commit (commit). After expiring the reflog now and pruning now, the commit is really gone. Never run these two commands while you are trying to recover something.

Common Mistakes

Waiting weeks before recovering. Reflog entries expire and garbage collection runs. Recover as soon as you notice.

Running git gc --prune=now "to clean up" during a recovery. It deletes exactly the unreachable objects you are looking for.

Recovering on a fresh clone. The lost objects exist only in the repository where they were lost. Work there.

Applying the wrong dangling commit. Check each with git log -1 or git show --stat first.

Expecting Git to have unstaged edits. It never saw them. Commit or stash often.

Under the Hood

Every object in .git/objects stays until git gc decides it is both unreachable and old enough. Reachability is checked by walking from every reference: branches, tags, HEAD, the stash, ORIG_HEAD and similar, and every reflog entry. That last part is what protects lost commits for weeks: a reflog entry is a reference too, until it expires. git fsck performs the same walk, and reports what it couldn't reach. --no-reflogs leaves the reflog out of the walk, which is why it finds commits that the reflog still protects. --lost-found writes the dangling objects to .git/lost-found/commit/ and .git/lost-found/other/, so you can read them as ordinary files.

Interview Corner

How do you recover a branch deleted with git branch -D?

Use the commit ID printed by the delete command, or find the branch's last commit in git reflog (it is there if you had it checked out), then git branch <name> <commit>. If it isn't in the reflog, look for dangling commits with git fsck.

How do you recover a dropped stash?

If the "Dropped ... ()" message is still visible, git stash apply <id>. Otherwise, find dangling commits with git fsck --no-reflogs, identify the stash by its "On : ..." message, and git stash apply <id>.

Can you recover files lost with git reset --hard?

Committed work, yes, through the reflog. Staged but uncommitted content often, as dangling blobs found by git fsck --lost-found. Changes that were never staged or committed, no.

What is a dangling commit?

A commit that no branch, tag or other reference points to, and that isn't the parent of another unreachable commit. It still exists until garbage collection removes it, and git fsck reports it.

How long does Git keep unreachable commits?

Reflog entries keep them for 90 days by default, or 30 days if unreachable from any branch (gc.reflogExpire, gc.reflogExpireUnreachable). After that, git gc deletes unreachable objects older than about two weeks (gc.pruneExpire).

Practice

Challenge: two branches gone. Navika deletes both feature/discount and a new branch feature/sports, whose commit was never checked out, so HEAD's reflog doesn't know it. Here, git commit-tree creates such a commit without touching any checked-out branch. Find both lost branch tips.

What does Git print? Before you open the solution, decide which tool can find a commit that was never in HEAD's reflog.

Show solution
Terminal
tree=$(git rev-parse HEAD^{tree})
sports=$(echo "Add the sports fee" | git commit-tree "$tree" -p HEAD)
git branch feature/sports "$sports"
git branch -D --quiet feature/discount feature/sports
git fsck --no-reflogs | grep "dangling commit" | while read -r _ _ id; do git log -1 --format="%h %s" "$id"; done
Output
6ddcc54 Add the sports fee
6ffe219 Add the staff discount

HEAD's reflog has no trace of feature/sports, but git fsck --no-reflogs finds every commit that nothing points to: the tip of feature/discount and the sports commit. Their messages identify them, and git branch <name> <id> would restore each one. (git commit-tree is a low-level command that creates a commit from a tree and a parent. It appears here only to create a commit that was never on any checked-out branch.)

Checkpoint. In a practice repository, delete a branch with unmerged work and run git reset --hard HEAD~2 on another branch. Recover both. Then stage a file, reset hard, and get its content back with git fsck --lost-found.

Quick check: Recovery

4 questions
1What does git branch -D delete?
2Which command lists objects that nothing points to?
3Which lost work can Git NOT recover?
4What deletes unreachable objects for good?

Key Takeaways

Quick reference

CommandIt is used to
git reflogFind recent positions of HEAD
git branch <name> <commit>Restore a branch
git stash apply <commit>Restore a dropped stash
git fsck --no-reflogsList dangling commits
git fsck --lost-foundSave dangling objects to .git/lost-found/
git cat-file -t <id>Check whether an object still exists

Was this tutorial helpful?

Lesson 65 of 106