Skip to content

Commit 7968bef

Browse files
pcloudsgitster
authored andcommitted
switch: only allow explicit detached HEAD
"git checkout <commit>" will checkout the commit in question and detach HEAD from the current branch. It is naturally a right thing to do once you get git references. But detached HEAD is a scary concept to new users because we show a lot of warnings and stuff, and it could be hard to get out of (until you know better). To keep switch a bit more friendly to new users, we only allow entering detached HEAD mode when --detach is given. "git switch" must take a branch (unless you create a new branch, then of course switch can take any commit-ish) Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e342c63 commit 7968bef

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

builtin/checkout.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct checkout_opts {
4545
int merge;
4646
int force;
4747
int force_detach;
48+
int implicit_detach;
4849
int writeout_stage;
4950
int overwrite_ignore;
5051
int ignore_skipworktree;
@@ -1298,6 +1299,29 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
12981299
return status;
12991300
}
13001301

1302+
static void die_expecting_a_branch(const struct branch_info *branch_info)
1303+
{
1304+
struct object_id oid;
1305+
char *to_free;
1306+
1307+
if (dwim_ref(branch_info->name, strlen(branch_info->name), &oid, &to_free) == 1) {
1308+
const char *ref = to_free;
1309+
1310+
if (skip_prefix(ref, "refs/tags/", &ref))
1311+
die(_("a branch is expected, got tag '%s'"), ref);
1312+
if (skip_prefix(ref, "refs/remotes/", &ref))
1313+
die(_("a branch is expected, got remote branch '%s'"), ref);
1314+
die(_("a branch is expected, got '%s'"), ref);
1315+
}
1316+
if (branch_info->commit)
1317+
die(_("a branch is expected, got commit '%s'"), branch_info->name);
1318+
/*
1319+
* This case should never happen because we already die() on
1320+
* non-commit, but just in case.
1321+
*/
1322+
die(_("a branch is expected, got '%s'"), branch_info->name);
1323+
}
1324+
13011325
static int checkout_branch(struct checkout_opts *opts,
13021326
struct branch_info *new_branch_info)
13031327
{
@@ -1345,6 +1369,14 @@ static int checkout_branch(struct checkout_opts *opts,
13451369
!opts->force_detach)
13461370
die(_("missing branch or commit argument"));
13471371

1372+
if (!opts->implicit_detach &&
1373+
!opts->force_detach &&
1374+
!opts->new_branch &&
1375+
!opts->new_branch_force &&
1376+
new_branch_info->name &&
1377+
!new_branch_info->path)
1378+
die_expecting_a_branch(new_branch_info);
1379+
13481380
if (new_branch_info->path && !opts->force_detach && !opts->new_branch &&
13491381
!opts->ignore_other_worktrees) {
13501382
int flag;
@@ -1602,6 +1634,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
16021634
opts.no_dwim_new_local_branch = 0;
16031635
opts.switch_branch_doing_nothing_is_ok = 1;
16041636
opts.accept_pathspec = 1;
1637+
opts.implicit_detach = 1;
16051638

16061639
options = parse_options_dup(checkout_options);
16071640
options = add_common_options(&opts, options);
@@ -1633,6 +1666,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
16331666
opts.no_dwim_new_local_branch = 0;
16341667
opts.accept_pathspec = 0;
16351668
opts.switch_branch_doing_nothing_is_ok = 0;
1669+
opts.implicit_detach = 0;
16361670

16371671
options = parse_options_dup(switch_options);
16381672
options = add_common_options(&opts, options);

0 commit comments

Comments
 (0)