{"thread":{"id":"59089","subject":"bugreport: \"git checkout -B\" allows checking out one branch across multiple worktrees","startedAt":"2023-01-14T09:46:16Z","lastAt":"2023-01-16T02:50:38Z","messageCount":4,"participants":["Jinwook Jeong","Carlo Marcelo Arenas Belón","Eric Sunshine"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"470334","messageId":"CAA3Q-aaO=vcZd9VLFr8UP-g06be80eUWN_GjygfyGkYmrLx9yQ@mail.gmail.com","threadId":"59089","inReplyTo":null,"subject":"bugreport: \"git checkout -B\" allows checking out one branch across multiple worktrees","fromName":"Jinwook Jeong","fromEmail":"vustthat@gmail.com","sentAt":"2023-01-14T09:45:34Z","receivedAt":"2023-01-14T09:46:16Z","isPatch":false,"sender":{"key":"vustthat@gmail.com","avatar":null},"body":"What did you do before the bug happened? (Steps to reproduce your issue)\n\n1. `cd` into any git repo that has at least one commit.\n2. Identify the current branch, say main\n3. $ git branch foo # a new branch\n4. $ git worktree add ../new_worktree foo\n5. $ cd ../new_worktree\n6. $ git checkout -B master HEAD\n\n\nWhat did you expect to happen? (Expected behavior)\n\ngit prints out \"Fatal: 'main' is already checked out at ...\" (or\nsimilar message that informs me that I'm not allowed to doubly\ncheckout one branch)\n\n\nWhat happened instead? (Actual behavior)\n\nThe new_worktree successfully changes its current branch to master,\nwhich is already held by another worktree.\n\n\nAnything else you want to add:\n\nhttps://www.git-scm.com/docs/git-checkout#Documentation/git-checkout.txt-emgitcheckoutem-b-Bltnew-branchgtltstart-pointgt\n\nAccording to the documentation, \"git checkout -B BRANCH START\" is the\ntransactionally equivalent of:\n\n  git branch -f BRANCH START\n  git checkout BRANCH\n\nWhen I ran the first command in place of the step 6 of the above\nreproducing procedure, git refused to carry on;\nI suppose that this is the intended behavior for \"git checkout -B\".\n\n\n[System Info]\ngit version:\ngit version 2.39.0\ncpu: arm64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nfeature: fsmonitor--daemon\nuname: Darwin 21.6.0 Darwin Kernel Version 21.6.0: Sun Nov  6 23:31:13\nPST 2022; root:xnu-8020.240.14~1/RELEASE_ARM64_T6000 arm64\ncompiler info: clang: 14.0.0 (clang-1400.0.29.202)\nlibc info: no libc information available\n$SHELL (typically, interactive shell): /opt/homebrew/bin/bash\n\n\n[Enabled Hooks]\nnot run from a git repository - no hooks to show\n"},{"id":"470416","messageId":"20230116001144.dt76xk6hkwn45klz@Carlos-MacBook-Pro-2.local","threadId":"59089","inReplyTo":"CAA3Q-aaO=vcZd9VLFr8UP-g06be80eUWN_GjygfyGkYmrLx9yQ@mail.gmail.com","subject":"Re: bugreport: \"git checkout -B\" allows checking out one branch across multiple worktrees","fromName":"Carlo Marcelo Arenas Belón","fromEmail":"carenas@gmail.com","sentAt":"2023-01-16T00:11:44Z","receivedAt":"2023-01-16T00:11:52Z","isPatch":false,"sender":{"key":"carenas@gmail.com","avatar":"https://avatars.githubusercontent.com/u/76036?v=4"},"body":"On Sat, Jan 14, 2023 at 06:45:34PM +0900, Jinwook Jeong wrote:\n> What did you do before the bug happened? (Steps to reproduce your issue)\n> \n> 1. `cd` into any git repo that has at least one commit.\n> 2. Identify the current branch, say main\n> 3. $ git branch foo # a new branch\n> 4. $ git worktree add ../new_worktree foo\n> 5. $ cd ../new_worktree\n> 6. $ git checkout -B master HEAD\n\nWas your intention to get this worktree's content back to what is in\nmaster's HEAD?, then the command should had been\n\n$ git reset --hard master\n\nThe documentation might be confusing, but you most likely do NOT want\nto use -B unless you want to force things, but the lowercase version `-b`\n\n> Anything else you want to add:\n> \n> https://www.git-scm.com/docs/git-checkout#Documentation/git-checkout.txt-emgitcheckoutem-b-Bltnew-branchgtltstart-pointgt\n> \n> According to the documentation, \"git checkout -B BRANCH START\" is the\n> transactionally equivalent of:\n> \n>   git branch -f BRANCH START\n>   git checkout BRANCH\n> \n> When I ran the first command in place of the step 6 of the above\n> reproducing procedure, git refused to carry on;\n> I suppose that this is the intended behavior for \"git checkout -B\".\n\nI think you are correct, and this is therefore a bug, but there is also\na reason why `--force` allows doing dangerous things and I am not sure\nif it might apply here.\n\nCarlo\n"},{"id":"470418","messageId":"CAPig+cQc1+D9gH7BAC-r03bGKWx3a9jpPyLuP-ehH-X2P+fV6Q@mail.gmail.com","threadId":"59089","inReplyTo":"20230116001144.dt76xk6hkwn45klz@Carlos-MacBook-Pro-2.local","subject":"Re: bugreport: \"git checkout -B\" allows checking out one branch across multiple worktrees","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2023-01-16T01:07:13Z","receivedAt":"2023-01-16T01:07:32Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Jan 15, 2023 at 7:23 PM Carlo Marcelo Arenas Belón\n<carenas@gmail.com> wrote:\n> On Sat, Jan 14, 2023 at 06:45:34PM +0900, Jinwook Jeong wrote:\n> > 1. `cd` into any git repo that has at least one commit.\n> > 2. Identify the current branch, say main\n> > 3. $ git branch foo # a new branch\n> > 4. $ git worktree add ../new_worktree foo\n> > 5. $ cd ../new_worktree\n> > 6. $ git checkout -B master HEAD\n>\n> Was your intention to get this worktree's content back to what is in\n> master's HEAD?, then the command should had been\n>\n> $ git reset --hard master\n>\n> The documentation might be confusing, but you most likely do NOT want\n> to use -B unless you want to force things, but the lowercase version `-b`\n>\n> > Anything else you want to add:\n> >\n> > https://www.git-scm.com/docs/git-checkout#Documentation/git-checkout.txt-emgitcheckoutem-b-Bltnew-branchgtltstart-pointgt\n> >\n> > According to the documentation, \"git checkout -B BRANCH START\" is the\n> > transactionally equivalent of:\n> >\n> >   git branch -f BRANCH START\n> >   git checkout BRANCH\n> >\n> > When I ran the first command in place of the step 6 of the above\n> > reproducing procedure, git refused to carry on;\n> > I suppose that this is the intended behavior for \"git checkout -B\".\n>\n> I think you are correct, and this is therefore a bug, but there is also\n> a reason why `--force` allows doing dangerous things and I am not sure\n> if it might apply here.\n\nI'd say there's a bug in `git-switch/git-checkout -B` not performing\nthe same checks as `git branch -f`. As a result, it is possible to get\ninto a state in which the same branch is checked out in multiple\nworktrees, which is probably undesirable. I looked briefly through the\ncode but don't have the time presently to dig into it.\n"},{"id":"470420","messageId":"CAA3Q-aazO1ko4uBSUs5W_rjn8AP_=fc2jAYrMsaeMtpnm54ExA@mail.gmail.com","threadId":"59089","inReplyTo":"20230116001144.dt76xk6hkwn45klz@Carlos-MacBook-Pro-2.local","subject":"Re: bugreport: \"git checkout -B\" allows checking out one branch across multiple worktrees","fromName":"Jinwook Jeong","fromEmail":"vustthat@gmail.com","sentAt":"2023-01-16T02:50:10Z","receivedAt":"2023-01-16T02:50:38Z","isPatch":false,"sender":{"key":"vustthat@gmail.com","avatar":null},"body":"On Mon, Jan 16, 2023 at 9:11 AM Carlo Marcelo Arenas Belón\n<carenas@gmail.com> wrote:\n>\n> On Sat, Jan 14, 2023 at 06:45:34PM +0900, Jinwook Jeong wrote:\n> > What did you do before the bug happened? (Steps to reproduce your issue)\n> >\n> > 1. `cd` into any git repo that has at least one commit.\n> > 2. Identify the current branch, say main\n> > 3. $ git branch foo # a new branch\n> > 4. $ git worktree add ../new_worktree foo\n> > 5. $ cd ../new_worktree\n> > 6. $ git checkout -B master HEAD\n>\n> Was your intention to get this worktree's content back to what is in\n> master's HEAD?, then the command should had been\n\nSorry for confusion. These steps didn't reflect my real use case but\nrather are for demonstration.\n\nI encountered this issue by chance, not knowing how that command would\nwork under multiple worktrees.\n"}]}