Skip to content

Commit f432544

Browse files
sunshinecogitster
authored andcommitted
worktree: add --force option
By default, "git worktree add" refuses to create a new worktree when the requested branch is already checked out elsewhere. Add a --force option to override this safeguard. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fc56361 commit f432544

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Documentation/git-worktree.txt

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

1515
DESCRIPTION
@@ -58,6 +58,12 @@ Prune working tree information in $GIT_DIR/worktrees.
5858
OPTIONS
5959
-------
6060

61+
-f::
62+
--force::
63+
By default, `add` refuses to create a new worktree when `<branch>`
64+
is already checked out by another worktree. This option overrides
65+
that safeguard.
66+
6167
-n::
6268
--dry-run::
6369
With `prune`, do not remove anything; just report what it would

builtin/worktree.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "run-command.h"
77

88
static const char * const worktree_usage[] = {
9-
N_("git worktree add <path> <branch>"),
9+
N_("git worktree add [<options>] <path> <branch>"),
1010
N_("git worktree prune [<options>]"),
1111
NULL
1212
};
@@ -125,9 +125,11 @@ 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;
128129
const char *path, *branch;
129130
struct argv_array cmd = ARGV_ARRAY_INIT;
130131
struct option options[] = {
132+
OPT__FORCE(&force, N_("checkout <branch> even if already checked out in other worktree")),
131133
OPT_END()
132134
};
133135

@@ -140,6 +142,8 @@ static int add(int ac, const char **av, const char *prefix)
140142

141143
argv_array_push(&cmd, "checkout");
142144
argv_array_pushl(&cmd, "--to", path, NULL);
145+
if (force)
146+
argv_array_push(&cmd, "--ignore-other-worktrees");
143147
argv_array_push(&cmd, branch);
144148

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

0 commit comments

Comments
 (0)