git worktree

Work on multiple branches simultaneously by creating additional working directories linked to the same repository.

You're deep into work on a feature branch when an urgent bug fix lands on your plate. Normally you'd stash your changes, switch branches, fix the bug, then switch back. But what if you could keep both branches active at the same time in separate directories? git worktree lets you check out multiple branches simultaneously, each in its own directory, all connected to the same repository.

Running git worktree add ../hotfix main creates a new directory alongside your current one, with main checked out and ready for work. You can make commits in the hotfix directory while your original directory stays on your feature branch. Both directories share the same repository history and configuration—commits made in either show up when you run git log in both.

This is perfect for urgent fixes that interrupt feature work. Instead of stashing and context-switching, you keep your feature branch as-is, work on the hotfix in a separate directory, test both simultaneously, and merge the fix without disturbing your in-progress feature. Running builds or tests in parallel across branches becomes trivial.

To create a worktree with a new branch, use git worktree add -b new-branch ../new-branch. This creates the directory, creates the branch, and checks it out there. Listing worktrees with git worktree list shows all active working trees and which branches they have checked out.

When you're done with a worktree, remove the directory and clean up with git worktree remove ../hotfix. This tells Git the worktree no longer exists. If you just delete the directory without using worktree remove, run git worktree prune to clean up Git's tracking.

One branch can only be checked out in one worktree at a time, preventing conflicts from simultaneous edits in different locations. If you try to create a worktree with a branch that's already checked out elsewhere, Git refuses unless you force it with --detach.

In pull request workflows, worktrees let you review PRs in separate directories while continuing your own work. Check out the PR branch in a worktree, run the code, test it, and leave feedback, all without touching your main working directory. You can even compare builds side-by-side.

Understanding worktrees means escaping the limitation of one working directory per repository. It's multiple checkouts of the same repository, sharing history and configuration but with independent working directories. This is cleaner than cloning the repository multiple times and more flexible than constantly stashing and switching.