bashadvanced

Git Worktree for Multiple Branches

Work on multiple branches simultaneously using git worktree without stashing or switching.

bash
# 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-feature

Use 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.