checkout: split part of it to new command 'switch'

"git checkout" doing too many things is a source of confusion for many users (and it even bites old timers sometimes). To remedy that, the command will be split into two new ones: switch and restore. The good old "git checkout" command is still here and will be until all (or most of users) are sick of it. See the new man page for the final design of switch. The actual implementation though is still pretty much the same as "git checkout" and not completely aligned with the man page. Following patches will adjust their behavior to match the man page. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Mar 29, 2019 at 17:39 UTC d787d311dbd7a4104a9dde23b90ae24528a15cf9
14 files changed +353 -27
.gitignore
+1
@@ -167,6 +167,7 @@
167 /git-submodule
168 /git-submodule--helper
169 /git-svn
170 +/git-switch
171 /git-symbolic-ref
172 /git-tag
173 /git-unpack-file
Documentation/config/advice.txt
+8 -5
@@ -42,7 +42,8 @@ advice.*::
42 state in the output of linkgit:git-status[1], in
43 the template shown when writing commit messages in
44 linkgit:git-commit[1], and in the help message shown
45 - by linkgit:git-checkout[1] when switching branch.
45 + by linkgit:git-switch[1] or
46 + linkgit:git-checkout[1] when switching branch.
47 statusUoption::
48 Advise to consider using the `-u` option to linkgit:git-status[1]
49 when the command takes more than 2 seconds to enumerate untracked
@@ -62,12 +63,14 @@ advice.*::
63 your information is guessed from the system username and
64 domain name.
65 detachedHead::
65 - Advice shown when you used linkgit:git-checkout[1] to
66 - move to the detach HEAD state, to instruct how to create
67 - a local branch after the fact.
66 + Advice shown when you used
67 + linkgit:git-switch[1] or linkgit:git-checkout[1]
68 + to move to the detach HEAD state, to instruct how to
69 + create a local branch after the fact.
70 checkoutAmbiguousRemoteBranchName::
71 Advice shown when the argument to
70 - linkgit:git-checkout[1] ambiguously resolves to a
72 + linkgit:git-checkout[1] and linkgit:git-switch[1]
73 + ambiguously resolves to a
74 remote tracking branch on more than one remote in
75 situations where an unambiguous argument would have
76 otherwise caused a remote-tracking branch to be
Documentation/config/branch.txt
+2 -2
@@ -1,5 +1,5 @@
1 branch.autoSetupMerge::
2 - Tells 'git branch' and 'git checkout' to set up new branches
2 + Tells 'git branch', 'git switch' and 'git checkout' to set up new branches
3 so that linkgit:git-pull[1] will appropriately merge from the
4 starting point branch. Note that even if this option is not set,
5 this behavior can be chosen per-branch using the `--track`
@@ -11,7 +11,7 @@ branch.autoSetupMerge::
11 branch. This option defaults to true.
12
13 branch.autoSetupRebase::
14 - When a new branch is created with 'git branch' or 'git checkout'
14 + When a new branch is created with 'git branch', 'git switch' or 'git checkout'
15 that tracks another branch, this variable tells Git to set
16 up pull to rebase instead of merge (see "branch.<name>.rebase").
17 When `never`, rebase is never automatically set to true.
Documentation/config/checkout.txt
+6 -3
@@ -1,5 +1,6 @@
1 checkout.defaultRemote::
2 - When you run 'git checkout <something>' and only have one
2 + When you run 'git checkout <something>'
3 + or 'git switch <something>' and only have one
4 remote, it may implicitly fall back on checking out and
5 tracking e.g. 'origin/<something>'. This stops working as soon
6 as you have more than one remote with a '<something>'
@@ -8,8 +9,10 @@ checkout.defaultRemote::
9 disambiguation. The typical use-case is to set this to
10 `origin`.
11 +
11 -Currently this is used by linkgit:git-checkout[1] when 'git checkout
12 -<something>' will checkout the '<something>' branch on another remote,
12 +Currently this is used by linkgit:git-switch[1] and
13 +linkgit:git-checkout[1] when 'git checkout <something>'
14 +or 'git switch <something>'
15 +will checkout the '<something>' branch on another remote,
16 and by linkgit:git-worktree[1] when 'git worktree add' refers to a
17 remote branch. This setting might be used for other checkout-like
18 commands or functionality in the future.
Documentation/config/diff.txt
+2 -1
@@ -78,7 +78,8 @@ diff.external::
78 diff.ignoreSubmodules::
79 Sets the default value of --ignore-submodules. Note that this
80 affects only 'git diff' Porcelain, and not lower level 'diff'
81 - commands such as 'git diff-files'. 'git checkout' also honors
81 + commands such as 'git diff-files'. 'git checkout'
82 + and 'git switch' also honor
83 this setting when reporting uncommitted changes. Setting it to
84 'all' disables the submodule summary normally shown by 'git commit'
85 and 'git status' when `status.submoduleSummary` is set unless it is
Documentation/git-checkout.txt
+4
@@ -560,6 +560,10 @@ $ edit frotz
560 $ git add frotz
561 ------------
562
563 +SEE ALSO
564 +--------
565 +linkgit:git-switch[1]
566 +
567 GIT
568 ---
569 Part of the linkgit:git[1] suite
Documentation/git-switch.txt new
+271
@@ -0,0 +1,271 @@
1 +git-switch(1)
2 +=============
3 +
4 +NAME
5 +----
6 +git-switch - Switch branches
7 +
8 +SYNOPSIS
9 +--------
10 +[verse]
11 +'git switch' [<options>] [--no-guess] <branch>
12 +'git switch' [<options>] --detach [<start-point>]
13 +'git switch' [<options>] (-c|-C) <new-branch> [<start-point>]
14 +'git switch' [<options>] --orphan <new-branch>
15 +
16 +DESCRIPTION
17 +-----------
18 +Switch to a specified branch. The working tree and the index are
19 +updated to match the branch. All new commits will be added to the tip
20 +of this branch.
21 +
22 +Optionally a new branch could be created with either `-c`, `-C`,
23 +automatically from a remote branch of same name (see `--guess`), or
24 +detach the working tree from any branch with `--detach`, along with
25 +switching.
26 +
27 +Switching branches does not require a clean index and working tree
28 +(i.e. no differences compared to `HEAD`). The operation is aborted
29 +however if the operation leads to loss of local changes, unless told
30 +otherwise with `--discard-changes` or `--merge`.
31 +
32 +OPTIONS
33 +-------
34 +<branch>::
35 + Branch to switch to.
36 +
37 +<new-branch>::
38 + Name for the new branch.
39 +
40 +<start-point>::
41 + The starting point for the new branch. Specifying a
42 + `<start-point>` allows you to create a branch based on some
43 + other point in history than where HEAD currently points. (Or,
44 + in the case of `--detach`, allows you to inspect and detach
45 + from some other point.)
46 ++
47 +You can use the `@{-N}` syntax to refer to the N-th last
48 +branch/commit switched to using "git switch" or "git checkout"
49 +operation. You may also specify `-` which is synonymous to `@{-1}`.
50 +This is often used to switch quickly between two branches, or to undo
51 +a branch switch by mistake.
52 ++
53 +As a special case, you may use `A...B` as a shortcut for the merge
54 +base of `A` and `B` if there is exactly one merge base. You can leave
55 +out at most one of `A` and `B`, in which case it defaults to `HEAD`.
56 +
57 +-c <new-branch>::
58 +--create <new-branch>::
59 + Create a new branch named `<new-branch>` starting at
60 + `<start-point>` before switching to the branch. This is a
61 + convenient shortcut for:
62 ++
63 +------------
64 +$ git branch <new-branch>
65 +$ git switch <new-branch>
66 +------------
67 +
68 +-C <new-branch>::
69 +--force-create <new-branch>::
70 + Similar to `--create` except that if `<new-branch>` already
71 + exists, it will be reset to `<start-point>`. This is a
72 + convenient shortcut for:
73 ++
74 +------------
75 +$ git branch -f <new-branch>
76 +$ git switch <new-branch>
77 +------------
78 +
79 +-d::
80 +--detach::
81 + Switch to a commit for inspection and discardable
82 + experiments. See the "DETACHED HEAD" section in
83 + linkgit:git-checkout[1] for details.
84 +
85 +--guess::
86 +--no-guess::
87 + If `<branch>` is not found but there does exist a tracking
88 + branch in exactly one remote (call it `<remote>`) with a
89 + matching name, treat as equivalent to
90 ++
91 +------------
92 +$ git switch -c <branch> --track <remote>/<branch>
93 +------------
94 ++
95 +If the branch exists in multiple remotes and one of them is named by
96 +the `checkout.defaultRemote` configuration variable, we'll use that
97 +one for the purposes of disambiguation, even if the `<branch>` isn't
98 +unique across all remotes. Set it to e.g. `checkout.defaultRemote=origin`
99 +to always checkout remote branches from there if `<branch>` is
100 +ambiguous but exists on the 'origin' remote. See also
101 +`checkout.defaultRemote` in linkgit:git-config[1].
102 ++
103 +`--guess` is the default behavior. Use `--no-guess` to disable it.
104 +
105 +-f::
106 +--force::
107 + An alias for `--discard-changes`.
108 +
109 +--discard-changes::
110 + Proceed even if the index or the working tree differs from
111 + `HEAD`. Both the index and working tree are restored to match
112 + the switching target. If `--recurse-submodules` is specified,
113 + submodule content is also restored to match the switching
114 + target. This is used to throw away local changes.
115 +
116 +-m::
117 +--merge::
118 + If you have local modifications to one or more files that are
119 + different between the current branch and the branch to which
120 + you are switching, the command refuses to switch branches in
121 + order to preserve your modifications in context. However,
122 + with this option, a three-way merge between the current
123 + branch, your working tree contents, and the new branch is
124 + done, and you will be on the new branch.
125 ++
126 +When a merge conflict happens, the index entries for conflicting
127 +paths are left unmerged, and you need to resolve the conflicts
128 +and mark the resolved paths with `git add` (or `git rm` if the merge
129 +should result in deletion of the path).
130 +
131 +--conflict=<style>::
132 + The same as `--merge` option above, but changes the way the
133 + conflicting hunks are presented, overriding the
134 + `merge.conflictStyle` configuration variable. Possible values are
135 + "merge" (default) and "diff3" (in addition to what is shown by
136 + "merge" style, shows the original contents).
137 +
138 +-q::
139 +--quiet::
140 + Quiet, suppress feedback messages.
141 +
142 +--progress::
143 +--no-progress::
144 + Progress status is reported on the standard error stream
145 + by default when it is attached to a terminal, unless `--quiet`
146 + is specified. This flag enables progress reporting even if not
147 + attached to a terminal, regardless of `--quiet`.
148 +
149 +-t::
150 +--track::
151 + When creating a new branch, set up "upstream" configuration.
152 + `-c` is implied. See `--track` in linkgit:git-branch[1] for
153 + details.
154 ++
155 +If no `-c` option is given, the name of the new branch will be derived
156 +from the remote-tracking branch, by looking at the local part of the
157 +refspec configured for the corresponding remote, and then stripping
158 +the initial part up to the "*". This would tell us to use `hack` as
159 +the local branch when branching off of `origin/hack` (or
160 +`remotes/origin/hack`, or even `refs/remotes/origin/hack`). If the
161 +given name has no slash, or the above guessing results in an empty
162 +name, the guessing is aborted. You can explicitly give a name with
163 +`-c` in such a case.
164 +
165 +--no-track::
166 + Do not set up "upstream" configuration, even if the
167 + `branch.autoSetupMerge` configuration variable is true.
168 +
169 +--orphan <new-branch>::
170 + Create a new 'orphan' branch, named `<new-branch>`. All
171 + tracked files are removed.
172 +
173 +--ignore-other-worktrees::
174 + `git switch` refuses when the wanted ref is already
175 + checked out by another worktree. This option makes it check
176 + the ref out anyway. In other words, the ref can be held by
177 + more than one worktree.
178 +
179 +--recurse-submodules::
180 +--no-recurse-submodules::
181 + Using `--recurse-submodules` will update the content of all
182 + initialized submodules according to the commit recorded in the
183 + superproject. If nothing (or `--no-recurse-submodules`) is
184 + used, the work trees of submodules will not be updated. Just
185 + like linkgit:git-submodule[1], this will detach `HEAD` of the
186 + submodules.
187 +
188 +EXAMPLES
189 +--------
190 +
191 +The following command switches to the "master" branch:
192 +
193 +------------
194 +$ git switch master
195 +------------
196 +
197 +After working in the wrong branch, switching to the correct branch
198 +would be done using:
199 +
200 +------------
201 +$ git switch mytopic
202 +------------
203 +
204 +However, your "wrong" branch and correct "mytopic" branch may differ
205 +in files that you have modified locally, in which case the above
206 +switch would fail like this:
207 +
208 +------------
209 +$ git switch mytopic
210 +error: You have local changes to 'frotz'; not switching branches.
211 +------------
212 +
213 +You can give the `-m` flag to the command, which would try a three-way
214 +merge:
215 +
216 +------------
217 +$ git switch -m mytopic
218 +Auto-merging frotz
219 +------------
220 +
221 +After this three-way merge, the local modifications are _not_
222 +registered in your index file, so `git diff` would show you what
223 +changes you made since the tip of the new branch.
224 +
225 +To switch back to the previous branch before we switched to mytopic
226 +(i.e. "master" branch):
227 +
228 +------------
229 +$ git switch -
230 +------------
231 +
232 +You can grow a new branch from any commit. For example, switch to
233 +"HEAD~3" and create branch "fixup":
234 +
235 +------------
236 +$ git switch -c fixup HEAD~3
237 +Switched to a new branch 'fixup'
238 +------------
239 +
240 +If you want to start a new branch from a remote branch of the same
241 +name:
242 +
243 +------------
244 +$ git switch new-topic
245 +Branch 'new-topic' set up to track remote branch 'new-topic' from 'origin'
246 +Switched to a new branch 'new-topic'
247 +------------
248 +
249 +To check out commit `HEAD~3` for temporary inspection or experiment
250 +without creating a new branch:
251 +
252 +------------
253 +$ git switch --detach HEAD~3
254 +HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'
255 +------------
256 +
257 +If it turns out whatever you have done is worth keeping, you can
258 +always create a new name for it (without switching away):
259 +
260 +------------
261 +$ git switch -c good-surprises
262 +------------
263 +
264 +SEE ALSO
265 +--------
266 +linkgit:git-checkout[1],
267 +linkgit:git-branch[1]
268 +
269 +GIT
270 +---
271 +Part of the linkgit:git[1] suite
Documentation/gitattributes.txt
+2 -1
@@ -112,7 +112,8 @@ Checking-out and checking-in
112
113 These attributes affect how the contents stored in the
114 repository are copied to the working tree files when commands
115 -such as 'git checkout' and 'git merge' run. They also affect how
115 +such as 'git switch', 'git checkout' and 'git merge' run.
116 +They also affect how
117 Git stores the contents you prepare in the working tree in the
118 repository upon 'git add' and 'git commit'.
119
Documentation/githooks.txt
+5 -3
@@ -165,12 +165,13 @@ rebased, and is not set when rebasing the current branch.
165 post-checkout
166 ~~~~~~~~~~~~~
167
168 -This hook is invoked when a linkgit:git-checkout[1] is run after having updated the
168 +This hook is invoked when a linkgit:git-checkout[1] or
169 +linkgit:git-switch[1] is run after having updated the
170 worktree. The hook is given three parameters: the ref of the previous HEAD,
171 the ref of the new HEAD (which may or may not have changed), and a flag
172 indicating whether the checkout was a branch checkout (changing branches,
173 flag=1) or a file checkout (retrieving a file from the index, flag=0).
173 -This hook cannot affect the outcome of `git checkout`.
174 +This hook cannot affect the outcome of `git switch` or `git checkout`.
175
176 It is also run after linkgit:git-clone[1], unless the `--no-checkout` (`-n`) option is
177 used. The first parameter given to the hook is the null-ref, the second the
@@ -406,7 +407,8 @@ exit with a zero status.
407 For example, the hook can simply run `git read-tree -u -m HEAD "$1"`
408 in order to emulate `git fetch` that is run in the reverse direction
409 with `git push`, as the two-tree form of `git read-tree -u -m` is
409 -essentially the same as `git checkout` that switches branches while
410 +essentially the same as `git switch` or `git checkout`
411 +that switches branches while
412 keeping the local changes in the working tree that do not interfere
413 with the difference between the branches.
414
Makefile
+1
@@ -802,6 +802,7 @@ BUILT_INS += git-merge-subtree$X
802 BUILT_INS += git-show$X
803 BUILT_INS += git-stage$X
804 BUILT_INS += git-status$X
805 +BUILT_INS += git-switch$X
806 BUILT_INS += git-whatchanged$X
807
808 # what 'all' will build and 'install' will install in gitexecdir,
builtin.h
+1
@@ -227,6 +227,7 @@ extern int cmd_show_index(int argc, const char **argv, const char *prefix);
227 extern int cmd_status(int argc, const char **argv, const char *prefix);
228 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
229 extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);
230 +extern int cmd_switch(int argc, const char **argv, const char *prefix);
231 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
232 extern int cmd_tag(int argc, const char **argv, const char *prefix);
233 extern int cmd_tar_tree(int argc, const char **argv, const char *prefix);
builtin/checkout.c
+48 -12
@@ -34,6 +34,11 @@ static const char * const checkout_usage[] = {
34 NULL,
35 };
36
37 +static const char * const switch_branch_usage[] = {
38 + N_("git switch [<options>] [<branch>]"),
39 + NULL,
40 +};
41 +
42 struct checkout_opts {
43 int patch_mode;
44 int quiet;
@@ -1411,33 +1416,25 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,
1416 return newopts;
1417 }
1418
1414 -int cmd_checkout(int argc, const char **argv, const char *prefix)
1419 +static int checkout_main(int argc, const char **argv, const char *prefix,
1420 + struct checkout_opts *opts, struct option *options,
1421 + const char * const usagestr[])
1422 {
1416 - struct checkout_opts real_opts;
1417 - struct checkout_opts *opts = &real_opts;
1423 struct branch_info new_branch_info;
1424 int dwim_remotes_matched = 0;
1425 int dwim_new_local_branch;
1421 - struct option *options = NULL;
1426
1423 - memset(opts, 0, sizeof(*opts));
1427 memset(&new_branch_info, 0, sizeof(new_branch_info));
1428 opts->overwrite_ignore = 1;
1429 opts->prefix = prefix;
1430 opts->show_progress = -1;
1431 opts->overlay_mode = -1;
1429 - opts->no_dwim_new_local_branch = 0;
1432
1433 git_config(git_checkout_config, opts);
1434
1435 opts->track = BRANCH_TRACK_UNSPECIFIED;
1436
1435 - options = parse_options_dup(options);
1436 - options = add_common_options(opts, options);
1437 - options = add_switch_branch_options(opts, options);
1438 - options = add_checkout_path_options(opts, options);
1439 -
1440 - argc = parse_options(argc, argv, prefix, options, checkout_usage,
1437 + argc = parse_options(argc, argv, prefix, options, usagestr,
1438 PARSE_OPT_KEEP_DASHDASH);
1439
1440 dwim_new_local_branch = !opts->no_dwim_new_local_branch;
@@ -1570,3 +1567,42 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1567 return checkout_branch(opts, &new_branch_info);
1568 }
1569 }
1570 +
1571 +int cmd_checkout(int argc, const char **argv, const char *prefix)
1572 +{
1573 + struct checkout_opts opts;
1574 + struct option *options = NULL;
1575 + int ret;
1576 +
1577 + memset(&opts, 0, sizeof(opts));
1578 + opts.no_dwim_new_local_branch = 0;
1579 +
1580 + options = parse_options_dup(options);
1581 + options = add_common_options(&opts, options);
1582 + options = add_switch_branch_options(&opts, options);
1583 + options = add_checkout_path_options(&opts, options);
1584 +
1585 + ret = checkout_main(argc, argv, prefix, &opts,
1586 + options, checkout_usage);
1587 + FREE_AND_NULL(options);
1588 + return ret;
1589 +}
1590 +
1591 +int cmd_switch(int argc, const char **argv, const char *prefix)
1592 +{
1593 + struct checkout_opts opts;
1594 + struct option *options = NULL;
1595 + int ret;
1596 +
1597 + memset(&opts, 0, sizeof(opts));
1598 + opts.no_dwim_new_local_branch = 0;
1599 +
1600 + options = parse_options_dup(options);
1601 + options = add_common_options(&opts, options);
1602 + options = add_switch_branch_options(&opts, options);
1603 +
1604 + ret = checkout_main(argc, argv, prefix, &opts,
1605 + options, switch_branch_usage);
1606 + FREE_AND_NULL(options);
1607 + return ret;
1608 +}
command-list.txt
+1
@@ -171,6 +171,7 @@ git-status mainporcelain info
171 git-stripspace purehelpers
172 git-submodule mainporcelain
173 git-svn foreignscminterface
174 +git-switch mainporcelain history
175 git-symbolic-ref plumbingmanipulators
176 git-tag mainporcelain history
177 git-unpack-file plumbinginterrogators
git.c
+1
@@ -573,6 +573,7 @@ static struct cmd_struct commands[] = {
573 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
574 { "stripspace", cmd_stripspace },
575 { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
576 + { "switch", cmd_switch, RUN_SETUP | NEED_WORK_TREE },
577 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
578 { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG },
579 { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT },