Why does git worktree add create a branch, and can I delete it?

后端 未结 6 821
暖寄归人
暖寄归人 2021-02-12 12:08

I used git worktree add to create a new worktree. I noticed that is has created a new branch in the repo with the same name as the worktree. What is this branch for

6条回答
  •  清歌不尽
    2021-02-12 12:26

    git worktree will add a new branch if none are specified:

    If is omitted and neither -b nor -B nor --detach used, then, as a convenience, a new branch based at HEAD is created automatically, as if -b $(basename ) was specified.

    Since Git 2.17, you can delete that branch with git worktree remove.

    But, that same remove command also included:

    Unclean working trees or ones with submodules can be removed with --force.
    The main working tree cannot be removed.

    True... except --force was not fully implemented in Git 2.17.

    With Git 2.18 (Q2 2018), "git worktree remove" learned that "-f" is a shorthand for "--force" option, just like for "git worktree add".

    See commit d228eea (17 Apr 2018) by Stefan Beller (stefanbeller).
    Helped-by: Eric Sunshine (sunshineco).
    (Merged by Junio C Hamano -- gitster -- in commit 90186fa, 08 May 2018)

    worktree: accept -f as short for --force for removal

    Many commands support a "--force" option, frequently abbreviated as "-f".
    However, "git worktree remove"'s hand-rolled OPT_BOOL forgets to recognize the short form, despite git-worktree.txt documenting "-f" as supported.
    Replace OPT_BOOL with OPT__FORCE, which provides "-f" for free, and makes 'remove' consistent with 'add' option parsing (which also specifies the PARSE_OPT_NOCOMPLETE flag).

提交回复
热议问题