Skip to content

Commit 0f4af3b

Browse files
sunshinecogitster
authored andcommitted
worktree: add: make -b/-B default to HEAD when <branch> is omitted
As a convenience, like "git branch" and "git checkout -b", make "git worktree add -b <newbranch> <path> <branch>" default to HEAD when <branch> is omitted. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f5682b2 commit 0f4af3b

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Documentation/git-worktree.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ OPTIONS
6868
-B <new-branch>::
6969
With `add`, create a new branch named `<new-branch>` starting at
7070
`<branch>`, and check out `<new-branch>` into the new worktree.
71+
If `<branch>` is omitted, it defaults to HEAD.
7172
By default, `-b` refuses to create a new branch if it already
7273
exists. `-B` overrides this safeguard, resetting `<new-branch>` to
7374
`<branch>`.

builtin/worktree.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,13 @@ static int add(int ac, const char **av, const char *prefix)
289289
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
290290
if (new_branch && new_branch_force)
291291
die(_("-b and -B are mutually exclusive"));
292-
if (ac != 2)
292+
if (ac < 1 || ac > 2)
293+
usage_with_options(worktree_usage, options);
294+
if (ac < 2 && !new_branch && !new_branch_force)
293295
usage_with_options(worktree_usage, options);
294296

295297
path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
296-
branch = av[1];
298+
branch = ac < 2 ? "HEAD" : av[1];
297299

298300
argv_array_push(&cmd, "checkout");
299301
if (force)

t/t2025-worktree-add.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,9 @@ test_expect_success '"add" from relative HEAD' '
140140
test_cmp expected actual
141141
'
142142

143+
test_expect_success '"add -b" with <branch> omitted' '
144+
git worktree add -b burble flornk &&
145+
test_cmp_rev HEAD burble
146+
'
147+
143148
test_done

0 commit comments

Comments
 (0)