Worktrees

Topic 3 of 4 in Power tools5 min readBeginnerFree course

Overview

Switching branches is quick — until you have uncommitted work, a running dev server, or a long build you don’t want to redo. Worktrees let one repository have several working directories, each on a different branch, side by side. Need to fix something on main while you’re mid-feature? Open a second worktree; your feature folder stays exactly as it was.

git worktree: several branches checked out at once. One .git repository, with shared objects and history, serves three folders: ~/shop on branch main, ~/shop-hotfix on branch hotfix/login, and ~/shop-review on branch pr-128. Commands: git worktree add ../shop-hotfix -b hotfix/login, git worktree list, and git worktree remove ../shop-hotfix.

Adding a worktree

Terminal
git worktree add ../shop-hotfix -b hotfix/login main   # new branch from main, in a new folder
git worktree add ../shop-review feature/search          # an existing branch
git worktree add --detach ../shop-v1 v1.0.0             # a tag, detached
Terminal
git worktree list
Text
/home/asha/shop          4b5a6f7 [feature/reports]
/home/asha/shop-hotfix   9c8d7e6 [hotfix/login]

cd ../shop-hotfix, and you’re in a normal working directory: edit, commit, push as usual.

What’s shared and what isn’t

Shared between worktrees Separate per worktree
commits and objects files on disk (working directory)
branches and tags staging area
remotes and config HEAD (which branch is checked out)
stashes untracked and ignored files (like node_modules)

So a commit made in one worktree is immediately visible from the others — git log hotfix/login works in your main folder straight away, with no push or fetch.

One branch, one worktree

A branch can only be checked out in one worktree at a time — otherwise two folders would fight over moving it:

Text
fatal: 'main' is already used by worktree at '/home/asha/shop'

Work around it by creating a new branch, checking out the commit detached, or updating the branch without checking it out. For example, to fast-forward main to include a branch, when main isn’t checked out anywhere:

Terminal
git fetch . hotfix/login:main     # "fetch" from this repository: move main to hotfix/login (fast-forward only)

Cleaning up

Terminal
git worktree remove ../shop-hotfix   # refuses if the worktree has uncommitted changes (--force to override)
git worktree prune                   # forget worktrees whose folders you deleted manually

Removing a worktree deletes its folder, not its branch or commits. Delete the branch separately if you’re done with it. Note that git branch -d checks whether the branch is merged into the branch you’re on — if you merged it into main from elsewhere, confirm with git branch --merged main and then use -D.

When worktrees shine

  • Urgent fixes without disturbing work in progress — no stashing, no switching back and forth.
  • Reviewing pull requests: gh pr checkout in a dedicated review worktree, run it, compare with your branch side by side.
  • Long-running tasks: let tests or a build run in one worktree while you keep coding in another.
  • Comparing versions: an old release in one folder, current code in another.
  • Running several AI coding agents or experiments in parallel, each in its own worktree on its own branch, so they can’t overwrite each other’s files.

Each worktree needs its own dependencies installed (node_modules, virtual environments), so they cost disk space — clean up finished ones.

Worktrees vs clones

You could clone the repository twice instead, but then the copies are independent: commits in one need a push and fetch to reach the other, and you download and store everything twice. Worktrees share one repository, so they’re lighter and instantly in sync.

Next time you reach for git stash to handle an interruption that’ll take more than a few minutes, try git worktree add instead.

Exercise

Fix production without touching your work

You’re mid-feature on feature/reports with uncommitted edits. An urgent fix is needed on main. Without stashing or committing your half-done work, add a second worktree in a sibling folder on a new branch hotfix/login based on main, fix the bug there and commit. Back in your original folder, confirm your uncommitted edits are untouched. Merge the hotfix into main (from the hotfix worktree’s point of view, main is checked out elsewhere — find a way), then remove the extra worktree and list worktrees.

Terminal
mkdir shop && cd shop && git init -qb main
echo "login v1" > login.txt && echo "reports v1" > reports.txt
git add . && git commit -qm "Start shop"
git switch -qc feature/reports
echo "reports v2 (half done)" > reports.txt         # uncommitted work in progress

# 1. Add a worktree for the hotfix, on a new branch from main

# 2. Fix and commit in the hotfix worktree

# 3. Check your original work is untouched

# 4. Get the fix into main

# 5. Clean up

Summary

  • git worktree add <path> <branch> checks out another branch in a separate folder, sharing the same repository.
  • Each worktree has its own working directory, staging area and HEAD, but commits and branches are shared.
  • A branch can only be checked out in one worktree at a time.
  • Use worktrees for urgent fixes, reviewing PRs, long builds or comparing versions side by side.
  • Remove finished worktrees with git worktree remove, and git worktree prune stale ones.