Sub topic 5
New branches, detached HEAD and choosing a starting point
This chapter assumes you can commit, create branches and use git fetch. It also assumes you know what a remote-tracking branch is. It covers the three ways git worktree add can decide what a new worktree contains: a new branch, an existing branch, or a detached commit.
The three forms of `git worktree add`
The command takes a path and an optional "commit-ish". A commit-ish is anything Git can resolve to a commit, such as a branch name, a tag, a remote-tracking branch or a hash. The options you pick decide what happens to the branch.
| Form | Command | Result |
|---|---|---|
| New branch | `git worktree add -b NAME PATH START` | Creates `NAME` at `START` and checks it out |
| Existing branch | `git worktree add PATH NAME` | Checks out `NAME` |
| Detached HEAD | `git worktree add --detach PATH START` | Checks out the commit with no branch |
Creating a new branch with a starting point
Use -b when the work needs a new branch. The last argument is the starting point.
git worktree add ../myproject-hotfix -b hotfix/login-crash mainThis creates the directory ../myproject-hotfix. It also creates the branch hotfix/login-crash at the tip of main and checks it out there. Your current directory is not touched.
If you leave out the starting point, Git uses the commit that HEAD points to in the directory where you ran the command. That may be a feature branch you are halfway through, which is rarely what you want for a hotfix. Name the starting point explicitly.
If the branch name already exists, -b fails. Use -B only when you want to reset an existing branch to the starting point. That moves the branch, so check that its commits are merged or saved elsewhere first.
Checking out an existing branch
Leave out -b and name the branch as the last argument.
git worktree add ../myproject-feature feature/searchThis works only if feature/search is not checked out in another worktree. Git refuses otherwise, because two working directories moving the same branch would overwrite each other's commits. The previous chapter covered that rule and its error message.
Detached HEAD
A detached HEAD means the worktree is checked out at a specific commit and no branch points to it. New commits you make there belong to no branch. Git will not stop you from making them, but they become unreachable once you switch away, and garbage collection can eventually delete them.
Detached worktrees suit read-only work, such as reviewing a colleague's branch or reproducing a bug at a tagged release.
git fetch origin
git worktree add --detach ../myproject-review origin/feature/searchFetching first makes sure origin/feature/search is up to date. A remote-tracking branch is not a local branch, so the one-branch-per-worktree rule does not apply. You can review a branch while its owner, or you, have it checked out elsewhere.
If you decide to keep commits made in a detached worktree, create a branch from inside it:
git switch -c review/search-fixesChoosing a starting point
The starting point decides what history the new work builds on. These are common choices:
mainor another local branch. Fast, but it may be behind the remote.origin/main. Use it aftergit fetchto branch from the latest shared state, even if your localmainis stale.- A tag such as
v2.3.0. Use it to reproduce a bug or prepare a patch release. - A commit hash. Use it to examine the exact state where a regression appeared.
When you base a branch on a remote-tracking branch, Git sets it up to track that branch. In recent versions this is controlled by branch.autoSetupMerge, so check git status in the new worktree if upstream tracking matters to you.
Common mistakes
- Omitting the starting point and branching from whatever you currently have checked out.
- Branching from a stale local
mainand forgetting to rungit fetch. - Committing in a detached worktree and then deleting it, which leaves the commits reachable only through the reflog until they expire.
- Using
-Bon a branch that has unmerged work.
Exercise
You need a patch for release v2.3.0, and a colleague's branch fix/export is checked out in another worktree. Write one command that creates a new worktree at ../myproject-patch on a new branch patch/2.3.1 starting from the tag v2.3.0. Then write one command that opens fix/export for read-only review in ../myproject-look without breaking the one-branch-per-worktree rule.
Show answer
git worktree add ../myproject-patch -b patch/2.3.1 v2.3.0. For review, run git fetch origin, then git worktree add --detach ../myproject-look origin/fix/export. The detached checkout uses the remote-tracking branch, so no local branch is claimed twice.