๐ŸŒฒ

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

1

`git worktree add ../path branch-name` -> checkout branch in new directory

2

`git worktree list` -> list registered worktrees

3

Commit, build, and test independently in each worktree

4

`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

Use Cases

Parallel work during long CI/builds Dedicated checkouts for reviewing PRs Emergency hotfix handling Providing per-worktree isolation for AI agents

References