December 5, 2026

Git Worktree: Two Branches Checked Out at Once, Without a Second Clone

Three file boxes on a desk, each tied by thread to one shared filing cabinet, like worktrees sharing one repository

A reviewer asks for a quick look at a colleague’s branch while a half-finished feature sits in the working directory. The usual escape routes are git stash, a throwaway “WIP” commit, or a second clone of the repository. Git has a fourth option built in: git worktree, which checks out a second branch into its own folder while sharing a single object database with the first. Every command and every line of output below was run on Git 2.43.0 in a scratch repository on a Linux test box.

Video: ThePrimeagen on YouTube

One repository, several folders

The main worktree and its linked siblings

The official git-worktree documentation draws a line between two kinds of working tree. The main worktree is the one git init or git clone creates, and a non-bare repository has exactly one. A linked worktree is anything made later with git worktree add, and a repository can have as many of those as disk space allows.

What makes the feature cheap is the split between shared and private state. Commits, blobs, tags and branches live in one place. Each worktree keeps only its own HEAD, its own index (the staging area) and its own checked-out files. Private administrative files for a linked worktree sit under .git/worktrees/ inside the main repository.

The practical effect: a commit made in one folder is visible from every other folder the instant it exists, with no fetch and no push between them.

Business laptop on a desk

Creating a worktree

A new branch, in a new folder, in one line

The test repository started with a single commit on main and a second branch called feature. Adding a worktree on a brand-new branch takes one command:

$ git worktree add ../demo-hotfix -b hotfix
Preparing worktree (new branch 'hotfix')
HEAD is now at 7a31f01 first commit

Checking out a branch that already exists drops the -b:

$ git worktree add ../demo-review feature
Preparing worktree (checking out 'feature')
HEAD is now at 7a31f01 first commit

git worktree list then shows all three folders, main worktree first:

$ git worktree list
/tmp/wt/demo         7a31f01 [main]
/tmp/wt/demo-hotfix  7a31f01 [hotfix]
/tmp/wt/demo-review  7a31f01 [feature]

With no branch option at all, git worktree add ../something names the new branch after the last part of the path. For a look at an old commit that will never be committed to, the documentation suggests git worktree add -d <path>, which creates a throwaway tree with a detached HEAD.

What the linked folder actually contains

A linked worktree has no .git directory. It has a .git file, one line long, that points back home:

$ cat ../demo-hotfix/.git
gitdir: /tmp/wt/demo/.git/worktrees/demo-hotfix

That pointer is an absolute path. It is the reason moving folders by hand causes trouble later, and the reason git worktree move and git worktree repair exist.

The one-branch-one-folder rule

Git refuses a double checkout

With feature checked out in demo-review, switching to it from the main worktree fails:

$ git checkout feature
fatal: 'feature' is already used by worktree at '/tmp/wt/demo-review'

The refusal is deliberate. Two folders with the same branch checked out would each move the branch pointer when committing, and the other folder’s files and index would silently fall out of step with its own HEAD. The documentation notes that --force overrides the check on git worktree add, which is best read as a warning label, not an invitation.

The workaround for “the same code in two places” is a second branch or a detached HEAD pointing at the same commit.

Shared history in practice

A commit made inside demo-hotfix showed up in the main worktree’s log immediately:

$ git log --oneline --all
7a31f01 first commit
89aedd8 hotfix: patch

No fetch ran between the two commands. Both folders read the same refs/heads/.

Data protection padlock

Removing, pruning and locking

Clean trees leave quietly; dirty ones need force

git worktree remove deletes the folder and its administrative entry, but only when the tree is clean. With an untracked scratch.txt sitting in demo-review, the test produced:

$ git worktree remove ../demo-review
fatal: '../demo-review' contains modified or untracked files, use --force to delete it

Adding --force removed it. The untracked file went with the folder, permanently, since Git never had a copy.

When the folder is deleted by hand

Deleting a worktree with rm -rf leaves a stale record behind. Git notices:

$ git worktree list
/tmp/wt/demo         7a31f01 [main]
/tmp/wt/demo-hotfix  89aedd8 [hotfix] prunable

$ git worktree prune -v
Removing worktrees/demo-hotfix: gitdir file points to non-existent location

The branch itself survives. Only the checkout record is pruned, and hotfix could be added again as a fresh worktree a moment later. According to the documentation, stale records are also cleaned up automatically in time, governed by the gc.worktreePruneExpire setting.

Locking a tree on removable storage

A worktree on a USB drive or network share looks “missing” whenever the drive is unplugged, which makes it a candidate for pruning. git worktree lock prevents that, and the reason is stored alongside:

$ git worktree lock --reason "on usb drive" ../demo-hotfix
$ git worktree list --porcelain | tail -4
worktree /tmp/wt/demo-hotfix
HEAD 89aedd85f1599644701ac02edcbcb1d0bd6e9a70
branch refs/heads/hotfix
locked on usb drive

