diff options
authorEric Sunshine <>2015-07-06 17:30:53 (GMT)
committerJunio C Hamano <>2015-07-06 18:07:47 (GMT)
commitcbdf60fa183e3a2330849046846b400ef4a99c3b (patch)
parent39ecb27436296361371d3caadcc9bc4a4e0b556d (diff)
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 <> Signed-off-by: Junio C Hamano <>
2 files changed, 21 insertions, 3 deletions
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 231271b..f44cd78 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -9,7 +9,7 @@ git-worktree - Manage multiple worktrees
-'git worktree add' [-f] [--detach] <path> <branch>
+'git worktree add' [-f] [--detach] [-b <new-branch>] <path> <branch>
'git worktree prune' [-n] [-v] [--expire <expire>]
@@ -64,6 +64,14 @@ OPTIONS
is already checked out by another worktree. This option overrides
that safeguard.
+-b <new-branch>::
+-B <new-branch>::
+ With `add`, create a new branch named `<new-branch>` starting at
+ `<branch>`, and check out `<new-branch>` into the new worktree.
+ By default, `-b` refuses to create a new branch if it already
+ exists. `-B` overrides this safeguard, resetting `<new-branch>` to
+ `<branch>`.
With `add`, detach HEAD in the new worktree. See "DETACHED HEAD" in
@@ -133,8 +141,7 @@ make the emergency fix, remove it when done, and then resume your earlier
refactoring session.
-$ git branch emergency-fix master
-$ git worktree add ../temp emergency-fix
+$ git worktree add -b emergency-fix ../temp master
$ pushd ../temp
# ... hack hack hack ...
$ git commit -a -m 'emergency fix for boss'
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 9dc92b0..d6d0eee 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -126,15 +126,22 @@ static int add(int ac, const char **av, const char *prefix)
struct child_process c;
int force = 0, detach = 0;
+ const char *new_branch = NULL, *new_branch_force = NULL;
const char *path, *branch;
struct argv_array cmd = ARGV_ARRAY_INIT;
struct option options[] = {
OPT__FORCE(&force, N_("checkout <branch> even if already checked out in other worktree")),
+ OPT_STRING('b', NULL, &new_branch, N_("branch"),
+ N_("create a new branch")),
+ OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
+ N_("create or reset a branch")),
OPT_BOOL(0, "detach", &detach, N_("detach HEAD at named commit")),
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
+ if (new_branch && new_branch_force)
+ die(_("-b and -B are mutually exclusive"));
if (ac != 2)
usage_with_options(worktree_usage, options);
@@ -145,6 +152,10 @@ static int add(int ac, const char **av, const char *prefix)
argv_array_pushl(&cmd, "--to", path, NULL);
if (force)
argv_array_push(&cmd, "--ignore-other-worktrees");
+ if (new_branch)
+ argv_array_pushl(&cmd, "-b", new_branch, NULL);
+ if (new_branch_force)
+ argv_array_pushl(&cmd, "-B", new_branch_force, NULL);
if (detach)
argv_array_push(&cmd, "--detach");
argv_array_push(&cmd, branch);