Git worktrees: work on two branches at once

ยทcheatly.dev

You're deep in a feature branch, files changed everywhere, and someone asks you to review a PR on a different branch. Your options: stash everything, commit half-finished work, or tell them to wait.

Git worktrees give you a fourth option: check out another branch in a separate directory, without touching your current work at all.

What a worktree is

A worktree is an additional working directory linked to the same repository. Each worktree has its own branch checked out, its own working tree, its own index โ€” but they all share the same .git data. Changes you commit in one worktree are immediately visible in the other.

The essential commands

Worktree Basics
git worktree add ../feature-x feature-x
Check out the feature-x branch in a sibling directory
git worktree add ../hotfix -b hotfix
Create a new branch and check it out in a sibling directory
git worktree list
See all worktrees and which branch each has
git worktree remove ../feature-x
Remove a worktree (branch stays, directory goes)

The pattern: git worktree add <path> <branch>. The path is where the new working directory lives โ€” typically a sibling folder next to your main repo.

A real workflow

~/projects/cheatly/           โ† main worktree (feature branch)
~/projects/cheatly-review/    โ† second worktree (PR review)
  1. You're working on a feature in ~/projects/cheatly/
  2. Need to review a PR: git worktree add ../cheatly-review pr-branch
  3. cd ../cheatly-review โ€” the PR is checked out, ready to review
  4. Review, test, leave comments
  5. cd ../cheatly โ€” your feature work is exactly where you left it
  6. Done with the review: git worktree remove ../cheatly-review

No stashing. No committing garbage. No context switching penalty.

Each worktree runs its own dev server on its own port. You can literally have two versions of your app running simultaneously โ€” perfect for comparing behavior between branches.

When worktrees beat stashing

When to just stash instead

If the switch takes less than 5 minutes and you don't need both branches simultaneously, git stash is simpler. Worktrees shine when you need to keep both contexts alive.

Worktrees share the same git history, so git log, git branch, and git remote work identically in both. The only difference is which branch is checked out and which files are on disk.

For the full reference, see our git cheatsheet and git stash guide.