W3docs

git worktree

Learn the git worktree command to check out several branches at once from a single repository, without re-cloning. Includes commands and examples.

Definition

The git worktree command lets you check out more than one branch at the same time from a single repository. Each worktree is a separate working directory with its own checked-out branch, but they all share the same .git object store and history. It is the clean way to work on two branches in parallel without cloning the repository twice.

One Git repository linked to several working trees, each on a different branch

The problem it solves

Imagine you are deep in a feature branch when an urgent bug report arrives. Normally you would have to stash your work, switch branches, fix the bug, switch back, and unstash — disruptive and error-prone. With worktrees, you simply create a second working directory checked out to the hotfix branch, fix the bug there, and leave your feature work completely untouched.

Adding a worktree

Point git worktree add at a path and a branch. To check out an existing branch into a sibling directory:

git worktree add ../hotfix hotfix

This creates ../hotfix with the hotfix branch checked out. To create a brand-new branch at the same time, use -b:

git worktree add -b experiment ../experiment main

You now have two independent working directories backed by one repository.

Listing and removing worktrees

See every working tree attached to the repository:

git worktree list

When you are done with one, remove it (this deletes its directory):

git worktree remove ../hotfix

If you delete a worktree's folder manually, tidy up the stale administrative entry with:

git worktree prune

Common options

CommandDescription
git worktree add <path> <branch>Creates a working tree at <path> checked out to <branch>.
git worktree add -b <new> <path>Creates a new branch and checks it out in a new working tree.
git worktree listLists all working trees attached to the repository.
git worktree remove <path>Removes a working tree and its directory.
git worktree pruneCleans up records for working trees whose folders were deleted.

One rule to remember

A branch can only be checked out in one worktree at a time. If you try to add a worktree for a branch that is already checked out elsewhere, Git refuses. This guarantees the two directories can never fight over the same branch pointer.

Practice

Practice

Which statements about 'git worktree' are correct?