Sub topic 4
Under the hood: what a stash really is
This chapter assumes you have used git stash push and git stash pop, and that you know a commit stores a snapshot, a message, and parent pointers. You will inspect a stash with plumbing commands, see why it behaves like a branch of commits, and recover a stash you dropped by mistake.
A stash is a set of commits
Git has no separate storage format for stashes. A stash is a commit, and usually two. A reference named refs/stash points to the newest one.
When you run git stash push, Git creates these commits:
- The index commit. It records the staged state. Its message begins with
index on. - The work-in-progress commit. It records the state of your tracked files in the working tree. This is the commit
refs/stashpoints to. - An untracked-files commit. Git creates it only when you pass
-u(or-a). It holds the untracked files.
The work-in-progress commit is a merge commit. Its parents are the commit you were on (HEAD), then the index commit, then the untracked-files commit if one exists.
Inspecting a stash
Create a stash, then read the commit with git cat-file -p.
git stash push -m "wip: login form validation"
git cat-file -p stash@{0}The output looks like this. Your hashes will differ.
tree 9c1f0e2b5d7a4c8e1f3a6b2d0c9e8f7a6b5c4d3e
parent 3f2a9c1d8e7b6a5f4c3d2e1f0a9b8c7d6e5f4a3b
parent 7a1b2c3d4e5f60718293a4b5c6d7e8f901234567
author Dana Dev <[email protected]> 1764862127 +0000
committer Dana Dev <[email protected]> 1764862127 +0000
On main: wip: login form validationThe first parent is the base commit. The second is the index commit. If you stash without -m, the message reads WIP on main: 3f2a9c1 <subject>, using the base commit's subject.
The tree line is a snapshot of your tracked files, not a diff. When Git applies a stash, it compares that snapshot against the base commit's snapshot and merges the difference into your current files. This is why git stash apply can succeed or conflict on a different branch from the one you stashed on.
Useful read-only commands:
git log --graph --oneline stash@{0}
git diff stash@{0}^1 stash@{0}
git diff stash@{0}^1 stash@{0}^2
git show stash@{0}^3 --statThe first command draws the commit structure. The second shows everything the stash changed against its base. The third shows only the staged part. The fourth shows the untracked files and exists only for a stash made with -u. In these expressions, ^1, ^2, and ^3 select the first, second, and third parent.
The stack is a reflog
refs/stash points only to the newest stash. The others are kept in the reflog of that reference. A reflog is a log of the values a reference has pointed to over time. The name stash@{2} is shorthand for the third entry in the reflog of refs/stash.
git reflog show refs/stash
git rev-parse stash@{0}When you push a stash, Git adds a reflog entry. When you drop one, Git removes its entry and renumbers the rest. When you drop the last one, Git deletes refs/stash.
Recovering a dropped stash
Dropping a stash removes only the reference. The commits stay in the object database until garbage collection deletes unreachable objects. That gives you a recovery window.
If you just ran git stash pop or git stash drop, Git printed the hash of the dropped commit, for example Dropped refs/stash@{0} (7a1b2c3...). You can restore it directly:
git stash apply 7a1b2c3d4e5f60718293a4b5c6d7e8f901234567If you lost that output, search for unreachable commits:
git fsck --unreachable --no-reflogs | grep commit
git show --stat <hash>Stash commits are easy to identify. Their messages begin with WIP on or On <branch>:. Check candidates with git show, then pass the right hash to git stash apply. Run this soon after the mistake, because git gc eventually deletes unreachable commits.
Why this matters
- A stash records its base commit, so
git stash branch <name> stash@{0}can start a new branch at that exact commit and apply the stash there without conflicts. git stash show -pis just a diff between the stash's first parent and the stash commit.- Keeping a stash for a long time is risky. It sits outside every branch, and a dropped or expired entry is hard to find again. If the work matters, commit it to a branch instead.
Exercise
You ran git stash push -u. Which parent of the stash commit holds your untracked files, and what command shows them?
Show answer
The third parent. Run git show stash@{0}^3 --stat to list the files. Without -u the stash has only two parents, so ^3 does not exist.
Exercise
You ran git stash drop by mistake and the hash was not printed. How can you find the stash commit again?
Show answer
Run git fsck --unreachable --no-reflogs | grep commit, check the candidates with git show, and restore the right one with git stash apply <hash>. This works only until garbage collection removes the unreachable commit.