Skip to content

Commit ccb111b

Browse files
pcloudsgitster
authored andcommitted
switch: implicit dwim, use --no-guess to disable it
This is already the default in git-checkout. The real change in here is just minor cleanup. The main excuse is to explain why dwim is kept default. Contrary to detach mode that is easy to get into and confusing to get back out. Automatically creating a tracking branch often does not kick in as often (you would need a branch of the same name on a remote). And since the branch creation is reported clearly, the user should be able to undo/delete it if it's unwanted. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 163e3b2 commit ccb111b

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

Documentation/git-checkout.txt

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,13 @@ branch.
3131
`<branch>`.
3232
+
3333
If `<branch>` is not found but there does exist a tracking branch in
34-
exactly one remote (call it `<remote>`) with a matching name, treat as
35-
equivalent to
34+
exactly one remote (call it `<remote>`) with a matching name and
35+
`--no-guess` is not specified, treat as equivalent to
3636
+
3737
------------
3838
$ git checkout -b <branch> --track <remote>/<branch>
3939
------------
4040
+
41-
If the branch exists in multiple remotes and one of them is named by
42-
the `checkout.defaultRemote` configuration variable, we'll use that
43-
one for the purposes of disambiguation, even if the `<branch>` isn't
44-
unique across all remotes. Set it to
45-
e.g. `checkout.defaultRemote=origin` to always checkout remote
46-
branches from there if `<branch>` is ambiguous but exists on the
47-
'origin' remote. See also `checkout.defaultRemote` in
48-
linkgit:git-config[1].
49-
+
5041
You could omit `<branch>`, in which case the command degenerates to
5142
"check out the current branch", which is a glorified no-op with
5243
rather expensive side-effects to show only the tracking information,
@@ -183,6 +174,27 @@ explicitly give a name with `-b` in such a case.
183174
Do not set up "upstream" configuration, even if the
184175
`branch.autoSetupMerge` configuration variable is true.
185176

177+
--guess::
178+
--no-guess::
179+
If `<branch>` is not found but there does exist a tracking
180+
branch in exactly one remote (call it `<remote>`) with a
181+
matching name, treat as equivalent to
182+
+
183+
------------
184+
$ git checkout -b <branch> --track <remote>/<branch>
185+
------------
186+
+
187+
If the branch exists in multiple remotes and one of them is named by
188+
the `checkout.defaultRemote` configuration variable, we'll use that
189+
one for the purposes of disambiguation, even if the `<branch>` isn't
190+
unique across all remotes. Set it to
191+
e.g. `checkout.defaultRemote=origin` to always checkout remote
192+
branches from there if `<branch>` is ambiguous but exists on the
193+
'origin' remote. See also `checkout.defaultRemote` in
194+
linkgit:git-config[1].
195+
+
196+
Use `--no-guess` to disable this.
197+
186198
-l::
187199
Create the new branch's reflog; see linkgit:git-branch[1] for
188200
details.
@@ -287,10 +299,6 @@ Note that this option uses the no overlay mode by default (see also
287299
Just like linkgit:git-submodule[1], this will detach `HEAD` of the
288300
submodule.
289301

290-
--no-guess::
291-
Do not attempt to create a branch if a remote tracking branch
292-
of the same name exists.
293-
294302
--overlay::
295303
--no-overlay::
296304
In the default overlay mode, `git checkout` never

builtin/checkout.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct checkout_opts {
5353
int show_progress;
5454
int count_checkout_paths;
5555
int overlay_mode;
56-
int no_dwim_new_local_branch;
56+
int dwim_new_local_branch;
5757
int discard_changes;
5858
int accept_pathspec;
5959
int switch_branch_doing_nothing_is_ok;
@@ -1430,8 +1430,6 @@ static struct option *add_common_switch_branch_options(
14301430
OPT_BOOL_F(0, "overwrite-ignore", &opts->overwrite_ignore,
14311431
N_("update ignored files (default)"),
14321432
PARSE_OPT_NOCOMPLETE),
1433-
OPT_BOOL(0, "no-guess", &opts->no_dwim_new_local_branch,
1434-
N_("second guess 'git checkout <no-such-branch>'")),
14351433
OPT_BOOL(0, "ignore-other-worktrees", &opts->ignore_other_worktrees,
14361434
N_("do not check if another worktree is holding the given ref")),
14371435
OPT_END()
@@ -1468,7 +1466,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
14681466
{
14691467
struct branch_info new_branch_info;
14701468
int dwim_remotes_matched = 0;
1471-
int dwim_new_local_branch;
14721469

14731470
memset(&new_branch_info, 0, sizeof(new_branch_info));
14741471
opts->overwrite_ignore = 1;
@@ -1483,7 +1480,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
14831480
argc = parse_options(argc, argv, prefix, options, usagestr,
14841481
PARSE_OPT_KEEP_DASHDASH);
14851482

1486-
dwim_new_local_branch = !opts->no_dwim_new_local_branch;
14871483
if (opts->show_progress < 0) {
14881484
if (opts->quiet)
14891485
opts->show_progress = 0;
@@ -1545,7 +1541,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
15451541
struct object_id rev;
15461542
int dwim_ok =
15471543
!opts->patch_mode &&
1548-
dwim_new_local_branch &&
1544+
opts->dwim_new_local_branch &&
15491545
opts->track == BRANCH_TRACK_UNSPECIFIED &&
15501546
!opts->new_branch;
15511547
int n = parse_branchname_arg(argc, argv, dwim_ok,
@@ -1626,12 +1622,14 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
16261622
OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"),
16271623
N_("create/reset and checkout a branch")),
16281624
OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
1625+
OPT_BOOL(0, "guess", &opts.dwim_new_local_branch,
1626+
N_("second guess 'git checkout <no-such-branch>' (default)")),
16291627
OPT_END()
16301628
};
16311629
int ret;
16321630

16331631
memset(&opts, 0, sizeof(opts));
1634-
opts.no_dwim_new_local_branch = 0;
1632+
opts.dwim_new_local_branch = 1;
16351633
opts.switch_branch_doing_nothing_is_ok = 1;
16361634
opts.accept_pathspec = 1;
16371635
opts.implicit_detach = 1;
@@ -1656,14 +1654,16 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
16561654
N_("create and switch to a new branch")),
16571655
OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"),
16581656
N_("create/reset and switch to a branch")),
1657+
OPT_BOOL(0, "guess", &opts.dwim_new_local_branch,
1658+
N_("second guess 'git switch <no-such-branch>'")),
16591659
OPT_BOOL(0, "discard-changes", &opts.discard_changes,
16601660
N_("throw away local modifications")),
16611661
OPT_END()
16621662
};
16631663
int ret;
16641664

16651665
memset(&opts, 0, sizeof(opts));
1666-
opts.no_dwim_new_local_branch = 0;
1666+
opts.dwim_new_local_branch = 1;
16671667
opts.accept_pathspec = 0;
16681668
opts.switch_branch_doing_nothing_is_ok = 0;
16691669
opts.implicit_detach = 0;

0 commit comments

Comments
 (0)