Beyond Git Worktree

Comparison

git worktree vs branch: what each one is, and when to use a worktree

A branch is a name for a line of commits; a worktree is a directory where a branch is checked out. How they differ, how they work together, and when to switch branches or add a worktree instead.

Updated

"Worktree or branch?" is a slightly wrong question, and the answer to it explains both. A branch is a movable name that points at a commit. A worktree is a directory on disk where some commit (usually a branch) is checked out, with its own files, index and HEAD. You don't pick one instead of the other: every worktree normally has a branch checked out in it.

The real choice is between two ways of working on more than one branch: keep one working directory and switch branches in it, or give each branch its own directory with git worktree add. This page lays out what each one is, what they share, and when each way of working is the better fit, including for coding agents running side by side.

The short answer

BranchWorktree
What it isA name (a ref under refs/heads/) that points at a commit and moves forward as you commitA directory with checked-out files, plus its own HEAD and index, attached to a repository
What it costsAlmost nothing: a few bytes in .gitA full copy of the project's files on disk, plus whatever you install or build in it
How many at onceAs many as you likeAs many as you like, each in its own directory
What you seeOnly the branch currently checked out in your directoryOne branch per directory, all open at the same time
Create itgit branch <name> or git switch -c <name>git worktree add <path> [<branch>]
Delete itgit branch -d <name>git worktree remove <path> (the branch stays)

Switch branches when you work on one thing at a time and switching is quick. Add a worktree when you need two branches open at once: a hotfix while a refactor is half done, a pull request to review without stashing, a long test run on one branch while you keep coding on another, or one checkout per coding agent.

What a branch is

A branch is a pointer to a commit, stored under .git/refs/heads/ (or in .git/packed-refs). When you commit with that branch checked out, Git moves the pointer to the new commit. That is all a branch is: no files of its own, no directory, nothing copied.

git switch -c feature/login   # create a branch at HEAD and check it out
git rev-parse feature/login   # the commit it points at
git switch main               # put main's files back in this directory

Because a plain checkout has one working directory, only one branch can be checked out in it at a time. Switching rewrites the files that differ between the two branches. If you have uncommitted changes that would be overwritten, Git refuses with Your local changes to the following files would be overwritten by checkout, and you commit, stash or discard them first.

What a worktree is

Every repository already has one worktree: the directory you cloned into, called the main worktree. git worktree add creates linked worktrees, extra directories that use the same repository. Each has its own checked-out files, its own index (staging area) and its own HEAD, so each can have a different branch checked out:

git worktree add ../app-hotfix -b hotfix main   # new branch 'hotfix', in ../app-hotfix
git worktree add ../app-review pr-42            # existing branch, in ../app-review
git worktree list
# /home/me/code/app          c5c2233 [feature/login]
# /home/me/code/app-hotfix   c5c2233 [hotfix]
# /home/me/code/app-review   8cf6120 [pr-42]

Nothing is cloned. A linked worktree's .git is a small file pointing back at the main repository, and its private state lives in .git/worktrees/<name>/. The git worktree add guide covers every form of the command.

What worktrees share, and what each one keeps

Shared by every worktreeKept per worktree
Commits and other objectsChecked-out files
Branches and tags (refs/heads/, refs/tags/)HEAD: which branch or commit is checked out
Remotes and remote-tracking branchesThe index (staging area)
The stashBisect and in-progress rebase or merge state
Repository config and hooksUntracked and ignored files: node_modules, .env, build output

So a commit made in one worktree is visible in all the others at once, with no fetch or push between them, and a stash made in one can be popped in another. Settings that should differ per worktree need extensions.worktreeConfig turned on and git config --worktree.

The rule that ties them together: one branch, one worktree

A branch can be checked out in only one worktree at a time. Two directories committing to the same branch would overwrite each other's work, so Git refuses:

$ git switch feature
fatal: 'feature' is already used by worktree at '/home/me/code/app-feature'

$ git branch -d feature
error: cannot delete branch 'feature' used by worktree at '/home/me/code/app-feature'
Wording varies between Git versions.

