Git Worktree for Multiple Branches
Work on multiple branches simultaneously using git worktree without stashing or switching.
# Add a worktree for a different branch
git worktree add ../project-hotfix hotfix/critical-fix
# Add a worktree and create a new branch
git worktree add -b feature/new-feature ../project-feature main
# List all worktrees
git worktree list
# Navigate to the worktree and work normally
cd ../project-hotfix
git status
npm install && npm run build
git add . && git commit -m "fix: resolve critical issue"
git push origin hotfix/critical-fix
# Go back to main worktree
cd ../project
# Remove a worktree when done
git worktree remove ../project-hotfix
# Prune stale worktree references
git worktree prune
# Lock a worktree to prevent accidental removal
git worktree lock ../project-feature
# Unlock a worktree
git worktree unlock ../project-featureUse Cases
- Reviewing PRs while working on a feature
- Hotfix deployment without disrupting current work
- Running tests on multiple branches in parallel
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Git Rebase Workflow Example
Step-by-step git rebase workflow to keep feature branches up to date with main branch cleanly.
Git Cherry-Pick Commits Example
Apply specific commits from one branch to another using cherry-pick with conflict resolution.
Undo Last Git Commit (Soft and Hard)
Git commands to undo the last commit while keeping changes staged, unstaged, or fully discarded.
Squash Multiple Git Commits
Interactive rebase to squash multiple commits into one clean commit before merging a feature branch.