Skip to content

Commit cbdf60f

Browse files
sunshinecogitster
authored andcommitted
worktree: add -b/-B options
One of git-worktree's roles is to populate the new worktree, much like git-checkout, and thus, for convenience, ought to support several of the same shortcuts. Toward this goal, add -b/-B options to create a new branch and check it out in the new worktree. (For brevity, only -b is mentioned in the synopsis; -B is omitted.) Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 39ecb27 commit cbdf60f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

Documentation/git-worktree.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-worktree - Manage multiple worktrees
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git worktree add' [-f] [--detach] <path> <branch>
12+
'git worktree add' [-f] [--detach] [-b <new-branch>] <path> <branch>
1313
'git worktree prune' [-n] [-v] [--expire <expire>]
1414

1515
DESCRIPTION
@@ -64,6 +64,14 @@ OPTIONS
6464
is already checked out by another worktree. This option overrides
6565
that safeguard.
6666

67+
-b <new-branch>::
68+
-B <new-branch>::
69+
With `add`, create a new branch named `<new-branch>` starting at
70+
`<branch>`, and check out `<new-branch>` into the new worktree.
71+
By default, `-b` refuses to create a new branch if it already
72+
exists. `-B` overrides this safeguard, resetting `<new-branch>` to
73+
`<branch>`.
74+
6775
--detach::
6876
With `add`, detach HEAD in the new worktree. See "DETACHED HEAD" in
6977
linkgit:git-checkout[1].
@@ -133,8 +141,7 @@ make the emergency fix, remove it when done, and then resume your earlier
133141
refactoring session.
134142

135143
------------
136-
$ git branch emergency-fix master
137-
$ git worktree add ../temp emergency-fix
144+
$ git worktree add -b emergency-fix ../temp master
138145
$ pushd ../temp
139146
# ... hack hack hack ...
140147
$ git commit -a -m 'emergency fix for boss'

builtin/worktree.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,22 @@ static int add(int ac, const char **av, const char *prefix)
126126
{
127127
struct child_process c;
128128
int force = 0, detach = 0;
129+
const char *new_branch = NULL, *new_branch_force = NULL;
129130
const char *path, *branch;
130131
struct argv_array cmd = ARGV_ARRAY_INIT;
131132
struct option options[] = {
132133
OPT__FORCE(&force, N_("checkout <branch> even if already checked out in other worktree")),
134+
OPT_STRING('b', NULL, &new_branch, N_("branch"),
135+
N_("create a new branch")),
136+
OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
137+
N_("create or reset a branch")),
133138
OPT_BOOL(0, "detach", &detach, N_("detach HEAD at named commit")),
134139
OPT_END()
135140
};
136141

137142
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
143+
if (new_branch && new_branch_force)
144+
die(_("-b and -B are mutually exclusive"));
138145
if (ac != 2)
139146
usage_with_options(worktree_usage, options);
140147

@@ -145,6 +152,10 @@ static int add(int ac, const char **av, const char *prefix)
145152
argv_array_pushl(&cmd, "--to", path, NULL);
146153
if (force)
147154
argv_array_push(&cmd, "--ignore-other-worktrees");
155+
if (new_branch)
156+
argv_array_pushl(&cmd, "-b", new_branch, NULL);
157+
if (new_branch_force)
158+
argv_array_pushl(&cmd, "-B", new_branch_force, NULL);
148159
if (detach)
149160
argv_array_push(&cmd, "--detach");
150161
argv_array_push(&cmd, branch);

0 commit comments

Comments
 (0)