Worktrees
Git worktrees let you have multiple working directories attached to a single repository.
A worktree is simply another folder connected to the same repository. It can point to any branch or commit. Each worktree is isolated in its working files, but all of them share the same Git history, and switching to a worktree is as simple as moving into that folder with
cd. This lets you work on different branches or commits in parallel, avoid stashing or making temporary commits just to move around, and keep each task in its own separate folder.
git worktreelist- Show all active worktrees.add <path> <branch_name>- Create a worktree for an existing branch.add <path> <commit_hash>- Create a worktree at a specific commit in adetached HEADstate.remove <path>- Remove the worktree folder and its reference.
Do not place worktrees inside other working directories.
If you create a worktree folder inside another working directory, Git treats that folder as new content.
While Git can detect that it is a worktree and reduce it to a single entry, it still appears as untracked content, which you usually do not want in your real project history.
Example approaches to structure projects when working with worktrees.
-
One base and others:
You have one main working directory and other worktree folders next to it.
├─ project/ # <- main working directory (the repository folder) ├─ feat/ # <- worktree for the feature branchHow:
Run
git worktree add ../feat featureinside the repository folder. -
Everything is a worktree:
There is one repository folder, but to work, you have to create a worktree.
├─ repo.git/ # <- the repository folder ├─ main/ # <- worktree for the main branch ├─ feat/ # <- worktree for the feature branchHow:
First, you need to run
git clone --bare <remote_url>to create a local bare copy.A bare clone is a repository without a working directory. It contains only the
.git/data.Then you can create worktrees from the bare clone to work in, using
git worktree add ../main mainandgit worktree add ../feat feature.
git worktree command, you need to be inside a repository folder.