Sub topic 13
Last resort: git fsck --lost-found
This chapter assumes you can read git reflog output, create a branch from a hash, and know that a branch is a pointer to a commit. Sometimes the reflog has no useful entry. The work may have been committed on a branch whose reflog was deleted along with the branch, or the entry may have expired. The objects can still exist in the repository, and git fsck can find them.
Why unreachable commits can still exist
Git stores commits, trees and file contents (blobs) as objects. A commit is reachable if a branch, tag, HEAD, or another reference leads to it. When nothing points to a commit any more, it is unreachable, or dangling. Git does not delete it right away. Garbage collection removes unreachable objects only after a grace period, which is two weeks by default.
So the reflog is an index of recent positions, and fsck is a scan of everything on disk.
Running git fsck
git fsck --lost-found
# Checking object directories: 100% (256/256), done.
# dangling commit 8a1c3de4b6f0a7d2c9e5f1b3a4d6c8e0f2a4b6c8
# dangling blob 51f2c0a9e3b7d4c6a8f0e2b4d6c8a0e2f4b6d8a0Each line names the object type and its full hash. Look for lines that say dangling commit. The --lost-found option also writes the dangling objects into .git/lost-found/. Commits go in .git/lost-found/commit/ and blobs in .git/lost-found/other/.
Dangling blobs are file contents with no commit or tree pointing to them. They appear when you ran git add on a file and then lost the index state. They have no file names, so they are harder to use.
Note that fsck also reports a commit as dangling when it is only the older tip of an amended or rebased branch. Expect many results in an active repository.
Finding the right commit
Hashes alone tell you nothing, so inspect each candidate. List the dangling commits with their dates and subjects:
git fsck --no-reflogs --lost-found 2>/dev/null \
| awk '/dangling commit/ {print $3}' \
| xargs git log --no-walk --format='%h %ad %s' --date=shortThe --no-reflogs option matters. By default fsck treats commits that a reflog entry still mentions as reachable, so they are not reported. With the option, you also see commits that only the reflog keeps alive. Then pick one and check it:
git show --stat 8a1c3de
git log --oneline -5 8a1c3deWhen you find the right tip, give it a name so it becomes reachable again:
git branch recovered 8a1c3deWhat fsck cannot find
- Changes that were never added or committed. Git never stored them as objects.
- Files you added with
git addand then overwrote. Only the staged blob may remain, and it appears as a dangling blob without a name. - Objects already pruned by garbage collection.
- Work in another clone. The scan covers only this repository.
Exercise
You deleted a branch last month, git reflog has no entry for its commits, and you did not note the hash. What command lists commits that no reference points to, and what do you do once you find the right one?
Show answer
Run git fsck --lost-found (add --no-reflogs to include commits that only the reflog keeps alive) and look for dangling commit lines. Inspect candidates with git show --stat <hash>, then run git branch <name> <hash> to make the commit reachable again. If garbage collection has already pruned the objects, they cannot be recovered.
Exercise
Why does git fsck report many dangling commits in a repository where you have only amended and rebased?
Show answer
Amending and rebasing create new commits and leave the old ones with no branch pointing to them. These old commits are unreachable, so fsck lists them as dangling until garbage collection removes them.