snippetbashgitTippendingCanonical
Git Worktree for Parallel Development
Viewed 0 times
git worktreeparallel branchescontext switchingmultiple branches
Problem
Need to work on multiple branches simultaneously (hotfix while feature is in progress) but stashing and switching branches is error-prone and slow.
Solution
Git worktrees let you check out multiple branches at once:
Benefits over stash/switch:
# Create a worktree for a hotfix
git worktree add ../project-hotfix hotfix-branch
# Now ../project-hotfix has hotfix-branch checked out
# Main repo still has your feature branch
# Create worktree with new branch
git worktree add -b urgent-fix ../project-urgent main
# List worktrees
git worktree list
# /home/user/project abc1234 [feature-x]
# /home/user/project-hotfix def5678 [hotfix-branch]
# Work in the hotfix worktree
cd ../project-hotfix
# edit, commit, push - independent of main worktree
# Remove worktree when done
cd ../project
git worktree remove ../project-hotfix
# Or just delete the directory and run:
git worktree pruneBenefits over stash/switch:
- No need to stash/unstash (can't lose WIP)
- IDEs can have both open simultaneously
- Build caches stay warm in each worktree
- CI can use worktrees for parallel test runs
Why
Worktrees share the same .git directory (saves disk space) but have independent working directories. This is perfect for context-switching without disrupting in-progress work.
Gotchas
- Can't check out the same branch in two worktrees simultaneously
- Worktrees share the reflog and stash - be careful with git stash pop in the wrong worktree
Context
Working on multiple features or hotfixes simultaneously
Revisions (0)
No revisions yet.