3 · Getting lost work back, and when it really is gone
In one sentence: “lost” work stays in the store until a branch, a reflog line and a two-week grace period have all stopped protecting it and
git gcruns; until thengit reflogorgit fsckfinds it.
The problem it solves
Four ordinary mistakes at work:
| You ran | It looks like |
|---|---|
git reset --hard to the wrong commit | two days of commits gone from git log |
git branch -D on a branch that wasn’t merged | the branch and its commits gone |
git stash drop on the wrong stash | the stash gone from git stash list |
git reset --hard after git add, before any commit | the new file gone from your folder |
These commands move or delete names, never objects (lessons 1 and 2). So: what still points at the work?
The idea: what still points at it
An object lasts as long as something protects it. Four levels:
| What still points at it | Example | Find it with | Safe for |
|---|---|---|---|
| a branch or tag | everyday commits | git log | as long as the ref exists |
| only a reflog line | after reset --hard, branch -D | git reflog | 30 days (90 if the ref’s current tip still reaches it; for HEAD’s reflog: any branch or tag) |
| nothing | a dropped stash, a file git added then reset away, an expired reflog line | git fsck | until a git gc finds its file older than 2 weeks |
| it never was an object | edits you never git added | nothing | already gone |
The 90 and 30 days are gc.reflogExpire and gc.reflogExpireUnreachable, the 2 weeks is gc.pruneExpire
(git-gc). Unreachable means no ref and no reflog line leads to the
object, through any chain of parents or trees. Nothing is deleted until git gc runs. The 2 weeks count from
the age of the object’s file or pack (Go deeper), not from the reflog line’s expiry.
Finding each one
1. A reset (or a rebase, or an amend) that went wrong. The commits are held by the reflog, so git reflog
lists them, as lesson 2 showed. Notice what git fsck --unreachable prints here:
git reset --hard HEAD~2
git fsck --unreachable
git fsck --unreachable --no-reflogs
HEAD is now at e7488a1 one
unreachable blob 814f4a422927b82f5f8a43f8fab6d3839e3983f2
unreachable tree 6218aaa5fc1a58f5b32cbee55bc0cb0954022787
unreachable blob 4cb29ea38f70d7c61b2a3a25b02e3bdf44905402
unreachable tree 725322682524f6bb2c5ddf2d4c2bcad4adf95c6b
unreachable commit 3411c6e2fdbea8e3d9ecc07e7e8a0701e9f62a28
unreachable commit 785785b29a5da9a7dc84990308146bd094a6ff73
The first fsck prints nothing: a reflog line counts as pointing at a commit. Only --no-reflogs shows
the 6 objects of three and two (2 commits, 2 trees, 2 blobs). git reset --hard HEAD@{1} brings them
back.
2. A deleted branch. -d refuses an unmerged branch; -D deletes it and prints its id:
error: The branch 'feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature'.
Deleted branch feature (was 24b6130).
The branch’s own reflog file is deleted with it (.git/logs/refs/heads now holds only main). HEAD’s reflog
isn’t, and it recorded every commit you made while on the branch:
e7488a1 HEAD@{0}: checkout: moving from feature to main
24b6130 HEAD@{1}: commit: f2
8583618 HEAD@{2}: commit: f1
HEAD@{1}, the last commit made on the branch, is its tip. Recreate the branch there:
git branch feature HEAD@{1}
git log --oneline feature
24b6130 f2
8583618 f1
e7488a1 one
3. A dropped stash. A stash is a commit, and git stash list is the reflog of one ref, refs/stash. Drop
removes the reflog line, so nothing points at the stash any more. git stash drop prints the id; if it scrolled
away, fsck finds it, this time without --no-reflogs:
git fsck --unreachable | grep commit
unreachable commit 0fd67c504d8ba74680d7e3ac0837790afad2557e
unreachable commit 5622c56b73c6810a8d0e355ebc0a0808e14c15b2
The stash is the one git show --stat calls Merge: with WIP on ... (the other is its staged half). Applying it puts both the edit and the staged new file back (apply also prints a full git status, left out here):
git stash apply 0fd67c5
git status --short
M a.txt
A b.txt
4. A file you git added, then reset away. git add wrote a blob (lesson 1). No commit and no tree points at
it, so it’s dangling: unreachable, and nothing else unreachable points at it either. --lost-found copies
each dangling object into .git/lost-found/:
git fsck --lost-found
cat .git/lost-found/other/1e60a8aaab0441e0d37da0add56720cbf0ef1782
dangling blob 1e60a8aaab0441e0d37da0add56720cbf0ef1782
important notes
You get the contents back, not the file name: names live in trees, and this blob never got one. You also get
only the version you added: a later edit was never an object.
When it really is gone
One commit through every level, in a repo where three was just reset away (made with the real clock, so its
ids differ). git gc packs objects (Go deeper) and deletes what has run out:
git gc
git count-objects -v
count: 0
in-pack: 9
All 9 objects were packed, three included: the reflog still holds it, so gc keeps it. Now expire the
reflog line by hand and run gc again:
git reflog expire --expire-unreachable=now --all
git gc
git cat-file -t 1edeef92f49f59050b67fd8d90086bac47e40501
commit
Still there: gc packed the 3 objects main can reach and left the other 6 loose (git 2.39; newer gits use a cruft pack, see Go deeper), because their files are
minutes old, younger than 2 weeks. In a second repository whose loose object files were dated back to January,
the same expire and a plain gc deleted the commit at once. Here --prune=now does it:
git gc --prune=now
git cat-file -t 1edeef92f49f59050b67fd8d90086bac47e40501
fatal: git cat-file: could not get object info
Now the only copy is in another clone, if one has it. So after losing something, don’t run
git gc --prune=now “to clean up”. (The gc git starts on its own follows the same rules: see Go deeper.)
Try it on your computer
Practical 1 · The rescue (the capstone): from an empty folder, build a small history, lose work in all four ways, recover each one, then make one new commit truly gone with gc while everything you rescued survives.
Go deeper: what a stash is made of
git stash writes commits, not a special file. refs/stash holds the newest one:
tree 92658f61eef96067373c3259d382b95911f8d94a
parent 3411c6e2fdbea8e3d9ecc07e7e8a0701e9f62a28
parent 5622c56b73c6810a8d0e355ebc0a0808e14c15b2
...
WIP on main: 3411c6e three
It has 2 parents: the commit you were on (3411c6e) and a commit of your index (5622c56, “index on main”),
the staged state. Its own tree holds your tracked files as they were in the working folder.
* 0fd67c5 3411c6e 5622c56 WIP on main: 3411c6e three
|\
| * 5622c56 3411c6e index on main: 3411c6e three
|/
* 3411c6e 785785b three
Each git stash moves refs/stash and adds a reflog line, so stash@{1} is “where refs/stash was one move
ago”, exactly like main@{1}. git reflog stash prints the same list as git stash list. (git-stash)
Go deeper: packfiles, automatic gc and cruft packs
You rarely run gc yourself. “Some git commands run git gc --auto after performing operations that could
create many loose objects” (git-gc), which starts a real
gc once there are more than 6700 loose objects or more than 50 packs (gc.auto, gc.autoPackLimit). It obeys
the same expiry rules as a gc you type.
Objects start as loose files, one per object (lesson 1). git gc puts this repo’s 9 into one packfile plus an index:
.git/objects/pack/pack-3169a54a9966e6eb6b2d112d8042bbc55334193c.idx
.git/objects/pack/pack-3169a54a9966e6eb6b2d112d8042bbc55334193c.pack
git verify-pack -v lists what’s inside: here non delta: 9 objects. A pack may also store an object as a
delta, the difference from a similar object (none of these 9 is one). Ids don’t change: git cat-file -p HEAD reads the packed commit as before.
(gitformat-pack)
Packing resets the clock. A packed object’s age is its pack’s. Once the reflog line expires, gc keeps the object dated like its pack, or drops it if the pack is older than 2 weeks (pack-objects.c). Each gc rewrites the pack, so if gc runs often the 2 weeks start near the end of the 30 days and the protections mostly add up; if no gc ran since the object was written, they overlap.
Unreachable objects that are too young to prune need somewhere to live. git 2.39, on which this course was
tested, writes them back out as loose files (gc.cruftPacks defaults to false). From git 2.41, gc puts them in
a cruft pack, one pack that also records each object’s age, so the same two-week rule applies without thousands
of loose files (Highlights from Git 2.41). The
rescue is the same.
Common misunderstandings
- “If
git reflogdoesn’t show it, it’s gone.” Plaingit reflogis HEAD’s only. A stash’s commit never was in it, and a deleted branch’s commits are there only if you committed on it or checked it out;fsckstill finds the rest.
Check yourself
1. A colleague made a branch, committed on it and rebased it badly, all 3 weeks ago, and has made commits since. Can they get the branch as it was before the rebase?
Yes. The old commits are held by the branch's reflog (and HEAD's), and a line naming a commit the branch no longer reaches lasts 30 days.git reflog <branch> shows a rebase (finish) line; the entry
just below it is where the branch was before the rebase. git branch before-rebase <that id>
keeps it for good. (The 30 days count from when each line was written: here, when the commits were made.)
2. After a git stash drop, git fsck --unreachable lists two commits, 0fd67c5 and 5622c56, but git fsck --no-reflogs calls only 0fd67c5 dangling. Why, and which one do you apply?
5622c56 is the stash's index commit, one of 0fd67c5's 2 parents. It's unreachable, but
another unreachable object points at it, so it isn't a tip: not dangling. Apply the tip,
git stash apply 0fd67c5: it brings back the working-folder edits, and new files come back staged; --index restores the rest of
the staged state (git-stash).
3. After a bad reset, a teammate "helps" by running git reflog expire --expire-unreachable=now --all && git gc --prune=now. What can you still recover?
Nothing from your repository: the expire removed the reflog lines and --prune=now skipped the
two-week wait. Only the server (if you pushed) or another clone can help.
Next: Practical 1 · The rescue, the capstone.