git/worktree

Manage Worktrees

Manage multiple working trees linked to a single Git repository for simultaneous development across branches

git
worktree
repository
branches
parallel-development
multibranch

Command

git worktree

Explanation

`git worktree` enables you to have multiple working directories (worktrees) linked to a single Git repository. Each worktree can be checked out to a different branch, allowing you to work on multiple features or releases in parallel without needing to constantly switch branches or clone the repository multiple times. This is particularly useful for reviewing code, running tests, or managing release branches concurrently. When you add a new worktree with `git worktree add <path> <branch>`, Git creates a new directory at the specified path and associates it with the repository’s `.git` directory. You can list all worktrees using `git worktree list` and remove one safely using `git worktree remove <path>`. Worktrees share the same object database but maintain separate working directories and index files, keeping them lightweight and efficient compared to multiple clones.

Common Use Cases

  • Working on multiple branches simultaneously without switching context
  • Testing or reviewing code from different branches in parallel
  • Maintaining long-lived release branches while continuing development on main
  • Reducing disk space usage compared to multiple full clones
  • Performing isolated builds or experiments in separate directories

Best Practices

  • Use descriptive folder names when creating new worktrees for clarity (e.g., ../feature-login)
  • Run `git worktree list` regularly to monitor active worktrees
  • Always remove worktrees using `git worktree remove` to maintain a clean state
  • Use separate worktrees for feature development, testing, and releases
  • Prune stale worktree references using `git worktree prune` occasionally

Common Mistakes to Avoid

  • Deleting a worktree directory manually instead of using `git worktree remove`, causing dangling references
  • Forgetting that all worktrees share the same repository history and object database
  • Trying to check out the same branch in multiple worktrees simultaneously (Git prevents this)
  • Assuming each worktree is an independent repository clone (they share storage and config)

Troubleshooting

Problem: Cannot check out branch in new worktree — already checked out elsewhere

Solution: Each branch can only be checked out in one worktree at a time. Create a new branch or detach HEAD before reusing it.

Problem: Worktree directory deleted manually, but Git still lists it

Solution: Run `git worktree prune` to remove stale worktree entries.

Problem: Disk space still large despite multiple worktrees

Solution: Worktrees share objects but may still store separate index and working files. Clean up build artifacts or temporary files in each worktree.

Problem: Confusion about which worktree you are in

Solution: Run `git rev-parse --show-toplevel` or `git worktree list` to confirm which worktree you are currently working in.

Examples

Create a new worktree for the feature-branch

git worktree add ../new-feature feature-branch

List all worktrees associated with the repository

git worktree list

Remove an existing worktree

git worktree remove ../old-feature

Clean up references to deleted worktrees

git worktree prune

Create a separate worktree for release branch v1.0

git worktree add ../release v1.0