Skip to content

Commit 1d0fa89

Browse files
pcloudsgitster
authored andcommitted
checkout: add --ignore-other-wortrees
Noticed-by: Mark Levedahl <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 10f102b commit 1d0fa89

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Documentation/git-checkout.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
232232
specific files such as HEAD, index... See "MULTIPLE WORKING
233233
TREES" section for more information.
234234

235+
--ignore-other-worktrees::
236+
`git checkout` refuses when the wanted ref is already checked
237+
out by another worktree. This option makes it check the ref
238+
out anyway. In other words, the ref can be held by more than one
239+
worktree.
240+
235241
<branch>::
236242
Branch to checkout; if it refers to a branch (i.e., a name that,
237243
when prepended with "refs/heads/", is a valid ref), then that

builtin/checkout.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct checkout_opts {
3737
int writeout_stage;
3838
int overwrite_ignore;
3939
int ignore_skipworktree;
40+
int ignore_other_worktrees;
4041

4142
const char *new_branch;
4243
const char *new_branch_force;
@@ -1210,7 +1211,8 @@ static int parse_branchname_arg(int argc, const char **argv,
12101211
int flag;
12111212
char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
12121213
if (head_ref &&
1213-
(!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)))
1214+
(!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)) &&
1215+
!opts->ignore_other_worktrees)
12141216
check_linked_checkouts(new);
12151217
free(head_ref);
12161218
}
@@ -1341,6 +1343,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
13411343
N_("second guess 'git checkout no-such-branch'")),
13421344
OPT_FILENAME(0, "to", &opts.new_worktree,
13431345
N_("check a branch out in a separate working directory")),
1346+
OPT_BOOL(0, "ignore-other-worktrees", &opts.ignore_other_worktrees,
1347+
N_("do not check if another worktree is holding the given ref")),
13441348
OPT_END(),
13451349
};
13461350

t/t2025-checkout-to.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ test_expect_success 'die the same branch is already checked out' '
7979
)
8080
'
8181

82+
test_expect_success 'not die the same branch is already checked out' '
83+
(
84+
cd here &&
85+
git checkout --ignore-other-worktrees --to anothernewmaster newmaster
86+
)
87+
'
88+
8289
test_expect_success 'not die on re-checking out current branch' '
8390
(
8491
cd there &&

0 commit comments

Comments
 (0)