Using Git Worktree Deliberately for Local Code Reviews
AI generated
git
HEAD
Git · Code Review · Worktree
Git Worktree for Local Code Reviews
parallel checkouts instead of constant branch switching

A review that briefly bumps your own feature branch out of the way costs more than just the time spent on git stash and git checkout: running dev servers, compiler watchers or Docker containers often fall out of step too. git worktree instead sets up an additional, independent checkout of the same repository in its own directory, keeping the branch under review open alongside your own work in progress. This article shows how to use worktrees deliberately for reviews, manage them, and clean them up again.

10 min read Git Code Review

1. The problem with branch switching for parallel reviews

A quick review in between usually means: stash your own changes with git stash, switch to the branch under review, look through the code, then switch back and reapply the stash. For a single, short interruption that is still manageable, but with several parallel reviews on the same day the overhead adds up noticeably.

More disruptive than the raw time cost is often the side effect on running processes: a dev server, a watcher for Tailwind CSS builds, or a running Docker container usually operate on whatever is currently checked out in the working directory. Switching branches mid-review throws these processes out of sync or requires a full restart once you switch back to your own branch.

2. What a git worktree is and how it differs from cloning

A git worktree is an additional working directory that shares the same internal .git data store as the original repository but provides its own, independent checkout of a specific branch on the filesystem. Objects, commits and configuration are not duplicated but shared, which means an extra worktree needs barely any additional disk space compared to a full second clone of the repository.

Unlike a full second clone, worktrees stay tightly connected: a new commit created in one worktree is immediately visible as a reference in the main repository and in every other worktree, with no need to push and fetch between the directories first.

3. Creating a review worktree for a pull request branch

The git worktree add command takes a target path and, optionally, a branch name, and sets up a full, independent checkout from it. If the given branch already exists locally, it gets checked out right there; if it only exists remotely, Git automatically checks it out as a new local tracking branch in the new directory.

For a review, a single command is therefore enough: a new directory appears next to the main repository, the branch under review is fully checked out there, and your own work in progress in the main directory stays completely untouched throughout, including any uncommitted changes and running background processes.


# Create a new worktree for an existing feature branch
git worktree add ../review-checkout-redesign feature/checkout-redesign

# Create a new worktree for a branch that only exists remotely
git worktree add ../review-pricing-fix origin/fix/pricing-rounding

4. Checking out existing remote branches directly into a worktree

For reviewing pull requests from GitHub or GitLab, the associated remote branch can be referenced directly when creating the worktree, without first fetching manually into the main repository and creating a local branch there. A single git fetch followed by git worktree add is usually enough to make a submitted branch fully available locally.

For projects using the GitHub or GitLab CLI, this step can be simplified further: gh pr checkout checks out a pull request branch directly, and combined with a prepared worktree directory, the actual review workflow still stays fully separated from your ongoing work.


# Fetch the current state of all remote branches, then create a worktree
git fetch origin
git worktree add ../review-pr-142 origin/feature/pr-142

5. Managing several worktrees: list, lock and remove

As the number of parallel reviews grows, a regular glance at git worktree list pays off, since it lists every currently existing worktree along with its path and checked-out branch in a clear overview. That makes it easier to keep track of which directory belongs to which review, especially when several pull requests are being checked at the same time.

For worktrees on external or occasionally disconnected drives, git worktree lock prevents Git from mistakenly treating that worktree as orphaned during internal cleanup and removing it automatically while the drive is temporarily disconnected. Once a review is finished, git worktree remove cleanly removes the directory again, including the corresponding internal reference in the main repository.


# List every currently existing worktree with its path and branch
git worktree list

# Cleanly remove a finished review worktree
git worktree remove ../review-pr-142

6. Combining worktrees with your IDE workflow

Since every worktree is its own directory on the filesystem, PhpStorm or a comparable IDE can open a separate project window for each review, entirely independent from the main project window holding your own work in progress. A review comment, jumping to a definition, or a test run started on a whim then always operates in the right context, without accidentally affecting your own branch.

For Magento or Hyvä projects with their own Docker setup, that additionally means a separate compose setup or a dedicated database container can be started specifically for the review worktree, while the familiar development environment in the main directory keeps running unaffected.

7. Cleanup: removing deleted worktrees and orphaned references

If a worktree directory gets deleted manually through the filesystem by accident instead of through git worktree remove, the main repository is left with an orphaned internal reference at first, which shows up flagged as broken the next time you list worktrees. git worktree prune cleans up exactly these orphaned references by checking which referenced directories still actually exist and removing every remaining stale entry automatically.

