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

Re: bugreport: "git checkout -B" allows checking out one branch across multiple worktrees

From
Carlo Marcelo Arenas Belón <carenas@gmail.com>
Date
Jan 16, 2023, 00:11 UTC
Message-ID
<20230116001144.dt76xk6hkwn45klz@Carlos-MacBook-Pro-2.local>
In-Reply-To
<CAA3Q-aaO=vcZd9VLFr8UP-g06be80eUWN_GjygfyGkYmrLx9yQ@mail.gmail.com>
On Sat, Jan 14, 2023 at 06:45:34PM +0900, Jinwook Jeong wrote:
Show 8 quoted lines
> What did you do before the bug happened? (Steps to reproduce your issue)
> 
> 1. `cd` into any git repo that has at least one commit.
> 2. Identify the current branch, say main
> 3. $ git branch foo # a new branch
> 4. $ git worktree add ../new_worktree foo
> 5. $ cd ../new_worktree
> 6. $ git checkout -B master HEAD

Was your intention to get this worktree's content back to what is in master's HEAD?, then the command should had been

$ git reset --hard master

The documentation might be confusing, but you most likely do NOT want to use -B unless you want to force things, but the lowercase version `-b`

Show 13 quoted lines
> Anything else you want to add:
> 
> https://www.git-scm.com/docs/git-checkout#Documentation/git-checkout.txt-emgitcheckoutem-b-Bltnew-branchgtltstart-pointgt
> 
> According to the documentation, "git checkout -B BRANCH START" is the
> transactionally equivalent of:
> 
>   git branch -f BRANCH START
>   git checkout BRANCH
> 
> When I ran the first command in place of the step 6 of the above
> reproducing procedure, git refused to carry on;
> I suppose that this is the intended behavior for "git checkout -B".

I think you are correct, and this is therefore a bug, but there is also a reason why `--force` allows doing dangerous things and I am not sure if it might apply here.

Carlo
Previous: Jinwook JeongNext: Eric Sunshine
Message 2 of 4 in “bugreport: "git checkout -B" allows checking out one branch across multiple worktrees”
  1. Jinwook JeongJan 14, 2023
  2. Carlo Marcelo Arenas BelónJan 16, 2023
  3. Eric SunshineJan 16, 2023
  4. Jinwook JeongJan 16, 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.