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.pystays in the tree and can collide with whatever you switch to.-uadds a third commit for untracked files;-aalso grabs ignored files, which you almost never want (it takesnode_moduleswith it). popisapply+drop, but a failed apply keeps the entry — if the drop seems to have eaten your stash,git stash liststill shows it.- Stash is local — it lives in your
.gitand never pushes. - When the interruption outlasts the stash's context,
git stash branch rescuecreates 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 mainThis 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.4you maintain besidemain, 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 survivesgc, 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 popin 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 fetchin one updatesorigin/*refs for all of them, andgit gcrun in one collects objects for all. - After deleting a worktree directory manually, the stale admin entry lingers —
git worktree pruneis the broom. stashwon'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?
Keep reading
Related articles from the SudoSecurity blog.
What is Git? The Version Control System Every Developer Uses
Git tracks every change to every file in your codebase, lets multiple developers work simultaneously without overwriting each other, and gives you a complete history you can rewind to any point. It is the foundation of modern software development. Here is how it actually works.
VSCode 1.72: Git Source Control has some good enhancements.
The latest version of Microsoft’s Visual Studio Code code editor focuses on improvements to the toolbar and autoscrolling, as well as Git repos. Unveiled October 6, Visual Studio Code 1.72 is also known as the September 2022 release, as it features fixes and features from last month. Toolbar
Apple's WebKit comes to GitHub
The development of WebKit, the open source web browser engine at the heart of Apple’s Safari web browser, has migrated over to GitHub. The WebKit project team announced August 31 that the project had frozen its Subversion tree on June 23 and transitioned management and interaction with the source
GitHub Open Sources Entitlements IAM
GitHub is making available a new IAM (Identity and Access Management) tool, dubbed Entitlements, which leverages the company's own Git framework to parse, track and approve access to a business' systems. The basic idea of Entitlements is to use a dedicated Git repository as a way to provide a…