Thread (4 messages) 4 messages, 3 authors, 2023-01-16

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

From: Eric Sunshine <hidden>
Date: 2023-01-16 01:07:32

On Sun, Jan 15, 2023 at 7:23 PM Carlo Marcelo Arenas Belón
[off-list ref] wrote:
On Sat, Jan 14, 2023 at 06:45:34PM +0900, Jinwook Jeong wrote:
quoted
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`
quoted
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.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help