From a07cc06fbeec8c07d5e3b75873f17ed56f47ac0a Mon Sep 17 00:00:00 2001 From: Yoichi NAKAYAMA Date: Wed, 5 Aug 2026 12:54:45 +0000 Subject: [PATCH] worktree add: shouldn't dwim if -b or -B is given 'git worktree add ' DWIMs to a remote-tracking branch when neither -b, -B, nor --detach is given. However, 'git worktree add -b ' can still DWIM , causing to be ignored. This is a regression introduced by 128e5496b3 (worktree add: extend DWIM to infer --orphan, 2023-05-17), which appeared in Git 2.42. Signed-off-by: Yoichi NAKAYAMA Signed-off-by: Junio C Hamano --- builtin/worktree.c | 20 +++++++++++--------- t/t2400-worktree-add.sh | 10 ++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index d21c43fde3..fd1f83715a 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -897,16 +897,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; + 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; + 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 58b4445cc4..fb6a02f749 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -621,6 +621,16 @@ test_expect_success '"add" dwims' ' ) ' +test_expect_success '"add" 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" dwims with checkout.defaultRemote' ' test_when_finished rm -rf repo_upstream repo_dwim foo && setup_remote_repo repo_upstream repo_dwim &&