Checking Out Multiple Branches Simultaneously with worktree
No Need to Clone the Repo Multiple Times
Plain Git only lets you check out one branch per clone. Switching requires git checkout, and your working tree changes need to be committed or stashed first.
What worktree does
Run git worktree add ../other-branch feature-x and Git creates a separate directory that references the same repository, with feature-x checked out inside it.
Now you have two directories:
./repoโ working on main../other-branchโ working on feature-x
Both share the same .git. Commits made in one are immediately visible in the other.
When it shines
Long builds and tests. You can let CI run in one worktree while starting the next task in another.
Review and development, separated. Check out PRs you're reviewing into a dedicated worktree, and keep your own work in the main one.
Emergency hotfixes. Instead of stashing your current work, spin up a new worktree for the hotfix branch and tear it down when done.
Constraints
You can't check out the same branch in two worktrees at once. Submodule support became reliable starting with Git 2.25. Clean up with git worktree remove when you're done.
How It Works
`git worktree add ../path branch-name` -> checkout branch in new directory
`git worktree list` -> list registered worktrees
Commit, build, and test independently in each worktree
`git worktree remove ../path` -> clean up when done
Pros
- ✓ Faster than re-cloning, saves disk
- ✓ Parallel branch work without stashing
Cons
- ✗ Cannot check out the same branch twice
- ✗ Forgotten worktrees become clutter