How to use Git worktrees for multiple branches
· Category: Git
Short answer
git worktree allows you to check out multiple branches into separate directories from a single repository.
Steps
- Create a worktree for a branch:
git worktree add ../repo-fix fix-branch
- List worktrees:
git worktree list
- Remove a worktree:
git worktree remove ../repo-fix
- Create a new branch and worktree:
git worktree add -b hotfix ../repo-hotfix main
Tips
- Worktrees share the same
.gitobject database, saving disk space. - Ideal for reviewing PRs while continuing development on your main branch.
- You cannot check out the same branch in multiple worktrees.
Common issues
- Uncommitted changes in one worktree do not affect others.
- Deleting the worktree directory manually requires
git worktree pruneto clean up.