bashadvanced

Git Worktree — Work on Multiple Branches

Use git worktree to check out multiple branches simultaneously in separate directories.

bash
# Add a worktree for a branch
git worktree add ../project-hotfix hotfix/critical-bug
# Creates ../project-hotfix/ with hotfix branch checked out

# Add worktree with a new branch
git worktree add -b feature/new-ui ../project-new-ui main

# List all worktrees
git worktree list
# /home/user/project            abc1234 [main]
# /home/user/project-hotfix     def5678 [hotfix/critical-bug]
# /home/user/project-new-ui     ghi9012 [feature/new-ui]

# Work in the worktree (separate directory, shared .git)
cd ../project-hotfix
git log --oneline -5
# make changes, commit as usual
git add . && git commit -m "fix: critical auth bug"

# Remove a worktree when done
cd ../project
git worktree remove ../project-hotfix

# Force remove (if there are changes)
git worktree remove --force ../project-hotfix

# Prune stale worktree references
git worktree prune

# Lock a worktree (prevent accidental removal)
git worktree lock ../project-new-ui

Use Cases

  • Reviewing PRs without stashing current work
  • Running tests on one branch while coding on another
  • Hotfix deployment without disrupting feature work

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.