If you git init
or git clone <args>
you get a repository with one
worktree.[1]
A git init
repository will start with only the default
branch available, which is also checked out in the main worktree.
From there you can create however many branches you want. But there will
always be only one checked out branch at most (you could also be in
“detached head” mode where no branch is checked out).
So no, each branch does not have its own worktree; the repository
contains all the branches, and each work tree can have at most one
branch checked out at any given time.[3]
You always have a worktree when working on your code
Unless you are working in a “bare” repository, you are always working in
a worktree. A simple git init
will give you a worktree.
We haven’t used git worktree add
yet. And yet the answer to the
question falls out of basic repository management.
But it is most of the time either redundant or useless to talk about
“worktrees” unless you have more than one; you can just say “my
repository”.
Do you need more than one worktree?
Worktrees and branches are very different things. But they have one
thing in common: how many of them you have is completely up to you and
your workflow.
Say I’m making a repository for my own private notes. I will probably
just use the default branch. Why would I need more branches than the
default branch?
Same principle applies to worktrees. Do you need to:
- Work on multiple things at the same time without changing the working
tree each time (the working tree is unique to each worktree)[4]
- Maybe run a long process (like a test suite) on a commit while you
work on a branch in another worktree
- Perhaps other use-cases mentioned in
man git worktrees
If not then you might not ever need to use git-worktree(1).
More than one worktree
Finally, we can create one more worktree:
$ git worktree add -b quickfix ../wip
Preparing worktree (new branch 'quickfix')
HEAD is now at 7da80ac i
$ git worktree list
/home/kristoffer/programming/just-init 7da80ac [main]
/home/kristoffer/programming/wip 7da80ac [quickfix]
Two worktrees. How many branches do we have? We have at least two, but
we might as well have two thousand. Because we don’t have to create a
worktree for each branch that we have.
Worktree without a branch (detached head)
Just like your “main worktree”, the other (“linked” worktrees) don’t
have to have a branch checked out:
$ git worktree add --detach ../test
Preparing worktree (detached HEAD 7da80ac)
HEAD is now at 7da80ac i
This might seem like an academic point to make but I would say that it
is not: it can be very useful to check out whatever commit is at the tip
of your branch, run a test suite in a separate worktree, and then (while
the test suite is being run) continue hacking on that branch in your
original (probably main) worktree.[5]
In fact this is the approach recommended when working with the cool
program git-test
by Michael Haggerty:
git test
works really well together with git worktree
. Keep a second worktree and use it for testing your current branch continuously as you work:
git worktree add --detach ../test HEAD
cd ../test
git test run master..mybranch
Notes
-
$ mkdir just-init && cd just-init
$ git init
$ git worktree list
/home/kristoffer/programming/just-init 0000000 [main]
init.defaultBranch
if set; else master
And a branch cannot be checked out in two different worktrees at the
same time.
You in effect switch to using cd
instead of git checkout
or git switch
Recall that any given branch cannot be checked out in multiple
worktrees at the same time. So we have to “detach”.
Source