Sub topic 11
A Bare Repository with One Worktree per Branch
This chapter assumes you can create, list and remove linked worktrees, and that you know how remotes and remote-tracking branches work. It also assumes you know that one branch can be checked out in only one worktree at a time. The chapter shows a layout where no directory is the "main" checkout. Instead, a bare repository stores the history, and every branch you work on gets its own sibling directory.
Why consider this layout
In the usual setup you run git clone, and you get a directory with a .git folder inside it. That directory is the main worktree. Every linked worktree you add depends on it. The main worktree is also special in practical ways:
- You cannot remove it with
git worktree remove. - Its
.gitdirectory holds the object store that all other worktrees use. - It has a branch checked out, so that branch is unavailable to other worktrees.
If you decide to keep one directory per branch, this asymmetry gets in the way. You end up with myproject/ on main and myproject-feature/ beside it, and the first one is treated differently from the rest. Deleting myproject/ also deletes the repository that every other worktree needs.
A bare repository removes the asymmetry. A bare repository is a repository with no working directory. It holds only the history, branches and configuration that normally live inside .git. Every working directory is then a linked worktree, and each one can be created or deleted independently.
Creating the layout
The setup has four steps: clone bare, fix the fetch configuration, fetch, then add worktrees.
git clone --bare https://example.com/team/myproject.git myproject.git
cd myproject.git
git worktree add ../main main
git worktree add ../feature-x -b feature-x mainThe first command creates myproject.git, which contains files such as HEAD, config, objects/ and refs/. There is no checked-out source tree. The .git suffix is a naming convention that marks the directory as a bare repository.
The second git worktree add creates feature-x as a new branch that starts at main. Afterward, the directory tree looks like this:
projects/
├── myproject.git/ (bare repository)
├── main/ (worktree, branch main)
└── feature-x/ (worktree, branch feature-x)Running git worktree list from any of these directories shows the bare repository too:
git worktree list/home/dev/projects/myproject.git (bare)
/home/dev/projects/main a1b2c3d [main]
/home/dev/projects/feature-x a1b2c3d [feature-x]The commit hashes here are examples. Yours will differ.
Why `main` can be checked out here
The bare repository's HEAD usually points at main. You might expect that to block git worktree add ../main main, because of the one-branch-one-worktree rule. It does not. A bare repository has no working directory, so it does not count as having a branch checked out. The rule applies only to real worktrees.
Fixing the fetch configuration
A bare clone behaves differently from a normal clone in one way that catches many people. A normal clone sets up this configuration:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*A bare clone does not. It copies the remote's branches straight into local branches (refs/heads/*) and sets no fetch refspec. A fetch refspec tells Git which remote refs to copy and where to store them. Without one, git fetch origin does not create or update origin/main and the other remote-tracking branches. You cannot compare against origin/main, and you cannot see what teammates pushed.
The fix is to add the refspec and fetch:
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
git branch -rAfter this, git branch -r lists origin/main and the other remote branches. Run the config command once, right after cloning. The setting is stored in myproject.git/config, which every worktree shares.
A complete setup script looks like this:
#!/usr/bin/env bash
set -euo pipefail
url="https://example.com/team/myproject.git"
git clone --bare "$url" myproject.git
cd myproject.git
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
git worktree add ../main mainDaily work in this layout
Starting a new task
From any worktree, create the new branch from the up-to-date remote branch:
git fetch origin
git worktree add ../feature-y -b feature-y origin/mainBecause the start point is a remote-tracking branch, Git sets origin/main as the new branch's upstream by default. This is the default branch.autoSetupMerge behavior. If you start from a local branch, as in the earlier feature-x example, no upstream is set. You then publish the branch once with git push -u origin feature-x.
Updating branches
In a bare clone, git fetch updates only the remote-tracking branches under refs/remotes/origin/. It does not move local branches such as main. To bring a worktree's branch up to date, run the update inside that worktree:
cd ../main
git merge --ff-only origin/main--ff-only refuses to create a merge commit. If local main has diverged, the command fails instead of making a surprise merge.
Reviewing someone else's branch
Use a detached worktree, which does not occupy a branch name:
git fetch origin
git worktree add --detach ../review origin/feature/searchFinishing a task
git worktree remove ../feature-x
git branch -d feature-xRemoving a worktree does not delete its branch. Run git branch -d afterward if the branch is merged and you no longer need it. You can run both commands from the bare repository or from any other worktree.
Comparing the two layouts
| Aspect | Normal clone plus linked worktrees | Bare repository plus worktrees |
|---|---|---|
| Where history lives | In `.git` inside the main worktree | In the bare repository directory |
| Can every checkout be deleted freely? | No, the main worktree must stay | Yes, each one is equal |
| Branch checked out in the first directory | Yes, blocks that branch elsewhere | No, the bare repository holds no checkout |
| Fetch refspec after cloning | Set automatically | Must be set by hand |
| `git status` in the repository directory | Works | Fails, there is no working tree |
| Remote-tracking branches after clone | Created | Absent until you fix the refspec and fetch |
Neither layout is better in general. The bare layout fits people who always work in several branch directories and want a consistent structure. The normal layout fits people who mostly work in one checkout and use worktrees occasionally.
A variant: a hidden bare repository inside the project directory
Some people prefer to keep everything under one parent directory, with the bare repository hidden in a .bare folder:
git clone --bare https://example.com/team/myproject.git myproject/.bare
cd myproject
echo "gitdir: ./.bare" > .git
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
git worktree add main main
git worktree add feature-x -b feature-x mainThe file named .git contains a single line that points to the bare repository. Git follows it, so Git commands work from myproject/ itself, and the worktrees sit inside that directory. The result is one tidy folder per project:
myproject/
├── .bare/
├── .git (file: "gitdir: ./.bare")
├── main/
└── feature-x/The drawback is that myproject/ is neither a bare repository nor a working tree, so git status there fails with an error. Run status inside a worktree. Also keep the .git file out of the way of tools that scan parent directories for repositories.
Gotchas and edge cases
- Running working-tree commands in the bare directory.
git status,git addandgit commitfail in the bare repository with a message about needing a work tree. Change into a worktree first. - Forgetting the fetch refspec. The symptom is that
origin/maindoes not exist, even though you rangit fetch. Check withgit config --get-all remote.origin.fetch. If it prints nothing, set it as shown above. - Moving the bare repository. Each worktree stores the absolute path of its administrative directory under the bare repository. If you move
myproject.git, the worktrees lose their link. Rungit worktree repairfrom the moved bare repository, passing the new worktree paths if needed, as described in the chapter on moving and repairing worktrees. - Deleting a worktree directory by hand. The entry stays in the bare repository's metadata until you run
git worktree prune. This is the same cleanup as in any layout. - Dependencies and untracked files. Each worktree is a separate directory, so each needs its own
node_modules, virtual environment, build output and local config files. Nothing is copied from the other directories, and no directory is the "original" to copy from. - Default branch changes. The bare repository's
HEADrecords the default branch. If the remote renames its default branch, update it withgit symbolic-ref HEAD refs/heads/<new-name>inside the bare repository.
Putting it together
- Clone with
--bareinto a directory such asmyproject.git. - Set
remote.origin.fetchand rungit fetch origin. - Create one worktree per long-lived branch, such as
main. - Create a worktree with
-bfor every new task, starting fromorigin/main. - Remove the worktree and delete the branch when the task is merged.
Exercise
You cloned a repository with git clone --bare and added a worktree for main. You run git fetch origin but git branch -r shows nothing, and git worktree add ../topic -b topic origin/main fails with "invalid reference". What is wrong, and what two commands fix it?
Show answer
A bare clone does not set the fetch refspec, so git fetch never creates remote-tracking branches like origin/main. Run git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*", then git fetch origin. After that, origin/main exists and the git worktree add command works.
Exercise
In a bare layout, why does git worktree add ../main main succeed even though the bare repository's HEAD points at main?
Show answer
The bare repository has no working directory, so it does not count as a checkout of any branch. The one-branch-one-worktree rule applies only to real worktrees. No other worktree has main checked out, so the command succeeds.
Exercise
You finished the task in ../feature-x. Which two commands remove the directory and the branch, and which one deletes the metadata for the worktree?
Show answer
git worktree remove ../feature-x deletes the directory and its administrative metadata. git branch -d feature-x then deletes the merged branch, because removing a worktree does not delete its branch. git worktree prune is needed only if the directory was deleted by hand.