Git add a worktree from existing remote branch

前端 未结 4 751
执笔经年
执笔经年 2021-02-03 21:33

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         


        
4条回答
  •  没有蜡笔的小新
    2021-02-03 21:50

    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 ' 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   /
    

    If is omitted and neither -b nor -B nor --detach used, then, as a convenience, the new worktree is associated with a branch (call it ) named after $(basename ).

    • If doesn't exist, a new branch based on HEAD is automatically created as if -b was given.
    • If 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 the cmd and path strings are switched on the error message.
    Let's fix that.

    This is about the error messages:

     is a missing but locked worktree
    use ' -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear
    

    Or:

     is a missing but already registered worktree
    use ' -f' to override, or 'unlock' and 'prune' or 'remove' to clear
    

提交回复
热议问题