Git add working line from existing remote branch
I am new to Git. I searched a lot but nothing was found that exactly matches my case.
I have a personal WinForms application and there are 3 branches in my remote repo (master and two long branches):
master #the common features are here like Core, DAL,...
north #customized for A company (long-running)
razavi #customized for B company (long-running)
On my office PC, I add 2 worktree
for these branches north
and razavi
:
$ git worktree list
C:/Source/nis a6fb6e1 [master]
C:/Source/north ebc7670 [north]
C:/Source/razavi eed08a2 [razavi]
It's ok so far, I decide to work on this project from my home too, but on my home PC, when I try to add a worktree for these two branches, it gives me an error:
$git worktree add -b north ../north north
fatal: A branch named 'north' already exists.
I remove the switch -b
to avoid adding a new branch, but it doesn't work either.
How do I add worktree
from an existing branch that is not local but remote?
UPDATE (solution)
After working for a while, I found a solution to my problem, it was actually a missing --checkout
switch:
$ git worktree add --checkout ../north north
I'll put that in the answers too.
source to share
TL; DR: you probably wantedgit worktree add ../north north
First, a reminder (or information for others raising this question): git worktree add
wants to create a new working tree and at the same time make sure that this new work tree uses a different branch name from any other working tree. This is because, while each added work tree has its own index and HEAD
, the files HEAD
are traversed by shared base branch pointers in the shared repository. Having two different working trees with independent index objects, but the same base branch, leads to some tricky problems for users. Rather than trying to figure out how to deal with these problems - either educating programmers or providing tools to solve the problems - it git worktree
simply prohibits the situation entirely.
Hence, it is quite common to want to create a new branch name when creating a new tree. By definition, the new branch name is automatically different from any 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 natural enough: no one is surprised by this.
You are using git worktree add
in a way that is similar to git checkout -b
, except that validation occurs on the newly added tree. But you already have a branch named north
.
If this existing branch is north
not useful, you can delete it. Now you don't have a local named branch north
and you can create a new one.
If this existing branch north
is helpful, don't delete it! If it is already checked in some existing work tree, go to that tree tree and work on it. If it is not checked on any existing work tree, you can create a new work tree that posted it; you just need to avoid using the flag -b
(and the corresponding branch name):
git worktree add ../north north
Note that when you create a new branch, you don't need to repeat:
git worktree add -b newbranch ../path
will create a new working tree in ../path
and git checkout -b newbranch
populate it with. You only need the branch name if:
- you don't use
-b
and - the path argument does not end with the branch name.
For example, if you want to checkout an existing branch zorg
in a new work tree in the path ../zorg
, you can simply run:
git worktree add ../zorg
Here, since there is -b zorg
no final argument, Git calculates the branch name using the last part ../zorg
, which is simple of course zorg
, so it tries to check out from the existing branch zorg
into the new working tree.
source to share
In addition to "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 checkout an existing branch.
See commit f60a7b7 , commit 6427f87 , commit 2c27002 , commit d861d34 (24 Apr 2018) Thomas Gummer ( tgummerer
) .
Assistant: Eric Sunshine ( sunshineco
) .
(merger Junio ββWith Hamano - gitster
- in commit 10174da , 23 May 2018)
worktree: teach "
add
" to check existing branchesCurrently '
git worktree add <path>
' creates a new branch named after the basename of the default path.
If a branch with the same name already exists, the command refuses to do anything unless the <option is specified--force
.However, we can do a little better than that and check out the branch if it isn't checked out elsewhere.
This will help users who just want to check out an existing branch for a new working line and save a few keystrokes.Since the current behavior is just "
die()
" when a branch with the base pathname name already exists, there is no backtracking compatibility issue here.We will still "
die()
" if the branch is checked out on a different working line if no flag--force
is passed.
The documentation states :
$ git worktree add --track -b <branch> <path> <remote>/<branch>
If
<commit-ish>
omitted, and is neither-b
, nor-b
, nor--detach
is used, then as a convenience the new working line is linked to a branch<branch>
named after$(basename <path>)
.
- If it
<branch>
doesn't exist, a new HEAD-based branch is automatically created as if specified-b <branch>
.- If it
<branch>
exists, it will be checked on a new working line, unless it was deleted elsewhere , otherwise the command will refuse to create a working line (unless--force
b).
source to share
In addition to git worktree add --checkout
, Git 2.16 (Q1 2018) will offer another alternative:
The method " git worktree add
" determines which branch to create, from where and to place an order in the new working tree has been slightly updated.
See commit e92445a , commit 71d6682 ( Nov 29, 2017) and commit 4e85333 , commit e284e89 , commit c4738ae , commit 7c85a87 ( Nov 26, 2017) by Thomas Gummerer ( tgummerer
) .
(Merged by Junio ββC Hamano - gitster
- in commit 66d3f19 , Dec 19, 2017)
add
worktree.guessRemote
configworktree.guessRemote
Some users may need the
--guess-remote
default parameter--guess-remote
introduced in the previous commit so they don't need to enter it every time they create a new working tree.Add a configuration parameter
worktree.guessRemote
that allows users to customize the default behavior for themselves.
The documentation for git config now reads:
worktree.guessRemote::
With the help of
add
, if-b
neither branch argument-b
nor-b
neither--detach
, the command defaults to creating a new branch from HEAD.
Ifworktree.guessRemote
set to true,worktree add
tries to find a remote tracking branch whose name is uniquely the same as 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.
In fact, Git 2.21 (Q1 2019) clarifies the documentation for this option, which immediately appeared with "C add
", without explaining what the add
"git worktree" subcommand is.
See commit b4583d5 ( Dec 23, 2018) by Eric Sunshine ( sunshineco
) .
(Merged by Eric Sunshine - sunshineco
- in commit b4583d5 , Dec 28, 2018)
The documentation now says:
worktree.guessRemote
:If the branch is not specified and no
-b
no-b
no--detach
not used, thegit worktree add
default values for the creation of a new branch of the HEAD.
source to share