Skip to content

Git Stash & Worktrees: Managing Multiple Contexts Efficiently

DodaTech Updated 2026-06-22 5 min read

In this tutorial, you'll learn about Git Stash & Worktrees: Managing Multiple Contexts Efficiently. We cover key concepts, practical examples, and best practices.

Git stash temporarily shelves uncommitted changes so you can switch branches, while worktree lets you check out multiple branches in separate directories.

In this tutorial, you'll learn Git stash and worktree — two features that help you manage multiple tasks without losing progress. Every developer faces interruptions: an urgent bugfix while you're mid-feature, or needing to work on two branches simultaneously. By the end, you'll switch contexts cleanly and keep your workflow productive.

flowchart TD
  A[Working on feature] --> B[Urgent bugfix needed]
  B --> C[git stash -u]
  C --> D[Switch to main]
  D --> E[Fix bug, commit, push]
  E --> F[Switch back]
  F --> G[git stash pop]
  G --> H[Continue feature work]

Git Stash: Saving Work in Progress

Use git stash when you need to switch branches but have uncommitted changes:

# Start working on a feature
echo "partial work" > feature.py
git add feature.py
# Oh no, urgent bugfix needed!
git stash

Expected output:

Saved working directory and index state WIP on main: a1b2c3d Initial commit

Your working directory is now clean. Switch to fix the bug, then return and restore:

git checkout main
echo "bugfix" > fix.py
git add fix.py
git commit -m "Urgent bugfix"
git checkout feature-branch
git stash pop

Expected output:

On branch feature-branch
Changes to be committed:
  new file: feature.py
Dropped refs/stash@{0}

Stash Variations

Command Description
git stash Stash tracked files only
git stash -u or --include-untracked Also stash untracked files
git stash -a or --all Stash everything including ignored files
git stash list Show all stashes
git stash show Show summary of latest stash
git stash pop Apply and remove latest stash
git stash apply Apply stash without removing it
git stash drop Remove a stash without applying
git stash clear Remove all stashes

Naming and Selecting Stashes

Give stashes descriptive names:

git stash push -m "WIP: login form validation"
git stash list

Expected output:

stash@{0}: On feature/login: WIP: login form validation
stash@{1}: On main: a1b2c3d Initial commit

Apply a specific stash by index:

git stash apply stash@{1}

Git Worktree: Multiple Branches at Once

Git worktree lets you check out different branches in separate directories simultaneously:

# Add a new worktree for a feature branch
git worktree add ../project-feature feature/login

This creates a directory ../project-feature with the feature/login branch checked out. You can work in both directories without switching.

# List all worktrees
git worktree list

Expected output:

/home/user/project          main
/home/user/project-feature  feature/login

Remove a worktree when done:

git worktree remove ../project-feature

Stash vs Worktree

Feature Stash Worktree
Purpose Temporary save Parallel workspaces
Scope Uncommitted changes Full branch checkout
Number Unlimited stashes One per branch
IDE support Native in all tools Manual switching
Disk usage None Full repo clone per worktree
Best for Quick context switches Long-term parallel work

Common Errors

Error Cause Fix
Cannot stash: index has unmerged files Merge conflict in progress Resolve conflicts first or git stash --include-untracked
fatal: refusing to merge unrelated histories Worktree from different repo Use --allow-unrelated-histories
git stash pop caused conflicts Stash conflicts with current state Resolve conflicts, Git keeps the stash
Worktree path not found Directory was deleted git worktree prune to clean metadata
Stash is empty No stashes saved Check git stash list
Permission denied on worktree Lock from another process git worktree unlock <path>
fatal: '<path>' already exists Directory conflicts Remove directory or choose different path
Cannot remove worktree Unsaved changes Commit or stash first

Practice Questions

What is the difference between git stash pop and git stash apply?

git stash pop applies the stash to your working directory and removes it from the stash list. git stash apply applies the stash but keeps it in the list, allowing you to apply the same stash to multiple branches.

When should I use worktree instead of stash?

Use worktree when you need to work on two branches simultaneously for an extended period — for example, maintaining a release branch while developing the next version. Use stash for quick interruptions that last minutes to hours.

Does git stash save untracked files?

By default, git stash only saves tracked files. Use git stash -u (or --include-untracked) to also stash untracked files. Use git stash -a to stash everything including ignored files.

Can I recover a dropped stash?

Yes, if you know the commit hash. Run git fsck --lost-found to find dangling commits, or check git reflog. The stash is stored as a commit object in Git's database.

How many worktrees can I have?

There is no hard limit, but each worktree is a full checkout with its own working directory and index. Disk usage grows linearly with the number of worktrees. Most developers keep 2-5 active worktrees

Challenge

You're working on a feature branch with 10 modified files (3 staged, 7 unstaged). An urgent hotfix needs your attention. Stash the unstaged changes only, keeping the staged ones. Fix the hotfix on main, commit, then restore your partial work. Write the complete command sequence.

Real-World Task

Set up a Git worktree for a release branch while keeping your main development worktree active. In the release worktree, cherry-pick a bugfix commit from main. Test it, commit, and push. Remove the worktree when done. This mirrors how Linux kernel maintainers handle release branches alongside main development.


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro