Git Worktree: Several Branches Checked Out at Once

git worktree lets one repository have several working folders, each on its own branch, sharing a single object store. Run git worktree add ../hotfix -b hotfix to make a new folder on a new branch, git worktree list to see them all, and git worktree remove ../hotfix when finished. No second clone needed.

The worktree commands you need day to day
TaskCommand
New folder on a new branchgit worktree add ../app-fix -b fix/login
New folder on an existing branchgit worktree add ../app-review feature/cart
Throwaway folder at a commitgit worktree add --detach ../app-old v1.4.0
See every worktreegit worktree list
Delete a worktreegit worktree remove ../app-fix
Clean up folders you deleted by handgit worktree prune

Updated

Stashing, switching and rebuilding every time someone asks for a quick fix wastes time. A worktree gives you a second checkout of the same repo in another folder, so a long test run, a code review and an urgent patch can each live in their own directory. Use the builder to get the commands, then check the reference for the rarer subcommands.

Every git worktree subcommand

git worktree subcommands and their syntax
SubcommandSyntaxUse it to
addgit worktree add [-b <new-branch>] <path> [<commit-ish>]Create a folder and check out a branch or commit in it
listgit worktree list [-v | --porcelain]Show every worktree with its HEAD and branch; --porcelain is for scripts
removegit worktree remove [-f] <worktree>Delete a clean worktree folder and its records
prunegit worktree prune [-n] [-v]Forget worktrees whose folders are gone
movegit worktree move <worktree> <new-path>Move a worktree folder and update git's records
lock / unlockgit worktree lock [--reason <text>] <worktree>Stop a worktree being pruned, moved or removed
repairgit worktree repair [<path>...]Reconnect worktrees after folders were moved by hand

A hotfix without leaving your branch

Patch production while a feature is half done

Your feature folder stays exactly as it was. The fix lives in a sibling folder and is removed once pushed.

# from inside ~/code/app, on feature/cart with uncommitted changes
git fetch origin
git worktree add ../app-hotfix -b hotfix/price-rounding origin/main
cd ../app-hotfix
npm install
# fix, test, commit
git push -u origin hotfix/price-rounding
cd ../app
git worktree remove ../app-hotfix

List worktrees in a script-friendly form

Each worktree is a block of lines separated by a blank line: its path, HEAD hash and branch (or detached).

git worktree list --porcelain
# worktree /home/me/code/app
# HEAD 4c1d9e0...
# branch refs/heads/feature/cart
#
# worktree /home/me/code/app-hotfix
# HEAD 7a2b113...
# branch refs/heads/hotfix/price-rounding

Good reasons to use worktrees

Reviewing a pull request without disturbing half finished work, keeping a slow test suite running on one branch while coding on another, and comparing two versions of an app side by side are the classic cases.

They also suit tools that run several coding sessions on one repository: each session gets its own folder and branch, so one session's uncommitted files never leak into another's build.

What is shared and what is not

All worktrees share commits, branches, tags, remotes and most config. Each has its own HEAD, index and working files, so staging in one folder does nothing to the others.

Ignored files such as .env, node_modules and build folders are not copied into a new worktree. Copy the env file over yourself and install dependencies before running anything.

How it works

  1. Type the folder you want and the branch name, and choose new branch, existing branch or detached commit.
  2. Copy the add command and run it from inside your main checkout.
  3. Work in the new folder like any clone: commit, push and pull there.
  4. When the branch is merged, run the remove command the builder shows, and prune if you ever deleted a folder by hand.

Tips

Frequently asked questions

What is the difference between a worktree and a clone?

A clone copies the whole repository, history included. A linked worktree shares the original repository's objects and refs and only adds a new working folder plus a little metadata, so commits made in one are instantly visible from the others without fetching.

Can two worktrees use the same branch?

Not by default. git worktree add refuses a branch that is already checked out elsewhere, because two folders moving one branch would overwrite each other's work. --force overrides this, but a detached HEAD or a second branch is almost always the better choice.

Why does git worktree remove fail?

It refuses when the folder has modified tracked files or untracked files, so you do not lose work. Commit or clean the folder first, or pass --force. A locked worktree needs --force twice.

What does git worktree prune do?

It deletes the leftover records git keeps for worktrees whose folders no longer exist, for example after you removed one with rm -rf. Add -n to see what it would remove first.

How do I rename or move a worktree folder?

Use git worktree move <worktree> <new-path> so git updates its records. If you already moved it with the file manager, run git worktree repair from the new location to reconnect it.

Related: Git cheat sheet, Git rebase guide, Rename a git branch, npm commands cheat sheet.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap