Git Stash and Worktrees: Managing Multiple Tasks
Every developer faces the same problem: you’re in the middle of a feature when a critical bug fix is needed. Git stash and worktrees offer two complementary solutions for managing multiple tasks without losing work or context.
Git Stash: Save Work in Progress
git stash temporarily shelves changes so you can switch branches without committing half-finished work.
Basic Stash Operations
# Stash tracked changes (files already staged or tracked)
git stash
# Stash everything including untracked files
git stash --include-untracked
# or git stash -u
# Stash untracked AND ignored files
git stash --allStash with a Message
git stash push -m "WIP: refactoring auth middleware"List and Inspect Stashes
git stash list
# stash@{0}: On main: WIP: refactoring auth middleware
# stash@{1}: On feature/payments: fix payment validation
git stash show stash@{1}
# Shows files changed in that stash
git stash show -p stash@{1}
# Shows the full diffApplying Stashes
# Apply the most recent stash (keeps it in the stash list)
git stash apply
# Apply a specific stash
git stash apply stash@{2}
# Apply and remove from stash list
git stash pop
# Apply to a different branch
git stash branch new-feature-branch stash@{0}Partial Stashing
Stash only specific files or hunks:
# Stash only staged changes
git stash --staged
# Interactive hunk selection
git stash --patchThe --staged option (available since Git 2.35) is useful when you have staged changes ready for one commit but unstaged changes that need more work. It stashes only the staged changes, leaving the unstaged work in place.
Cleaning Up
# Remove a specific stash
git stash drop stash@{1}
# Remove all stashes
git stash clearAdvanced Stash Patterns
Stashing in a Script
# Safe context switching in automation
git stash push -m "auto-save before pull"
git pull --rebase
if [ $? -eq 0 ]; then
git stash pop
else
echo "Pull failed. Stash saved as: auto-save before pull"
fiStash Across Feature Branches
Keep a stash per context:
# On branch feature/alpha
git stash push -m "feature/alpha: WIP"
# Switch to feature/beta
git checkout feature/beta
git stash push -m "feature/beta: WIP"
# Back to alpha
git checkout feature/alpha
git stash apply stash@{1} # apply alpha's stashStashing Untracked Files with Specific Patterns
# Stash only untracked markdown files
git stash push -- include-untracked -- '*.md'Git Worktrees: Parallel Working Copies
Worktrees let you check out multiple branches simultaneously in separate directories — all sharing the same Git repository. No stashing, no committing WIP, no context switching cost.
Why Use Worktrees
| Problem | Stash Solution | Worktree Solution |
|---|---|---|
| Urgent bug fix on main | Stash WIP, fix, pop stash | Open worktree for main, fix there |
| Reviewing a PR | Clone again or fetch + checkout | Add worktree for PR branch |
| Long-running test | Stay on same branch | Run test in worktree, keep coding |
| Compare behavior | Can’t easily | Open two worktrees side by side |
Basic Worktree Commands
# Add a new worktree for branch "hotfix/login"
git worktree add ../project-hotfix hotfix/login
# Add a worktree and create a new branch
git worktree add -b new-feature ../project-new-feature main
# List all worktrees
git worktree list
# /home/user/project (main)
# /home/user/project-hotfix (hotfix/login)
# /home/user/project-new-feature (new-feature)The worktree directory structure:
project/ # main worktree (bare repo lives here)
.git/ # shared with all worktrees
src/
tests/
project-hotfix/ # linked worktree (has its own working dir)
.git → ../project/.git/worktrees/hotfix-login # symlink
src/
tests/Practical Workflow
# Your main worktree: feature branch
cd ~/project
# Urgent hotfix: create a worktree
git worktree add ../project-hotfix main
cd ../project-hotfix
# Fix the bug, commit, push
cd ../project
# Clean up when done
git worktree remove ../project-hotfix
# or prune stale worktrees
git worktree pruneConcurrent Testing
Run tests on different branches simultaneously:
# Worktree 1 — run integration tests on staging
git worktree add ../project-staging staging
cd ../project-staging && npm run test:integration &
# Worktree 2 — keep developing
cd ../project && git checkout feature/new-api
# Continue coding while tests run in the backgroundBisect in a Worktree
A common advanced use case is running git bisect in a separate worktree to avoid disturbing your main development environment:
# Create a worktree for bisecting
git worktree add ../project-bisect main
cd ../project-bisect
# Run bisect in the isolated worktree
git bisect start HEAD v1.0.0
git bisect run npm test
# When done, remove the bisect worktree
cd ..
git worktree remove ../project-bisectThis keeps your main worktree on your feature branch while bisect checks out dozens of commits in the isolated worktree. You can continue coding in your main worktree while the bisect runs in the background.
Worktree Limitations
- Shared refs: Deleting a branch used by a worktree will fail. Remove the worktree first.
- Same remote: All worktrees share the same remote configuration.
- Storage: Each worktree has its own working directory (~200MB for a full checkout).
- Stashing across worktrees: Stashes are global, accessible from any worktree.
# Cannot delete a branch that has an active worktree
git branch -D hotfix/login
# error: Cannot delete branch 'hotfix/login' checked out at '/home/user/project-hotfix'
# Remove the worktree first
git worktree remove ../project-hotfix
git branch -D hotfix/loginWorktree with Different Remotes
While worktrees share the same remote configuration by default, you can configure individual worktrees with different remotes for specialized workflows:
# In your main worktree
cd ~/project
git remote add upstream https://github.com/upstream/project.git
# In a worktree for open-source contribution
cd ../project-contribution
git remote add fork https://github.com/my-user/project.gitThis is useful when you maintain both an upstream and a fork workflow. Each worktree can push to different remotes while sharing the same object database.
When to Use Each
| Situation | Stash | Worktree |
|---|---|---|
| Quick context switch (< 5 min) | ✓ | — |
| Need the same IDE window | ✓ | — |
| Long-running parallel tasks | — | ✓ |
| Code review multiple branches | — | ✓ |
| Running tests on different branches | — | ✓ |
| Comparing behavior between branches | — | ✓ |
| Limited disk space | ✓ | — |
| CI/CD automation | ✓ | ✓ |
Combining Stash and Worktrees
The most powerful workflow uses both tools together. Use worktrees for sustained parallel development (multiple features, code reviews, long-running tests) and stash for quick context switches within a single worktree. For example, you might have three worktrees open (main, feature-A, hotfix) and use stash within each worktree for interruptions like helping a colleague debug an issue.
IDE Integration
Most modern IDEs have built-in support for Git stash and worktrees. VS Code’s Source Control panel shows stashes in a dedicated section and lets you apply, drop, or pop them with a single click. JetBrains IDEs (IntelliJ, PyCharm, WebStorm) support worktrees through the Git tool window. Using these integrations can reduce the cognitive load of managing multiple contexts without leaving your editor.
Summary
Git stash and worktrees solve the same problem — managing multiple tasks — at different scales. Stash is lightweight and good for quick context switches. Worktrees provide full parallel environments for sustained multi-branch work. Used together, they let you handle interruptions cleanly without half-baked commits cluttering your history. Mastering both tools will dramatically reduce the friction of context switching and help you maintain flow throughout your workday.
Related: Git Branching Strategy | Git Workflow Strategies
FAQ
Q: Can I have more than one worktree for the same branch? A: No. A branch can only be checked out in one worktree at a time. If you try, Git will refuse with an error message.
Q: Do worktrees share Git hooks?
A: By default, yes. Hooks in the main repository’s .git/hooks/ apply to all worktrees unless a worktree has its own hooks configured.
Q: Can I push from a worktree? A: Yes. Worktrees can push and fetch normally since they share the repository’s remote configuration.
Q: What happens to stashes when I delete a worktree? A: Stashes are stored in the main repository, not in individual worktrees. Deleting a worktree does not affect stashes.
Q: How do I clean up stale worktree directories?
A: Use git worktree prune to remove stale administrative files for deleted worktrees. To fully remove a worktree, use git worktree remove <path>.
Q: Is stash or worktree better for frequent context switching? A: For very frequent switches (every few minutes), stash is lighter weight. For sustained parallel work that lasts hours or days, worktrees are better because they avoid the cost of stashing and unstashing repeatedly.
Related Concepts and Further Reading
Understanding git stash worktrees requires familiarity with several interconnected ideas and principles that together form a complete picture. Exploring these related concepts deepens your knowledge and provides context that makes the core material more meaningful and applicable. Each concept builds on the others, creating a web of understanding that supports deeper learning and practical application. Taking time to explore how these elements connect reveals patterns that accelerate comprehension and retention of new information.
The relationship between git stash worktrees and adjacent fields is worth particular attention. Many of the most important insights emerge at the boundaries between disciplines, where ideas from different areas combine to create new approaches and solutions that neither field could produce alone. Exploring these connections pays dividends in both breadth and depth of understanding, revealing patterns and principles that might otherwise remain hidden from view. Cross-disciplinary knowledge is increasingly valued as problems become more complex and interconnected.
For those looking to go beyond introductory material, several excellent resources provide deeper treatment of specific aspects of git stash worktrees. Academic journals, industry publications, authoritative reference works, and online courses each offer different perspectives and levels of detail. The key is to match your reading to your current learning goals and build knowledge progressively, focusing on quality over quantity in your study materials. A well-chosen resource that matches your current level is worth more than dozens of resources that are too basic or too advanced.