In my remote repository there are 3 branches (master and 2 long running branches):
master #the common features are here like Core, DAL,...
north #customized f
For this problem, worktree add
does need a --checkout
switch to do so:
$ git worktree add --checkout ../north north
$ git worktree add --checkout ../razavi razavi
TL;DR: you probably wanted git worktree add ../north north
First, a reminder (or information for others coming across this question): git worktree add
wants to create a new work-tree and, at the same time, make sure that this new work-tree is using a different branch name from every other work-tree. This is because, while each added work-tree has its own index and HEAD
, the HEAD
files wind up sharing the underlying branch pointers in the shared repository. Having two different work-trees with independent index objects but the same underlying branch leads to some tricky problems for users to deal with. Rather than trying to figure out how to deal with these—by either educating programmers or providing tools to deal with the problems—git worktree
simply forbids the situation entirely.
Hence, it's pretty typical to want to create a new branch name when creating a new work-tree. By definition, a new branch name is automatically different from every existing branch name:
$ git checkout -b newbranch
Switched to a new branch 'newbranch'
$ git checkout -b newbranch
fatal: A branch named 'newbranch' already exists.
This seems pretty natural: no one is ever surprised by this.
You're running git worktree add
in a way that is just like git checkout -b
, except that the checkout occurs in the new added work-tree. But you already have a branch named north
.
If this existing north
branch is not useful, you can delete it. Now you don't have a local branch named north
and you can create a new one.
If this existing north
branch is useful, don't delete it! If it's already checked out in some existing work-tree, move to that work-tree and work on it there. If it's not checked out in some existing work-tree, you can make a new work-tree that does have it checked out; you just need to avoid using the -b
flag (and the corresponding branch name):
git worktree add ../north north
Note that when you're creating a new branch, you do not have to repeat yourself:
git worktree add -b newbranch ../path
will create a new work-tree in ../path
, and use git checkout -b newbranch
to populate it. You only need the branch name when:
-b
, andFor instance, if you want to check out the existing branch zorg
in a new work-tree in path ../zorg
, you can just run:
git worktree add ../zorg
Here, since there is neither a -b zorg
nor a final argument, Git figures out the branch name by using the last part of ../zorg
, which is of course just zorg
, so this tries to check out the existing branch zorg
into the new work-tree.
In addition of "guessing the remote branch", as I explain in my other answer, Git 2.18 (Q2 2018) will offer a new feature:
"git worktree add
" learned to check out an existing branch.
See commit f60a7b7, commit 6427f87, commit 2c27002, commit d861d34 (24 Apr 2018) by Thomas Gummerer (tgummerer).
Helped-by: Eric Sunshine (sunshineco).
(Merged by Junio C Hamano -- gitster -- in commit 10174da, 23 May 2018)
worktree: teach "
add
" to check out existing branches
Currently '
git worktree add <path>
' creates a new branch named after the basename of the path by default.
If a branch with that name already exists, the command refuses to do anything, unless the '--force
' option is given.However we can do a little better than that, and check the branch out if it is not checked out anywhere else.
This will help users who just want to check an existing branch out into a new worktree, and save a few keystrokes.As the current behaviour is to simply '
die()
' when a branch with the name of the basename of the path already exists, there are no backwards compatibility worries here.We will still '
die()
' if the branch is checked out in another worktree, unless the--force
flag is passed.
The documentation now states:
$ git worktree add --track -b <branch> <path> <remote>/<branch>
If
<commit-ish>
is omitted and neither-b
nor-B
nor--detach
used, then, as a convenience, the new worktree is associated with a branch (call it<branch>
) named after$(basename <path>)
.
- If
<branch>
doesn't exist, a new branch based on HEAD is automatically created as if-b <branch>
was given.- If
<branch>
does exist, it will be checked out in the new worktree, if it's not checked out anywhere else, otherwise the command will refuse to create the worktree (unless--force
is used).
Git 2.30 (Q1 2021) fixes the formulation of an error message with two placeholders in "git worktree add"(man) subcommand.
See commit b86339b (20 Nov 2020) by Matheus Tavares (matheustavares).
(Merged by Junio C Hamano -- gitster -- in commit f73ee0c, 30 Nov 2020)
worktree: fix order of arguments in error message
Signed-off-by: Matheus Tavares
Reviewed-by: Eric Sunshine
git worktree add(man) (without
--force
) errors out when given a path that is already registered as a worktree and the path is missing on disk.
But thecmd
andpath
strings are switched on the error message.
Let's fix that.
This is about the error messages:
<path> is a missing but locked worktree
use '<cmd> -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear
Or:
<path> is a missing but already registered worktree
use '<cmd> -f' to override, or 'unlock' and 'prune' or 'remove' to clear
In addition of git worktree add --checkout
, Git 2.16 (Q1 2018) will propose another alternative:
The way "git worktree add
" determines what branch to create from where and checkout in the new worktree has been updated a bit.
See commit e92445a, commit 71d6682 (29 Nov 2017), and commit 4e85333, commit e284e89, commit c4738ae, commit 7c85a87 (26 Nov 2017) by Thomas Gummerer (tgummerer).
(Merged by Junio C Hamano -- gitster -- in commit 66d3f19, 19 Dec 2017)
add
worktree.guessRemote
config optionSome users might want to have the
--guess-remote
option introduced in the previous commit on by default, so they don't have to type it out every time they create a new worktree.Add a config option
worktree.guessRemote
that allows users to configure the default behaviour for themselves.
The documentation for git config now reads:
worktree.guessRemote::
With
add
, if no branch argument, and neither of-b
nor-B
nor--detach
are given, the command defaults to creating a new branch from HEAD.
Ifworktree.guessRemote
is set to true,worktree add
tries to find a remote-tracking branch whose name uniquely matches the new branch name.
- If such a branch exists, it is checked out and set as "upstream" for the new branch.
- If no such match can be found, it falls back to creating a new branch from the current HEAD.
Actually, Git 2.21 (Q1 2019) clarifies the documentation for this option, which jumped right in with "With add
", without explaining that add
is a sub-command of "git worktree".
See commit b4583d5 (23 Dec 2018) by Eric Sunshine (sunshineco).
(Merged by Eric Sunshine -- sunshineco -- in commit b4583d5, 28 Dec 2018)
The documentation now reads:
worktree.guessRemote
:If no branch is specified and neither
-b
nor-B
nor--detach
is used, thengit worktree add
defaults to creating a new branch from HEAD.