Multiple Checkouts with git worktree

Check out several branches at once in separate folders that share one repository — no stashing, no re-cloning.

The classic interruption: you are mid-feature and a hotfix lands. You could git stash, switch branch, fix, switch back, and pop — or you could just open the hotfix in its own folder with git worktree.

What a worktree is

A linked worktree is a second working directory attached to the same .git database. Each worktree has its own checked-out branch, index, and files, but they all share history, remotes, and stashes. No duplicate clone, no duplicated download.

  • git worktree add ../hotfix main — create a folder ../hotfix on main.
  • git worktree add -b fix/login ../login-fix — create a new branch and its folder at once.
  • git worktree list — see every worktree and its branch.
  • git worktree remove ../hotfix — clean up when done.

Why seniors love it

A branch can only be checked out in one worktree at a time, which prevents you from clobbering yourself. It is perfect for running a long build on one branch while you keep coding on another, or reviewing a PR without disturbing your work-in-progress.

Example

Example · bash
# You're deep in a feature; a hotfix can't wait
git worktree add -b hotfix/payments ../app-hotfix main

cd ../app-hotfix     # a clean checkout of main, your feature untouched
# ...fix, commit, push, open PR...

cd ../app            # back to your feature exactly as you left it
git worktree remove ../app-hotfix
git worktree list

When to use it

  • A developer checks out a release branch in a separate worktree to apply a hotfix while keeping the main worktree's feature work intact.
  • A team member runs the test suite on the main branch in one worktree while continuing feature development in another, using the same repo.
  • A developer uses a linked worktree to build documentation from the gh-pages branch without ever switching away from the feature branch they are coding.

More examples

Add a worktree for a hotfix branch

Creates a second checkout of the repository in a sibling directory, each worktree tracking its own branch independently.

Example · bash
# Current worktree is on feature/payments
git worktree add ../hotfix-tree hotfix/login-crash

# In the new directory, the worktree is on that branch
cd ../hotfix-tree
git branch
# * hotfix/login-crash

List and inspect active worktrees

Lists all linked worktrees with their paths, current HEAD commit, and checked-out branch — useful for keeping track of multiple active checkouts.

Example · bash
git worktree list
# /work/my-project         e4a1f3c [feature/payments]
# /work/hotfix-tree        9b0d14a [hotfix/login-crash]

Remove a worktree after the hotfix

Removes the linked worktree directory and its administrative files, leaving the main worktree and repository untouched.

Example · bash
# Done with the hotfix branch
git worktree remove ../hotfix-tree

# Confirm it is gone
git worktree list
# /work/my-project  e4a1f3c [feature/payments]

Discussion

  • Be the first to comment on this lesson.
Multiple Checkouts with git worktree — GitHub | SoundsCode