Skip to content

Lesson 6 of 14 · 17m

stash vs worktree: Parking Work Properly

stash is two commits on a side ref; worktree is a second checkout sharing one .git — pick by how long the interruption lasts.

You are mid-refactor on feature/rate-limit, nothing is ready to commit, and CI just went red on main — you need a clean tree now. The two tools people conflate here work completely differently: git stash doesn't shelve your files, it commits them to a side ref and cleans the tree. git worktree doesn't clone anything, it gives you a second working directory sharing one .git. Which you want depends on how long the interruption lasts.

stash: two commits you didn't know you made

git stash push -m "wip rate-limit" writes a commit whose first parent is HEAD and whose second parent is a commit of your index — the staged and unstaged states are stored separately — then hangs it off refs/stash and resets your tree. git stash list is just the reflog of refs/stash, which is why entries look like stash@{0}.

Consequences people learn the hard way:

  • Untracked files are not stashed — your new worker.py stays in the tree and can collide with whatever you switch to. -u adds a third commit for untracked files; -a also grabs ignored files, which you almost never want (it takes node_modules with it).
  • pop is apply + drop, but a failed apply keeps the entry — if the drop seems to have eaten your stash, git stash list still shows it.
  • Stash is local — it lives in your .git and never pushes.
  • When the interruption outlasts the stash's context, git stash branch rescue creates a branch from the commit you stashed on, applies the stash there, and drops it — the recovery path for a stash that no longer applies cleanly.

worktree: a second checkout, one object store

For interruptions measured in days, not minutes:

git worktree add ../repo-hotfix main

This creates a sibling directory whose .git is a file pointing into the real .git/worktrees/ admin area — same object store, same refs, same remotes, zero re-download. You get a clean main checkout while your feature tree keeps its node_modules, build cache, and unsaved mess. git worktree list shows them; git worktree remove ../repo-hotfix deletes one (it refuses on a dirty tree without --force); git worktree prune cleans admin entries for directories you deleted by hand.

One constraint is deliberate: a branch can be checked out in only one worktree at a time — the refs would fight. Detached checkouts (--detach, for read-only builds or tests) are exempt. Worktrees have existed since git 2.5 (2015) and are solid now; this is what CI systems and release engineers actually use for parallel builds.

Choosing

  • Stash: minutes — review a teammate's PR, run a test on clean main, pull without conflicts.
  • Worktree: hours to weeks — a parallel release/2.4 you maintain beside main, a long-running build you can't disturb.
  • WIP commit: the underrated third option. git commit -m "wip" on your branch is a real object — it survives gc, can be pushed to a private branch as an off-machine backup, and gets squashed away in the pre-merge rebase. A stash is none of those things.

Pitfalls

  • git stash pop in a dirty tree merges the stash against your edits; conflicts land inline in your files and the stash stays in the list.
  • A worktree shares the repository: a git fetch in one updates origin/* refs for all of them, and git gc run in one collects objects for all.
  • After deleting a worktree directory manually, the stale admin entry lingers — git worktree prune is the broom.
  • stash won't take your tree state on a branch you haven't committed to yet in a repo with no commits; it needs a HEAD to parent on.

Try this

git worktree add ../tmp-clean --detach main in a real repo — detached checkouts are exempt from the one-branch-one-worktree rule, so it succeeds even when your main checkout is sitting on main — run your test suite there while that checkout stays dirty, then git worktree remove ../tmp-clean. Compare disk usage to a second clone — the shared object store is the point.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.