From 908a32fb565c0888fb9d025cfeb68d4846e10fe6 Mon Sep 17 00:00:00 2001 From: Yoichi NAKAYAMA Date: Fri, 31 Jul 2026 20:12:15 +0900 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 in v2.42.0 (128e5496b325640f0a09cc1d5b1e346c069b410f). Signed-off-by: Yoichi NAKAYAMA --- builtin/worktree.c | 2 +- t/t2400-worktree-add.sh | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index 654d27c3e1ce99..3204afdb12a348 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -897,7 +897,7 @@ 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; diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index 87b926728ad8cf..9cbf84861d2b7f 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 foo && + 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_expect_success '"add" dwims with checkout.defaultRemote' ' test_when_finished rm -rf repo_upstream repo_dwim foo && setup_remote_repo repo_upstream repo_dwim &&