git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git checkout -B <branch> lets you checkout a branch that is already checked out in another worktree Inbox

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 23, 2023, 01:28 UTC
Message-ID
<xmqqjzq9cl70.fsf@gitster.g>
In-Reply-To
<CAGX9RpFMCVLQV7RbK2u9AabusvkZD+RZNv_UD=R00cSUrjutBg@mail.gmail.com>
Willem Verstraeten <willem.verstraeten@gmail.com> writes:
>     git checkout -b main #reports a fatal error, as expected

This is expected because "main" already exists, not because "main" is checked out elsewhere.

>     git checkout -f main origin/main #also reports a fatal error, as expected

This is expected because origin/main is taken as pathspec, and it is a request to checkout the paths that match the pathspec out of the named tree-ish (i.e., "main"), even when these paths have local changes, but you do not have paths that match "origin/main". The failure is not because "main" is checked out elsewhere.

A slight variant of the command
    git checkout -f -b main origin/main
still fails for the same reason as the first of your examples above.

It is a tangent, but I suspect this failure may be a bit unexpected. In this example, "-f"orce could be overriding the usual failure from "-b" to switch to a branch that already exists, but that is what "-B" does, and "-f -b" does not work as a synonym for "-B".

In any case, these example you marked "fail as expected" do fail as expected, but they fail for reasons that have nothing to do with the protection of branches that are used in other worktrees.

>     git checkout -B main origin/main # ----> this succeeds, which is
> unexpected <----
I agree this may be undesirable.

But it makes sort of sense, because "-B" is a forced form of "-b" (i.e., it tells git: even when "-b" would fail, take necessary measures to make it work), and we can view that it is part of "forcing" to override the protection over branches that are used elsewhere.

I guess we could change the behaviour so that
    git checkout -B <branch> [<start-point>]

fails when <branch> is an existing branch that is in use in another worktree, and allow "-f" to be used to override the safety, i.e.,

    git checkout -f -B <branch> [<start-point>]

would allow the <branch> to be repointed to <start-point> (or HEAD) even when it is used elsewhere.

Thoughts, whether they agree or disagree with what I just said, by other experienced contributors are very much welcome, before I can say "patches welcome" ;-).

Willem, thanks for raising the issue.
Previous: Willem VerstraetenNext: Junio C Hamano
Message 2 of 17 in “git checkout -B <branch> lets you checkout a branch that is already checked out in another worktree Inbox”
  1. Willem VerstraetenNov 22, 2023
  2. Junio C HamanoNov 23, 2023
  3. Junio C HamanoNov 23, 2023
  4. 2/2 checkout: forbid "-B <branch>" from touching a branch used elsewhereJunio C Hamano, Nov 23, 2023
  5. Phillip WoodNov 23, 2023
  6. Eric SunshineNov 23, 2023
  7. Junio C HamanoNov 24, 2023
  8. Junio C HamanoNov 27, 2023
  9. Jeff KingNov 27, 2023
  10. Phillip WoodNov 30, 2023
  11. Willem VerstraetenDec 4, 2023
  12. Eric SunshineDec 4, 2023
  13. Junio C HamanoDec 8, 2023
  14. Willem VerstraetenJan 30, 2024
  15. Junio C HamanoJan 30, 2024
  16. Andy KoppeNov 23, 2023
  17. Willem VerstraetenNov 23, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.