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

4 messages from 2023-01-14 to 2023-01-16. Participants: Jinwook Jeong, Carlo Marcelo Arenas Belón, Eric Sunshine.
Thread: https://gitlist.dev/t/59089

## Jinwook Jeong, 2023-01-14 09:45

Subject: bugreport: "git checkout -B" allows checking out one branch across multiple worktrees
Message-ID: <CAA3Q-aaO=vcZd9VLFr8UP-g06be80eUWN_GjygfyGkYmrLx9yQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAA3Q-aaO%3DvcZd9VLFr8UP-g06be80eUWN_GjygfyGkYmrLx9yQ%40mail.gmail.com

```
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


What did you expect to happen? (Expected behavior)

git prints out "Fatal: 'main' is already checked out at ..." (or
similar message that informs me that I'm not allowed to doubly
checkout one branch)


What happened instead? (Actual behavior)

The new_worktree successfully changes its current branch to master,
which is already held by another worktree.


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


[System Info]
git version:
git version 2.39.0
cpu: arm64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
feature: fsmonitor--daemon
uname: Darwin 21.6.0 Darwin Kernel Version 21.6.0: Sun Nov  6 23:31:13
PST 2022; root:xnu-8020.240.14~1/RELEASE_ARM64_T6000 arm64
compiler info: clang: 14.0.0 (clang-1400.0.29.202)
libc info: no libc information available
$SHELL (typically, interactive shell): /opt/homebrew/bin/bash


[Enabled Hooks]
not run from a git repository - no hooks to show

```

## Carlo Marcelo Arenas Belón, 2023-01-16 00:11

Subject: Re: bugreport: "git checkout -B" allows checking out one branch across multiple worktrees
Message-ID: <20230116001144.dt76xk6hkwn45klz@Carlos-MacBook-Pro-2.local>
URL: https://gitlist.dev/e/20230116001144.dt76xk6hkwn45klz%40Carlos-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:
> 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`

> 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

```

## Eric Sunshine, 2023-01-16 01:07

Subject: Re: bugreport: "git checkout -B" allows checking out one branch across multiple worktrees
Message-ID: <CAPig+cQc1+D9gH7BAC-r03bGKWx3a9jpPyLuP-ehH-X2P+fV6Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQc1%2BD9gH7BAC-r03bGKWx3a9jpPyLuP-ehH-X2P%2BfV6Q%40mail.gmail.com
In-Reply-To: <20230116001144.dt76xk6hkwn45klz@Carlos-MacBook-Pro-2.local>

```
On Sun, Jan 15, 2023 at 7:23 PM Carlo Marcelo Arenas Belón
<carenas@gmail.com> wrote:
> On Sat, Jan 14, 2023 at 06:45:34PM +0900, Jinwook Jeong wrote:
> > 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`
>
> > 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.

I'd say there's a bug in `git-switch/git-checkout -B` not performing
the same checks as `git branch -f`. As a result, it is possible to get
into a state in which the same branch is checked out in multiple
worktrees, which is probably undesirable. I looked briefly through the
code but don't have the time presently to dig into it.

```

## Jinwook Jeong, 2023-01-16 02:50

Subject: Re: bugreport: "git checkout -B" allows checking out one branch across multiple worktrees
Message-ID: <CAA3Q-aazO1ko4uBSUs5W_rjn8AP_=fc2jAYrMsaeMtpnm54ExA@mail.gmail.com>
URL: https://gitlist.dev/e/CAA3Q-aazO1ko4uBSUs5W_rjn8AP_%3Dfc2jAYrMsaeMtpnm54ExA%40mail.gmail.com
In-Reply-To: <20230116001144.dt76xk6hkwn45klz@Carlos-MacBook-Pro-2.local>

```
On Mon, Jan 16, 2023 at 9:11 AM Carlo Marcelo Arenas Belón
<carenas@gmail.com> wrote:
>
> On Sat, Jan 14, 2023 at 06:45:34PM +0900, Jinwook Jeong wrote:
> > 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

Sorry for confusion. These steps didn't reflect my real use case but
rather are for demonstration.

I encountered this issue by chance, not knowing how that command would
work under multiple worktrees.

```
