Stash & Worktrees
7 pages · ~5 min✓ Reviewed
Builds on Resolving Merge Conflicts. Next up: Tags, Releases and Cherry-pick.
Stash and Worktree Mental Model
1. Stash = a stack
- Stack of commits:
stash@{0}is the newest - Saves tracked changes from working tree and index
- Then resets the tree to
HEAD - One entry = 2 or 3 commits
- Local only: a stash is never pushed
- Working treecommit 1
- Indexcommit 2
- Untrackedoptional commit 3
2. Worktree = extra checkout
- Extra directory linked to the same repo
- Own checked-out branch,
HEADand index - One shared
.gitobject store and refs - A commit in one is instantly visible in the others
bash
git worktree add ../hotfix -b fix/login
new folder, new branch
3. Which one?
| Stash | Worktree | |
|---|---|---|
| Lasts | Minutes | Hours or days |
| Checkouts | One, time-shared | Many, side by side |
| Holds | Stack of commits | Own branch, HEAD, index |
Rule of thumb
Stash = short interruption you return to in minutes. Worktree = parallel work lasting hours or days.
Core Stash Workflow
1. Save and list
git stash push -m "wip: login form"saves tracked changes with a label, cleans the tree- Bare
git stashdoes the same, with no message git stash listshowsstash@{0},stash@{1}, ...: newest is always 0
bash
git stash push -m "wip: login form"
git stash listoutput
stash@{0}: On main: wip: login form
stash@{1}: WIP on main: 3f2a1 lexer2. Restore
| Command | Applies | Entry after |
|---|---|---|
git stash pop | newest | dropped on clean apply; kept on conflict |
git stash apply | newest | kept |
git stash apply stash@{1} | chosen entry |
Everyday Stash Options
1. What gets swept up
| Flag | Tracked | Untracked | Ignored |
|---|---|---|---|
| none | yes | no | no |
-u | yes | yes | no |
-a | yes | yes | yes |
-u(--include-untracked) stashes new files; plain stash silently leaves them behind-a(--all) adds ignored files: can sweep up build output and.env, so use rarely
2. Stash only part
- Pathspec form stashes only the listed paths
-ppicks hunks interactively
Stash Conflicts and Safe Recovery
1. When Pop or Apply Conflicts
- Pop or apply onto a changed base can conflict
- Resolve the files, then
git addthem: no commit needed applyrefuses if local changes touch the same files: commit or stash first
2. The Entry Stays
- git stash popbase changed
- Conflictentry kept
- Resolve + addno commit
- git stash dropmanual
Duplicate trap
A conflicted pop does not drop the entry. Check git stash list, then git stash drop, or you reapply it later.
3. Recover and Escape
bash
git fsck --unreachable | grep commit git stash apply <sha>
a dropped stash is a dangling commit
git stash branch fix/topic stash@{0}replays on the exact original base commit- Cleanest way out of persistent conflicts
Git Worktree Commands and Choosing Between Them
1. Worktree Commands
add -bmakes a new directory + new branch- A remote-tracking ref gives a detached HEAD; add
-bfor a local branch listshows each path, HEAD commit and branchremovedeletes a clean worktree;remove --forceif dirty
bash
git worktree add ../hotfix -b fix/login git worktree add ../review -b pr-branch origin/pr-branch git worktree list git worktree remove --force ../hotfix
-b pr-branch gives a local branch tracking the PR
2. Rules and Cleanup
- One branch lives in one worktree at a time
- Git refuses a second checkout of the same branch
- Deleting the folder by hand leaves stale metadata
- Clean it with
git worktree prune - Ignored files are not shared: own
node_modules/ venv each
3. Worktree or Stash?
Stash and Worktree Cheat Sheet
1. Stash commands
bash
git stash push -m "msg"
git stash push -u
git stash push -- path
git stash push -p
git stash list; git stash show -p
git stash apply; git stash pop
git stash drop; git stash clear
git stash branch name2. Pop vs apply
pop= apply + drop on successapplykeeps the entry- A conflict keeps the entry in both cases
3. Worktrees
bash
git worktree add ../hotfix -b fix/login git worktree add ../review origin/pr-branch git worktree list git worktree remove ../hotfix git worktree prune
4. Pitfalls and choice
- Untracked files need
-u - A stash is never pushed
- One branch, one worktree only
- A conflicted pop leaves the entry on the stack
| Situation |
|---|
Check yourself
git stash pop hits a conflict. Is the stash entry still in the list?
- Yes: pop drops the entry only after a clean apply
- Resolve,
git add, thengit stash dropby hand
You edited app.py and created notes.txt, then ran git stash. What stays on disk?
notes.txt: untracked files are skippedapp.pyis reset to HEADgit stash push -utakes both
git worktree add ../x main fails while main is checked out elsewhere. Why, and what is the fix?
- One branch can live in only one worktree
- Use
--detachor-b new-name main
You ran rm -rf ../hotfix on a linked worktree. What is left behind?
- Stale metadata, still listed
- Clear it with
git worktree prune