"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-branch a bit more friendly to new users, we only allow entering detached HEAD mode when --detach is given. "git switch-branch" must take a branch (unless you create a new branch, then of course switch-branch can take any commit-ish) Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx> --- builtin/checkout.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/builtin/checkout.c b/builtin/checkout.c index c7ae068d2c..fbfebba2d9 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -49,6 +49,7 @@ struct checkout_opts { int merge; int force; int force_detach; + int implicit_detach; int writeout_stage; int overwrite_ignore; int ignore_skipworktree; @@ -1241,6 +1242,13 @@ static int checkout_branch(struct checkout_opts *opts, !opts->force_detach) die(_("nothing to do")); + if (!opts->implicit_detach && + !opts->new_branch && + !opts->new_branch_force && + new_branch_info->name && + !new_branch_info->path) + die(_("a branch is expected, got %s"), new_branch_info->name); + if (new_branch_info->path && !opts->force_detach && !opts->new_branch && !opts->ignore_other_worktrees) { int flag; @@ -1485,6 +1493,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) opts.dwim_new_local_branch = 1; opts.switch_branch_doing_nothing_not_ok = 0; opts.accept_pathspec = 1; + opts.implicit_detach = 1; options = parse_options_dup(checkout_options); options = add_common_options(&opts, options); @@ -1513,6 +1522,7 @@ int cmd_switch_branch(int argc, const char **argv, const char *prefix) opts.dwim_new_local_branch = 1; opts.accept_pathspec = 0; opts.switch_branch_doing_nothing_not_ok = 1; + opts.implicit_detach = 0; options = parse_options_dup(switch_options); options = add_common_options(&opts, options); -- 2.20.0.rc1.380.g3eb999425c.dirty