Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Practical 1 · The rescue (capstone)

Goal: a repository called rescue where you lose work in the four ways lesson 3 described, and get every piece back without retyping it; then one commit you destroy for good with git gc, while everything you rescued survives.

Time: ~40 minutes. Where: your own computer, in ~/git-internals-intro/, in a terminal. Read lesson 3 first.

cd ~/git-internals-intro
./lab update
./lab setup p1

./lab update gets the copy of the lab script that knows this practical. Skipped Practical 0, or your folder is in a state you don’t trust? ./lab reset p1 moves your repositories aside and replays Practical 0’s solution.

This time you make commits the ordinary way, with git add and git commit. Your ids will differ from the ones below (they contain your name and the time); read yours from your own output.

Task 1 · A history to lose

Make a repository rescue in ~/git-internals-intro, first branch main, with three commits:

  • one: a file a.txt holding the line one
  • two: adds the line two to a.txt
  • three: adds the line three
Solution
git init -b main rescue
cd rescue
echo one > a.txt; git add a.txt; git commit -m one
echo two >> a.txt; git commit -am two
echo three >> a.txt; git commit -am three
git log --oneline
a2e7ddb three
9a55164 two
24b4168 one

Task 2 · Lost by a reset

git reset --hard HEAD~2
HEAD is now at 24b4168 one

Predict first: what does git fsck --unreachable print now? And git fsck --unreachable --no-reflogs?

What you see

The first prints nothing: a reflog line still points at three, so for fsck nothing is unreachable. The second ignores reflogs and lists the 6 objects of two and three:

unreachable blob 814f4a422927b82f5f8a43f8fab6d3839e3983f2
unreachable tree 6218aaa5fc1a58f5b32cbee55bc0cb0954022787
unreachable commit a2e7ddbc99d0a416468179766c8fdf49db9509bb
...

Put main back at three, without typing its id.

Hint

git reflog lists where HEAD has been; HEAD@{n} names a line of it.

Solution
git reflog
git reset --hard HEAD@{1}
24b4168 HEAD@{0}: reset: moving to HEAD~2
a2e7ddb HEAD@{1}: commit: three
9a55164 HEAD@{2}: commit: two
24b4168 HEAD@{3}: commit (initial): one
HEAD is now at a2e7ddb three

Task 3 · Lost with a branch

From main, make a branch feature with two commits: f1 (a file f.txt holding f1) and f2 (adds the line f2). Switch back to main and delete feature, first with -d, then with -D:

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 8b68bea).

Newer git words the refusal differently (git 2.47.3: error: the branch 'feature' is not fully merged, with the git branch -D suggestion on hint: lines); it means the same.

Pretend the last line scrolled away. Recreate feature at f2 without that id.

Hint

ls .git/logs/refs/heads: the branch’s own reflog went with it. HEAD’s didn’t, and you made both commits on HEAD.

Solution
git switch -c feature
echo f1 > f.txt; git add f.txt; git commit -m f1
echo f2 >> f.txt; git commit -am f2
git switch main
git branch -d feature
git branch -D feature
git reflog -4
a2e7ddb HEAD@{0}: checkout: moving from feature to main
8b68bea HEAD@{1}: commit: f2
2f4ae19 HEAD@{2}: commit: f1
a2e7ddb HEAD@{3}: checkout: moving from main to feature
git branch feature HEAD@{1}
git log --oneline main..feature
8b68bea f2
2f4ae19 f1

main..feature means “commits on feature that aren’t on main”: exactly the two you rescued.

Task 4 · Lost with a stash

On main, add the line four, not committed yet to a.txt, create b.txt holding a new file and git add it (not a.txt). git status --short shows M a.txt and A b.txt. Now:

git stash
git stash drop
git stash list
Saved working directory and index state WIP on main: a2e7ddb three
Dropped refs/stash@{0} (a017ee4dd5858879a25d79f0228c05b37bf9c614)

