Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git add a worktree from existing remote branch

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?

like image 565
vaheeds Avatar asked Aug 03 '17 17:08

vaheeds


People also ask

How do I add Worktree?

add worktree.Add a config option worktree. guessRemote that allows users to configure the default behaviour for themselves. 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. If worktree.

What is Worktree in git?

A Git worktree is a linked copy of your Git repository, allowing you to have multiple branches checked out at a time. A worktree has a separate path from your main working copy, but it can be in a different state and on a different branch.

What is a git Worktree And why would you require to use multiple Worktrees?

A git repository can support multiple working trees, allowing you to check out more than one branch at a time. With git worktree add a new working tree is associated with the repository, along with additional metadata that differentiates that working tree from others in the same repository.

Can I work on 2 branches simultaneously?

You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.


2 Answers

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:

  1. you're not using -b, and
  2. the path argument does not end in the name of the branch.

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

like image 57
torek Avatar answered Oct 12 '22 01:10

torek


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 
like image 44
vaheeds Avatar answered Oct 12 '22 03:10

vaheeds