git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Sunshine <sunshine@sunshineco.com>
To: "Carlo Marcelo Arenas Belón" <carenas@gmail.com>
Cc: Jinwook Jeong <vustthat@gmail.com>, git@vger.kernel.org
Subject: Re: bugreport: "git checkout -B" allows checking out one branch across multiple worktrees
Date: Sun, 15 Jan 2023 20:07:13 -0500	[thread overview]
Message-ID: <CAPig+cQc1+D9gH7BAC-r03bGKWx3a9jpPyLuP-ehH-X2P+fV6Q@mail.gmail.com> (raw)
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.

  reply	other threads:[~2023-01-16  1:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-14  9:45 bugreport: "git checkout -B" allows checking out one branch across multiple worktrees Jinwook Jeong
2023-01-16  0:11 ` Carlo Marcelo Arenas Belón
2023-01-16  1:07   ` Eric Sunshine [this message]
2023-01-16  2:50   ` Jinwook Jeong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAPig+cQc1+D9gH7BAC-r03bGKWx3a9jpPyLuP-ehH-X2P+fV6Q@mail.gmail.com \
    --to=sunshine@sunshineco.com \
    --cc=carenas@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=vustthat@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).