A locked tree also resists removal. The test box answered git worktree remove with “cannot remove a locked working tree” and a hint to unlock first or pass -f twice.

Moving a worktree, and repairing one moved by hand

The supported way

Renaming a worktree folder through Git keeps both ends of the link in step. On the test box, the hotfix tree moved cleanly:

$ git worktree move ../demo-hotfix ../demo-patch
$ git worktree list
/tmp/wt/demo        7a31f01 [main]
/tmp/wt/demo-patch  89aedd8 [hotfix]

Two limits apply, both from the documentation: the main worktree cannot be moved this way, and neither can a linked worktree that contains submodules.

The unsupported way, and the fix

Dragging the folder somewhere else in a file manager, or running a plain mv, breaks half of the link. The moved folder still knows where the repository lives, so git status inside it keeps working. The repository, though, is left holding the old address, and the listing flags the tree as lost:

$ mv ../demo-patch ../demo-moved
$ git worktree list
/tmp/wt/demo        7a31f01 [main]
/tmp/wt/demo-patch  89aedd8 [hotfix] prunable

That prunable label is a countdown. A git worktree prune, run by hand or by routine garbage collection, would discard the record of a tree that is in fact alive and well one directory over. git worktree repair with the new path rewrites the stale pointer:

$ git worktree repair ../demo-moved
repair: gitdir incorrect: /tmp/wt/demo/.git/worktrees/demo-hotfix/gitdir
$ git worktree list
/tmp/wt/demo        7a31f01 [main]
/tmp/wt/demo-moved  89aedd8 [hotfix]

The administrative entry kept its original name, demo-hotfix, through both moves. Folder names and internal names are allowed to drift apart, and Git tracks the internal one.

The reverse breakage, moving the main repository while linked trees stay put, is handled by the same subcommand run from inside the main worktree, which reconnects every linked tree it can find.

Throwaway trees with a detached HEAD

For bisecting, benchmarking an old release or reading code at a tag, a branch is unnecessary. The -d flag makes a tree that points straight at a commit:

$ git worktree add -d ../demo-look
Preparing worktree (detached HEAD 7a31f01)
HEAD is now at 7a31f01 first commit

The listing marks it (detached HEAD) in place of a branch name. The documentation also notes that a handful of ref namespaces, refs/bisect among them, are kept per worktree, so a bisect session in one folder does not disturb another.

Tree and sky

Worktree, stash or second clone

Three tools for one interruption

git stash Second clone git worktree
Disk cost None Full object database again Checked-out files only
Keeps the feature folder untouched No Yes Yes
Commits visible across copies n/a After push and fetch Immediately
Same branch in two places n/a Allowed Refused by default
Build artifacts survive the switch Often rebuilt Yes Yes

Stash is still the right tool for a thirty-second detour. It gets awkward when the interruption needs a build, a test run or a dev server, because switching branches underneath a build directory tends to invalidate caches and restart long compiles. A separate folder leaves the original one exactly as it was, with editors, terminals and running processes none the wiser.

A second clone solves the same problem at a higher price: a duplicate object database, a second set of remotes to keep current, and local branches that have to be pushed somewhere before the other clone can see them.

Where worktrees bite

Untracked files do not come along

A new worktree contains tracked files only. .env files, node_modules, virtual environments and build output are absent, because Git ignores them by design. A fresh worktree of a JavaScript or Python project usually needs a dependency install before anything runs. The disk saving applies to Git’s history, not to a gigabyte of installed packages per folder.

Submodules are a known weak spot

The documentation is blunt in its BUGS section: “the support for submodules is incomplete”, and multiple checkouts of a superproject are not recommended. Worktrees containing submodules cannot be moved with git worktree move and need --force to remove.

Hooks and config are shared

Hooks live in the common .git/hooks directory and fire in every worktree. Repository config is shared as well, unless the extensions.worktreeConfig extension is switched on and settings are written with git config --worktree.

A layout that stays tidy

Placing worktrees next to the main checkout, as in the examples above (demo, demo-hotfix, demo-review), keeps them out of the main tree and out of its .gitignore. Nesting a worktree inside the main working directory works, but then every tool that walks the project folder walks the second checkout too.

A short routine covers most use:

  • git worktree add ../project-review some-branch when the interruption arrives
  • work, build, test and commit inside that folder
  • git worktree remove ../project-review once it is merged or abandoned
  • git worktree list every so often, to spot anything marked prunable

The branch outlives the folder in every case.

Sources

  • Git documentation, git-worktree: https://git-scm.com/docs/git-worktree
  • Test commands and output: Git 2.43.0 on Linux, run in a scratch repository for this post

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *