Ch. 16 · Git & CI/CD

Git Worktrees

Check out several branches at once in separate directories with git worktree, sharing one object database.

~2 min readintermediateupdated Oct 5, 2026

A worktree lets you have several branches checked out at the same time in different directories, all backed by the same repository. It is useful for reviewing a branch while continuing work, running a build from another branch, or comparing two versions without stashing.

Before you start

You should be comfortable with branches and the working tree. This article covers worktrees.

Step-by-step walkthrough

Step 1: Add a worktree for another branch

git worktree add ../hotfix hotfix creates a directory with hotfix checked out, while your main working tree keeps its branch. Both share the same .git object database, so there is no second clone and no duplicated objects.

Step 2: Work in each independently

Each worktree has its own working files and index, so changes in one do not disturb the other. You can build, test and commit in either, and branch operations are shared. This is cleaner than stashing and switching back and forth.

Step 3: Clean up when done

git worktree remove <path> (or deleting the directory and running git worktree prune) removes the worktree. Leaving stale entries clutters git worktree list, and a directory deleted outside Git can leave a dangling record.

Worked scenario

A second directory holds another branch.

git worktree add ../review feature/payments
cd ../review
git log --oneline -1     # on feature/payments
Terminal

Walk through the example

The new directory has feature/payments checked out while the original keeps its branch, and both use the same repository data. You can review or build the feature without touching your current work. Removing the worktree later frees the directory without affecting the main checkout.

Common mistake

Trying to check out the same branch in two worktrees, which Git prevents because a branch can only be checked out once. Another is deleting a worktree directory manually and leaving a stale entry, which prune cleans up.

Verify the behavior

Add a worktree, confirm it has a different branch checked out, and confirm commits made there appear in the shared repo. Try to check out an already-checked-out branch elsewhere and observe the refusal. Remove the worktree and confirm git worktree list is clean.

Interview exercise

Why use a worktree instead of cloning the repository again?

Answer and reasoning

A worktree shares the same object database, so it uses a fraction of the disk and no additional network transfer, and branch state is immediately consistent because it is one repository. A second clone duplicates objects and can drift. The worktree also avoids stash-and-switch churn.

Continue learning

Compare stashing in Stash limits and working state in Working tree and index. Read the Git worktree documentation and try the Git interview questions.

More in Git & CI/CD

read ✓Git & CI/CD · easy

Git Detached HEAD

What a detached HEAD is, when it happens, and how to keep commits made in that state.

~2 min readread →
read ✓Git & CI/CD · mid

Git Hooks and Local Automation

Automate checks with client and server hooks, share them through a framework, and enforce the same rules in CI where they cannot be bypassed.

~2 min readread →
read ✓Git & CI/CD · hard

Git LFS for Large Files

Track large binaries with Git LFS, keep pointer files in the repository, and plan for LFS storage and access.

~2 min readread →
esc