git branch marks branches checked out in another worktree with a + (the current one gets *), and git worktree list shows which directory has which. To get a second copy of a branch anyway, add a detached worktree with git worktree add -d <path> <branch>, or start a new branch from it with -b.

When switching branches is enough

  • You work on one thing at a time and finish or commit before moving on.
  • Switching is cheap: the branches differ in a handful of files, and nothing has to reinstall or rebuild afterwards.
  • The interruption is short. git stash, a quick fix on another branch, git switch - and git stash pop is less work than setting up a new directory.
  • Disk space or setup time matters: a big monorepo, a slow dependency install, or local services that only run from one path.

When a worktree is better

  • Urgent work lands mid-task. Add a worktree for the hotfix and leave your half-finished changes exactly where they are, with no stash to forget.
  • Reviewing a pull request. Check it out in its own directory, run it, and keep your own branch's server and editor open.
  • Long-running jobs. A test suite or build can run in one worktree while you keep editing in another, without the files changing underneath it.
  • Comparing behaviour. Run the old and new versions side by side, on different ports.
  • Switching is expensive. Every switch rewrites the files that differ, which can trigger rebuilds and reindexing; a worktree keeps each branch's build and editor state warm.
  • Parallel coding agents. Each agent session needs its own files and branch so it doesn't edit under another one; a worktree per session gives that without a full clone each.
A new worktree starts with tracked files only. Install dependencies, copy local config such as .env, and give its dev server and test database their own port and name before you rely on it.

Both together: a branch-per-worktree workflow

In practice the two combine: each task gets a branch, and each branch you need open gets a directory. Create both in one step, work, merge, then remove the worktree and delete the branch:

git fetch origin
git worktree add -b fix/timeouts ../app-fix-timeouts origin/main
cd ../app-fix-timeouts
# install dependencies, work, commit, push, open a pull request

cd ../app
git worktree remove ../app-fix-timeouts
git branch -d fix/timeouts

Removing a worktree never deletes its branch, and deleting a branch never removes a worktree: you clean up each one separately. The git worktree remove guide covers the order, --force and git worktree prune.

And a second clone?

Cloning the repository again also gives you a second directory with another branch checked out, but it is a separate repository: its own objects, its own branches, and nothing in common with the first until you push from one and fetch into the other. Nothing stops both clones having the same branch checked out, which is how work diverges. A worktree shares one repository, so there is nothing to sync and Git enforces one branch per directory. A clone is the better fit when you want real separation, such as a different remote or a throwaway copy you can delete without touching your main repository.

Frequently asked questions

Is a git worktree the same as a branch?

No. A branch is a name that points at a commit; a worktree is a directory where a branch (or a single commit) is checked out. A worktree usually has a branch checked out in it, and a branch can be checked out in at most one worktree at a time.

Does git worktree add create a branch?

Only if you ask it to, or if the branch doesn't exist yet. git worktree add -b <name> <path> creates one, and git worktree add <path> creates a branch named after the directory when no branch by that name exists. Given an existing branch, it checks that branch out instead.

Can two worktrees use the same branch?

Not by default. Git refuses with is already used by worktree at, so that two directories don't commit over each other. Use a detached worktree (-d) or a new branch, or --force if you really mean it.

If I delete a worktree, do I lose the branch?

No. git worktree remove deletes the directory and Git's record of it, and leaves the branch and its commits in place. Delete the branch separately with git branch -d once it has been merged.

Should I use worktrees instead of git stash?

For a short interruption, stash and switch is quicker. When the interruption is long, or you need both branches running at once, a worktree avoids stashing entirely and leaves your work untouched. The stash is shared between worktrees, so you can use both.

Do worktrees take up a lot of disk space?

Each one holds a full copy of the project's checked-out files, plus anything installed or built in it, such as node_modules. History isn't copied: commits and other objects are stored once and shared by every worktree.

Sources

More on worktrees in Git Worktrees, or browse all guides.

Further reading in Git Worktrees