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 for A company (long-running)
razavi #customized for B company (long-running)
At my office PC, I add 2 worktree
for those north
and razavi
branches:
$ git worktree list
C:/Source/nis a6fb6e1 [master]
C:/Source/north ebc7670 [north]
C:/Source/razavi eed08a2 [razavi]
Everything is OK so far, I decide to work on this project from my home as well, but in my home PC, when I try to add worktree for those two branches, it gives me an error:
$git worktree add -b north ../north north
fatal: A branch named 'north' already exists.
I remove the -b
switch to not add a new branch, but it doesn't work too.
How can I add a worktree
from existing branch that is not local but remote?
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.