Sub topic 5
New branches, detached HEAD and choosing a starting point
This chapter assumes you can already create a linked worktree with git worktree add and know that a branch can be checked out in only one worktree at a time. It also assumes you know what a remote-tracking branch is and how git fetch updates it. Here you will decide what a new worktree is attached to (an existing branch, a new branch, or a bare commit) and which commit it starts from.
The three things a new worktree can point at
Every git worktree add call answers two questions:
- What will
HEADbe in the new directory: a branch or a detached commit? - Which commit does the working tree start from?
Git infers the answers from the arguments you give. The table shows the common forms.
| Command form | HEAD in the new worktree | Starting commit |
|---|---|---|
| `git worktree add <path> <branch>` | The existing local branch | Tip of that branch |
| `git worktree add -b <new> <path> [<start>]` | A new branch named `<new>` | `<start>`, or the current `HEAD` if omitted |
| `git worktree add -B <name> <path> [<start>]` | The branch `<name>`, created or reset | `<start>`, or the current `HEAD` if omitted |
| `git worktree add --detach <path> [<start>]` | Detached | `<start>`, or the current `HEAD` if omitted |
| `git worktree add <path> <tag-or-remote-ref>` | Detached, because the ref is not a local branch | The commit the ref names |
| `git worktree add <path>` | A branch named after the last path component | The current `HEAD`, if that branch does not exist yet |
The diagram shows how the choice works in practice.
Creating a new branch with -b
The most common form creates a branch and its worktree in one step:
git worktree add -b hotfix/login-crash ../myproject-hotfix mainThe last argument, main, is the starting point. Git creates hotfix/login-crash at the commit main points to, then checks it out in ../myproject-hotfix. The order matters: -b <branch> comes before the path, and the starting point comes after the path.
If you leave out the starting point, Git uses the HEAD of the directory where you run the command. That is not always what you want:
# Run from a worktree that is on feature/search:
git worktree add -b spike/ranking ../myproject-spikeThis creates spike/ranking at the tip of feature/search, not at main. That is useful when you want to branch off your current work. It is a mistake if you wanted a clean start, so name the starting point explicitly whenever it matters.
Starting from a remote-tracking branch
A starting point can be a remote-tracking branch. Fetch first, because Git uses whatever your local copy of the ref says:
git fetch origin
git worktree add -b feature/search ../myproject-feature origin/feature/searchWhen the starting point is a remote-tracking branch, Git sets the new local branch to track it by default (this is the branch.autoSetupMerge setting). You can confirm it:
git -C ../myproject-feature status -sbThe first line of the output looks like ## feature/search...origin/feature/search. That means a plain git pull or git push in that worktree knows which remote branch to use.
If the starting point is a local branch such as main, no tracking is set up by default. Add --track to track it, or --no-track to prevent tracking when the starting point is a remote-tracking branch.
Letting Git guess the branch
Git can create the branch from just the path. If you omit both -b and the starting point, Git names the branch after the last component of the path:
git worktree add ../login-fixGit behaves as if you had run git worktree add -b login-fix ../login-fix HEAD. If a branch called login-fix already exists and is free, Git checks it out instead. If login-fix does not exist locally but exactly one remote has a branch of that name, Git can create a local branch from it. The --guess-remote option and the worktree.guessRemote setting control that last behavior.
This shortcut is convenient but makes directory names and branch names depend on each other. A branch named hotfix/login-crash cannot come from a path like this, because the last path component would be only login-crash. For anything other than a throwaway, use -b explicitly.
Resetting a branch with -B
-B is like -b, except it also succeeds when the branch already exists. In that case Git moves the branch to the starting point:
git worktree add -B release/2.4 ../myproject-release origin/release/2.4If release/2.4 did not exist, it is created. If it did exist, it is reset to origin/release/2.4, and any local commits that were only on the old branch tip are no longer reachable from the branch. They stay in the reflog for a while, but treat -B as a destructive option. Use it when you deliberately want a local branch to match a remote one. Use -b when you expect the branch to be new, because -b fails loudly if the name is taken.
Detached HEAD worktrees
A detached worktree has no branch. Its HEAD points straight at a commit. Create one with --detach:
git fetch origin
git worktree add --detach ../myproject-review origin/feature/searchThis is the right tool for jobs where you only look at code: reviewing a pull request, running the test suite on a release tag, or bisecting. Detached worktrees avoid the exclusivity rule from the previous chapter. Any number of them can sit on the same commit, and none of them blocks a branch from being checked out elsewhere.
You can check whether a worktree is detached in two ways:
git -C ../myproject-review branch --show-current
git -C ../myproject-review statusThe first command prints nothing for a detached HEAD. The second prints a line such as HEAD detached at 3f9a1c2. In git worktree list, the same worktree shows (detached HEAD) where a branch name would appear.
Tags and other non-branch references are detached automatically. These two commands are equivalent:
git worktree add ../myproject-v1 v1.2.0
git worktree add --detach ../myproject-v1 v1.2.0Passing --detach makes your intent obvious to anyone reading a script, and it protects you if a branch with the same name as the ref ever appears.
Commits made on a detached HEAD
You can commit in a detached worktree, but those commits belong to no branch. Once you check out something else or remove the worktree, they can only be found through the reflog, and Git eventually discards them during garbage collection. If you decide to keep your work, give it a branch from inside the worktree:
cd ../myproject-review
git switch -c review/fixupsThis creates the branch at the current commit and attaches HEAD to it. Nothing in the working tree changes. The new branch is now subject to the one-branch-per-worktree rule like any other.
Choosing a starting point
The starting point is any commit-ish: a branch, a remote-tracking branch, a tag, a commit hash, or an expression like HEAD~3 or main@{yesterday}. Pick it by asking what you want the new work to be based on.
- A fix for released code: the release tag or maintenance branch, for example
v2.3.1ororigin/release/2.3. - A fix for the next release:
origin/main, after a fetch. - A feature that builds on unmerged work: the branch it depends on, so that its commits are included.
- A review or test run: the exact commit under test, detached.
Prefer the remote-tracking form (origin/main) over the local branch (main) when you want the latest shared state. A local main that you have not updated for two weeks is a quiet source of fixes based on old code. Fetching first and branching from origin/main avoids switching to main and pulling just to get a starting point.
The following script combines these practices. It creates a hotfix worktree from the current upstream state of main and reports what it did:
#!/bin/sh
set -eu
branch="$1"
path="../$(basename "$(pwd)")-$(echo "$branch" | tr '/' '-')"
git fetch origin
git worktree add -b "$branch" "$path" origin/main
git -C "$path" log --oneline -1
echo "Worktree ready at $path on branch $branch"Run it as sh new-wt.sh hotfix/login-crash from the main repository. Because origin/main is a remote-tracking branch, the new branch tracks it. If you push the hotfix with a different remote branch name, set the upstream explicitly at push time with git push -u origin HEAD.
Common errors
| Message or symptom | Cause | Fix |
|---|---|---|
| `fatal: a branch named 'x' already exists` | You used `-b` with a name that exists | Use the branch without `-b`, pick another name, or use `-B` if resetting is intended |
| `fatal: 'x' is already checked out at '<path>'` | The branch is in use in another worktree | Use another branch, use `--detach`, or switch that other worktree to a different branch |
| `fatal: invalid reference: origin/x` | The remote-tracking branch does not exist locally | Run `git fetch origin` and check the name with `git branch -r` |
| New branch does not track anything | The starting point was a local branch | Add `--track`, or run `git branch --set-upstream-to=origin/x` in the worktree |
| Commits vanish after removing a worktree | They were made on a detached `HEAD` | Run `git switch -c <name>` before removing, or recover them from `git reflog` |
Exercise
You are in the main repository on a branch with unfinished work. You need a worktree for a fix based on the latest main from the server, and you want the branch to track origin/main. Write the commands. Then explain why git worktree add -b fix/typo ../fix would be the wrong command.
Show answer
Run git fetch origin, then git worktree add -b fix/typo ../fix origin/main. The fetch updates origin/main, and the remote-tracking starting point makes the new branch track it. Without a starting point, the command would branch from the current HEAD, which is your unfinished branch, so the fix would include unrelated commits and would not be based on the latest main.
Exercise
A colleague asks you to run the tests on commit 3f9a1c2 while you keep working in your current directory. Which command creates a suitable worktree, and what must you do if you make a commit there that you want to keep?
Show answer
Use git worktree add --detach ../test-3f9a1c2 3f9a1c2. A detached worktree needs no branch, so it cannot trigger the one-branch-per-worktree error. To keep a commit made there, run git switch -c <branch-name> inside that worktree before removing it, so the commit is reachable from a branch.