From: "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Jacob Abel <jacobabel@nullpo.dev>,
Yoichi Nakayama <yoichi.nakayama@gmail.com>,
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>,
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Subject: [PATCH v2] worktree add: shouldn't dwim if -b or -B is given
Date: Wed, 05 Aug 2026 12:16:47 +0000 [thread overview]
Message-ID: <pull.2192.v2.git.1785932208004.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2192.git.1785852032626.gitgitgadget@gmail.com>
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
git worktree add <path> <branch> DWIMs <branch> to a
remote-tracking branch when neither -b, -B, nor --detach
is given.
However, git worktree add -b <new-branch> <path> <branch> can
still DWIM <branch>, causing <new-branch> to be ignored.
This is a regression introduced in v2.42.0
(128e5496b325640f0a09cc1d5b1e346c069b410f).
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
worktree add: shouldn't dwim if -b or -B is given
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2192%2Fyoichi%2Fworktree-add-should-not-dwim-with-b-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2192/yoichi/worktree-add-should-not-dwim-with-b-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2192
Range-diff vs v1:
1: 908a32fb56 ! 1: b00f6c2fa1 worktree add: shouldn't dwim if -b or -B is given
@@ Metadata
## Commit message ##
worktree add: shouldn't dwim if -b or -B is given
- 'git worktree add <path> <branch>' DWIMs <branch> to a
+ git worktree add <path> <branch> DWIMs <branch> to a
remote-tracking branch when neither -b, -B, nor --detach
is given.
- However, 'git worktree add -b <new-branch> <path> <branch>' can
+ However, git worktree add -b <new-branch> <path> <branch> can
still DWIM <branch>, causing <new-branch> to be ignored.
This is a regression introduced in v2.42.0
@@ Commit message
## builtin/worktree.c ##
@@ builtin/worktree.c: static int add(int ac, const char **av, const char *prefix,
-
/* DWIM: Infer --orphan when repo has no refs. */
opts.orphan = (!s) && dwim_orphan(&opts, !!opt_track, 1);
-- } else if (ac == 2) {
-+ } else if (ac == 2 && !new_branch) {
- struct object_id oid;
- struct commit *commit;
- char *remote;
+ } else if (ac == 2) {
+- struct object_id oid;
+- struct commit *commit;
+- char *remote;
+-
+- commit = lookup_commit_reference_by_name(branch);
+- if (!commit) {
+- remote = unique_tracking_name(branch, &oid, NULL);
+- if (remote) {
+- new_branch = branch;
+- branch = new_branch_to_free = remote;
++ if (!new_branch) {
++ struct object_id oid;
++ struct commit *commit;
++ char *remote;
++
++ commit = lookup_commit_reference_by_name(branch);
++ if (!commit) {
++ remote = unique_tracking_name(branch, &oid, NULL);
++ if (remote) {
++ new_branch = branch;
++ branch = new_branch_to_free = remote;
++ }
+ }
+ }
+
## t/t2400-worktree-add.sh ##
@@ t/t2400-worktree-add.sh: test_expect_success '"add" <path> <branch> dwims' '
@@ t/t2400-worktree-add.sh: test_expect_success '"add" <path> <branch> dwims' '
'
+test_expect_success '"add" <path> <branch> does not dwim with -b' '
-+ test_when_finished rm -rf repo_upstream repo_dwim foo &&
++ test_when_finished rm -rf repo_upstream repo_dwim wt &&
+ setup_remote_repo repo_upstream repo_dwim &&
-+ git init repo_dwim &&
+ (
+ cd repo_dwim &&
-+ test_must_fail git worktree add -b branch ../foo foo
++ test_must_fail git worktree add -b branch ../wt foo 2>actual &&
++ test_grep "^fatal: invalid reference: foo" actual
+ )
+'
+
builtin/worktree.c | 22 ++++++++++++----------
t/t2400-worktree-add.sh | 10 ++++++++++
2 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 654d27c3e1..cc46c1b415 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -898,16 +898,18 @@ static int add(int ac, const char **av, const char *prefix,
/* DWIM: Infer --orphan when repo has no refs. */
opts.orphan = (!s) && dwim_orphan(&opts, !!opt_track, 1);
} else if (ac == 2) {
- struct object_id oid;
- struct commit *commit;
- char *remote;
-
- commit = lookup_commit_reference_by_name(branch);
- if (!commit) {
- remote = unique_tracking_name(branch, &oid, NULL);
- if (remote) {
- new_branch = branch;
- branch = new_branch_to_free = remote;
+ if (!new_branch) {
+ struct object_id oid;
+ struct commit *commit;
+ char *remote;
+
+ commit = lookup_commit_reference_by_name(branch);
+ if (!commit) {
+ remote = unique_tracking_name(branch, &oid, NULL);
+ if (remote) {
+ new_branch = branch;
+ branch = new_branch_to_free = remote;
+ }
}
}
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index 87b926728a..ba3bec078f 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -621,6 +621,16 @@ test_expect_success '"add" <path> <branch> dwims' '
)
'
+test_expect_success '"add" <path> <branch> does not dwim with -b' '
+ test_when_finished rm -rf repo_upstream repo_dwim wt &&
+ setup_remote_repo repo_upstream repo_dwim &&
+ (
+ cd repo_dwim &&
+ test_must_fail git worktree add -b branch ../wt foo 2>actual &&
+ test_grep "^fatal: invalid reference: foo" actual
+ )
+'
+
test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
test_when_finished rm -rf repo_upstream repo_dwim foo &&
setup_remote_repo repo_upstream repo_dwim &&
base-commit: 5b2471720c93ee30e5764a19f3d3b3ae9ec9712a
--
gitgitgadget
next prev parent reply other threads:[~2026-08-05 12:16 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-04 14:00 [PATCH] worktree add: shouldn't dwim if -b or -B is given Yoichi NAKAYAMA via GitGitGadget
2026-08-04 17:11 ` Junio C Hamano
2026-08-05 10:57 ` Yoichi Nakayama
2026-08-05 12:16 ` Yoichi NAKAYAMA via GitGitGadget [this message]
2026-08-05 12:30 ` [PATCH v2] " Yoichi Nakayama
2026-08-05 12:54 ` [PATCH v3] " Yoichi NAKAYAMA via GitGitGadget
2026-08-05 16:58 ` Junio C Hamano
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=pull.2192.v2.git.1785932208004.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--cc=jacobabel@nullpo.dev \
--cc=yoichi.nakayama@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