An occasional, regular git worktree prune is especially worthwhile with a high turnover of short-lived review worktrees, so the repository's internal bookkeeping does not needlessly accumulate references to directories that no longer exist.


# Remove orphaned references to manually deleted worktree directories
git worktree prune

8. Limitations and pitfalls of worktrees in everyday review work

The same branch cannot be checked out into two different worktrees at the same time, Git deliberately prevents that to avoid conflicting simultaneous changes to one and the same branch. For parallel reviews of different branches that is no issue at all, but it does matter if the same branch is accidentally opened twice.

In addition, every worktree of a repository shares the same underlying object database, which means aggressive, destructive operations such as git gc --aggressive or manually deleting objects in the main repository can in theory affect other worktrees too. In everyday review work built purely on reading and testing, this risk practically never comes up.

9. Comparison to stash-based context switching

The classic sequence of git stash, switching branches and later git stash pop remains a legitimate, simple option for very rare, one-off interruptions, especially when no second directory should be created at all. But once reviews happen regularly, or several branches need to stay open at once, the advantages of a dedicated worktree clearly win out.

The decisive difference is that a worktree does not touch your own work in progress at all: no stash that gets accidentally forgotten, no risk of a stash conflict when switching back later, and no need to restart running development processes. The table below summarizes the key differences.

Criterion Stash and branch switching Git worktree Recommendation
Affects your own work in progress Yes, temporarily displaced No, stays fully untouched Worktree for frequent reviews
Running dev servers/watchers Get interrupted Keep running independently Worktree with active background processes
Extra disk space needed None Small, shares the object database Both economical on disk space
Suited to a one-off, quick check Yes, straightforward Slightly more setup effort Stash for isolated one-off cases
Suited to several parallel reviews Impractical, gets messy fast Yes, one directory per review Worktree for several open PRs

Mironsoft

Git workflows, branching strategies, and CI hooks

Chaotic Git history and unclear branching rules across the team?

We set up clean Git workflows, clarify branching strategies for the team, and automate quality checks via Git hooks and CI pipelines so the history stays traceable.

Workflow Audit

Review the existing branching strategy and merge practice for weak spots.

Hook Automation

Set up pre-commit and pre-push hooks for linting, tests, and commit conventions.

Team Training

Teach rebase, cherry-pick, and conflict resolution hands-on across the team.

10. Summary

Worktree for Reviews

Core idea

git worktree add sets up a second, independent checkout of the same repository in its own directory.

Biggest advantage

Your own work in progress stays untouched, running dev servers and watchers are not interrupted by the review.

Management

git worktree list for an overview, remove for finished reviews, prune for orphaned references.

Key limitation

The same branch cannot be checked out into two different worktrees at the same time.

11. FAQ: Worktree for Reviews

1What is a git worktree at its core?
An additional working directory that shares the same internal .git data store as the original repository but provides its own, independent checkout of a specific branch on the filesystem.
2How does a worktree differ from a full second clone?
A worktree shares objects, commits and configuration with the main repository, needing barely any extra disk space, while a second clone duplicates everything fully.
3How do I create a worktree for an existing feature branch?
With git worktree add followed by the desired target path and branch name, for instance git worktree add ../review-branch feature/checkout-redesign.
4Can I check out a branch that only exists remotely directly into a worktree?
Yes, after a git fetch, git worktree add with the corresponding remote branch as reference is enough to automatically create it as a new local tracking branch in the worktree.
5How do I keep track of several parallel review worktrees?
git worktree list shows every currently existing worktree with its path and checked-out branch in a clear overview.
6How do I cleanly remove a finished review worktree?
With git worktree remove and the corresponding path, which removes the directory and the corresponding internal reference in the main repository together.
7What happens if I accidentally delete a worktree directory manually?
The main repository is left with an orphaned internal reference at first. git worktree prune detects such no-longer-existing directories and removes the corresponding references automatically.
8Can I check out the same branch into two worktrees at once?
No, Git deliberately prevents that to avoid conflicting simultaneous changes to one and the same branch.
9How can worktrees be combined with a Docker-based Magento setup?
A separate compose setup can be started specifically for a review worktree, while the familiar development environment in the main directory keeps running unaffected.
10When does the classic stash-based branch switch remain a sensible choice?
For very rare, one-off interruptions, when no second directory should be created at all and no running background processes are affected.