Sub topic 11
A bare repository with one worktree per branch
This chapter assumes you can commit, branch and merge, and that you know what a remote-tracking branch is. You should also have used git worktree add at least once. Here you will set up a layout in which no directory is the "main" checkout.
Why a bare repository
A normal clone has a working directory and a .git directory inside it. When you add linked worktrees, that first directory is the main worktree. It holds the shared repository data, so you cannot delete it without breaking the others. Its branch is also checked out, so no other worktree can use it.
A bare repository has history and no working directory. It contains what .git normally holds: objects, refs and configuration. Because no branch is checked out in it, every branch is free to be checked out in a worktree of its own. All the worktrees are then equal siblings.
Setting it up
Clone with --bare, then add one worktree per branch you want to work on.
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 add checks out the existing branch main in ../main. The second creates the branch feature-x from main and checks it out in ../feature-x. The result looks like this:
projects/
myproject.git/ bare repository
main/ worktree on main
feature-x/ worktree on feature-xWork inside main/ or feature-x/ as you would in any clone. Run git status, git commit and so on from those directories. Rarely do you need to enter myproject.git/ again, except to add or remove worktrees.
Fetching in a bare clone
A bare clone made with --bare copies remote branches directly into local branches (refs/heads/*). It does not create refs/remotes/origin/* and it does not set up a fetch refspec. As a result, git fetch does not update remote-tracking branches the way you expect.
To get the usual behavior, set the refspec once and fetch:
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch originAfter this, origin/feature/search exists, and you can use it as a starting point:
git worktree add ../search -b feature/search origin/feature/searchBecause the bare clone already made local branches during cloning, those local branches are not updated by later fetches. Update them from inside the worktree that has them checked out, with git pull or git merge --ff-only origin/main.
Listing the layout
From any worktree, git worktree list shows the bare repository too:
git worktree list/home/sam/projects/myproject.git (bare)
/home/sam/projects/main 3f2a9c1 [main]
/home/sam/projects/feature-x 8b71d04 [feature-x]The (bare) entry is the central store. It has no commit or branch, because nothing is checked out there.
Comparing the two layouts
| Aspect | Normal clone plus linked worktrees | Bare repository plus worktrees |
|---|---|---|
| Main worktree | One directory is special | None, all are equal |
| Deleting a directory | The main one cannot be removed safely | Any worktree can be removed |
| Branch availability | Main worktree's branch is taken | No branch is taken by the store |
| Remote-tracking refs | Set up automatically | Need a fetch refspec |
| Setup effort | One `git clone` | Clone, config, then `add` per branch |
Common errors
- Running commands in the bare directory. Commands such as
git statusfail there with "this operation must be run in a work tree". Change into a worktree. - Missing
origin/*branches. Ifgit branch -ris empty, you skipped the fetch refspec. - Relative paths.
git worktree add ../main mainis relative to the directory you run it from. Run it from insidemyproject.git, as shown, so the siblings land beside it. - Branch already checked out. Each branch can be in only one worktree. Use
--detachfor a read-only look at a branch that is already in use.
When to choose this layout
Choose it when you switch among many branches daily and want every branch to be a peer directory. A normal clone suits you better if you mostly stay on one branch and add a worktree only occasionally. The extra setup in the bare layout pays off once you keep three or more branches open at once.
Exercise
You cloned with git clone --bare and ran git fetch origin, but git branch -r shows nothing, and git worktree add ../search -b search origin/search fails. What is missing, and what command fixes it?
Show answer
A bare clone does not set a fetch refspec, so fetch does not create remote-tracking branches under refs/remotes/origin/. Run git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" and then git fetch origin. After that origin/search exists and the worktree add command works.
Exercise
Why can you add a worktree for main from a bare repository, even though a normal clone's main worktree blocks other worktrees from using its own branch?
Show answer
A bare repository has no working directory and no checked-out branch. Since no worktree currently holds main, the one-branch-per-worktree rule is not violated when you check it out in a new directory.