Skip to content

Commit 39ecb27

Browse files
sunshinecogitster
authored andcommitted
worktree: add --detach option
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 a --detach option to detach HEAD in the new worktree. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f432544 commit 39ecb27

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Documentation/git-worktree.txt

Lines changed: 5 additions & 1 deletion
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] <path> <branch>
12+
'git worktree add' [-f] [--detach] <path> <branch>
1313
'git worktree prune' [-n] [-v] [--expire <expire>]
1414

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

67+
--detach::
68+
With `add`, detach HEAD in the new worktree. See "DETACHED HEAD" in
69+
linkgit:git-checkout[1].
70+
6771
-n::
6872
--dry-run::
6973
With `prune`, do not remove anything; just report what it would

builtin/worktree.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ static int prune(int ac, const char **av, const char *prefix)
125125
static int add(int ac, const char **av, const char *prefix)
126126
{
127127
struct child_process c;
128-
int force = 0;
128+
int force = 0, detach = 0;
129129
const char *path, *branch;
130130
struct argv_array cmd = ARGV_ARRAY_INIT;
131131
struct option options[] = {
132132
OPT__FORCE(&force, N_("checkout <branch> even if already checked out in other worktree")),
133+
OPT_BOOL(0, "detach", &detach, N_("detach HEAD at named commit")),
133134
OPT_END()
134135
};
135136

@@ -144,6 +145,8 @@ static int add(int ac, const char **av, const char *prefix)
144145
argv_array_pushl(&cmd, "--to", path, NULL);
145146
if (force)
146147
argv_array_push(&cmd, "--ignore-other-worktrees");
148+
if (detach)
149+
argv_array_push(&cmd, "--detach");
147150
argv_array_push(&cmd, branch);
148151

149152
memset(&c, 0, sizeof(c));

0 commit comments

Comments
 (0)