git stash list prints nothing. Pretend the id in the Dropped line scrolled away. Put the stash back into git stash list, with the message rescued stash, so you can apply it whenever you like.

CommandWhat it does
git log -1 --format='%h %s' <id>one commit’s short id and subject
git stash store -m <message> <id>adds a commit to refs/stash and its reflog: what git stash does after writing the commits
Hint

fsck finds the stash’s commits even without --no-reflogs (lesson 3 says why). There are two; the stash is the one whose subject starts with WIP on.

Solution
for id in $(git fsck --unreachable | awk '/commit/ {print $3}'); do git log -1 --format='%h %s' $id; done
ceae614 index on main: a2e7ddb three
a017ee4 WIP on main: a2e7ddb three
git stash store -m 'rescued stash' a017ee4
git stash list
git stash show --stat
stash@{0}: rescued stash
 a.txt | 1 +
 b.txt | 1 +
 2 files changed, 2 insertions(+)

Both changes are in it: the edit you never staged and the new file you did. Leave it stored; the checker reads it.

Task 5 · Lost before any commit

Create notes.txt holding important notes, git add it, then git reset --hard. ls no longer shows it. Get it back, and commit it on main with the message Recover notes.

Predict first: will git give you back the name notes.txt?

Solution
git fsck --lost-found
ls .git/lost-found/other
dangling blob 1e60a8aaab0441e0d37da0add56720cbf0ef1782
1e60a8aaab0441e0d37da0add56720cbf0ef1782
cp .git/lost-found/other/1e60a8aaab0441e0d37da0add56720cbf0ef1782 notes.txt
git add notes.txt
git commit -m 'Recover notes'

No name: only a tree gives a blob a name, and this one never got into a tree. You chose notes.txt yourself. The id is the same as lesson 3’s and every reader’s: the same bytes, the same blob. In a repository with more dangling blobs, cat each one to find yours.

Task 6 · Break it: the gc that makes it gone

Make one more commit on main, doomed, adding a file doomed.txt. Save its id outside the repository, then lose it:

git rev-parse HEAD > ../doomed-id
git reset --hard HEAD~1
HEAD is now at 53453ef Recover notes

Predict first: after each of these three steps, does git cat-file -t $(cat ../doomed-id) still say commit?

  1. git gc
  2. git reflog expire --expire-unreachable=now --all, then git gc
  3. git gc --prune=now
What you see

Run them in order, with git cat-file -t $(cat ../doomed-id) after each, and git count-objects -v after steps 1 and 2:

Aftercat-file -tWhy
1. git gccommita reflog line still points at it, so gc packs it with the rest: in-pack: 27
2. expire, then gccommitno reflog line left, but its 3 object files are minutes old, younger than 2 weeks: count: 3 loose, in-pack: 24
3. gc --prune=nowfatal: git cat-file: could not get object info--prune=now skips the two weeks

On git 2.41 or newer these 3 objects go into a cruft pack instead of loose files (lesson 3, Go deeper), so your count-objects numbers differ; cat-file -t still says commit until step 3.

The 2 weeks run on the real clock: your objects were made minutes ago, so step 2 keeps them. Now find out what’s left, in the order you would after a real loss:

  1. git reflog and git reflog main: no line names the doomed id.
  2. git fsck --unreachable --no-reflogs: prints nothing; there’s nothing unreachable left to find.
  3. The only way back would be another clone that has the commit.

And what you rescued?

git stash list
git log --oneline main..feature
stash@{0}: rescued stash
8b68bea f2
2f4ae19 f1

Still there: a branch and the stash’s ref point at them, so no gc removes them. The expire removed the doomed lines, which nothing reaches any more, but kept HEAD’s commit: f1 and commit: f2 lines: feature reaches those. Rescuing means giving lost work a name: a branch, a tag, a stash entry, a commit. Then it’s ordinary work again.

Check your work

cd ~/git-internals-intro
./lab check p1

All 12 checks should pass.

Next: that’s the course. Back to the front page for the whole path.