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
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.