More Git answers

by Danny
Published 2 min read

git worktree lets you check out a second branch in its own folder from the same repository, so you can fix a bug or review a PR without stashing or cloning again.

The short answer

#

Use git worktree add to check out another branch into a separate folder. Both folders share one repository, so commits made in either are visible to both, and you never need to stash your current work.


_10
git worktree add ../project-hotfix -b hotfix/login origin/main
_10
cd ../project-hotfix
_10
# work, commit, push as usual
_10
git worktree remove ../project-hotfix

Why it happens

#

A normal clone has one working directory, so it can only have one branch checked out at a time. Switching branches means committing or stashing what you are doing, waiting for files to change, and often rebuilding or reinstalling. Cloning the repository again works, but it duplicates the whole history and the two clones do not share branches or commits until you push and fetch.

A worktree is a second working directory attached to the same .git data. Each worktree has its own checked-out branch, its own staged changes and its own files on disk, but they all share commits, branches, tags, remotes and configuration. Creating one is fast because no history is copied.

How to fix it

#

Add a worktree for an existing branch

Pass a folder path and a branch name. Keeping worktrees next to the main checkout, rather than inside it, stops tools from scanning one inside the other.


_10
git fetch
_10
git worktree add ../project-review feature/new-checkout

If the branch only exists on the remote, Git creates a local branch that tracks it, as long as only one remote has a branch with that name.

Create a new branch in a new worktree

Use -b to make a new branch, and give a starting point. This is the usual way to start an urgent fix while your feature branch stays untouched.


_10
git worktree add -b hotfix/login ../project-hotfix origin/main

To try something out without a branch at all, use --detach.


_10
git worktree add --detach ../project-scratch HEAD

List, remove and tidy up


_10
# See every worktree and its branch
_10
git worktree list
_10
_10
# Remove one (refuses if it has uncommitted changes)
_10
git worktree remove ../project-hotfix
_10
_10
# Clean up records of worktree folders you deleted by hand
_10
git worktree prune

Removing a worktree does not delete its branch. Delete the branch separately with git branch -d once it is merged.

Set up each worktree like a fresh checkout

Only tracked files appear in a new worktree. Ignored files such as node_modules, build output and .env files are not copied, so install and configure each one.


_10
cd ../project-hotfix
_10
cp ../project/.env.local .env.local
_10
npm ci

If two worktrees run a dev server at the same time, give them different ports.

Common pitfalls

#
  • "is already checked out" or "is already used by worktree". A branch can only be checked out in one worktree at a time. Use a different branch, or switch the other worktree away first.
  • Deleting the folder with rm. Git still thinks the worktree exists and keeps the branch locked. Run git worktree prune, or use git worktree remove next time.
  • The stash is shared. All worktrees use the same stash list, so git stash pop in one can apply changes saved in another. Name your stashes or prefer commits.
  • Nesting worktrees inside the main folder. Editors, test runners and linters may pick up the second copy. Keep them as sibling folders.
  • Moving folders by hand. Use git worktree move so Git updates its records.
#

Was this resource helpful?