1GIT-SWITCH(1)                     Git Manual                     GIT-SWITCH(1)
2
3
4

NAME

6       git-switch - Switch branches
7

SYNOPSIS

9       git switch [<options>] [--no-guess] <branch>
10       git switch [<options>] --detach [<start-point>]
11       git switch [<options>] (-c|-C) <new-branch> [<start-point>]
12       git switch [<options>] --orphan <new-branch>
13

DESCRIPTION

15       Switch to a specified branch. The working tree and the index are
16       updated to match the branch. All new commits will be added to the tip
17       of this branch.
18
19       Optionally a new branch could be created with either -c, -C,
20       automatically from a remote branch of same name (see --guess), or
21       detach the working tree from any branch with --detach, along with
22       switching.
23
24       Switching branches does not require a clean index and working tree
25       (i.e. no differences compared to HEAD). The operation is aborted
26       however if the operation leads to loss of local changes, unless told
27       otherwise with --discard-changes or --merge.
28
29       THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
30

OPTIONS

32       <branch>
33           Branch to switch to.
34
35       <new-branch>
36           Name for the new branch.
37
38       <start-point>
39           The starting point for the new branch. Specifying a <start-point>
40           allows you to create a branch based on some other point in history
41           than where HEAD currently points. (Or, in the case of --detach,
42           allows you to inspect and detach from some other point.)
43
44           You can use the @{-N} syntax to refer to the N-th last
45           branch/commit switched to using "git switch" or "git checkout"
46           operation. You may also specify - which is synonymous to @{-1}.
47           This is often used to switch quickly between two branches, or to
48           undo a branch switch by mistake.
49
50           As a special case, you may use A...B as a shortcut for the merge
51           base of A and B if there is exactly one merge base. You can leave
52           out at most one of A and B, in which case it defaults to HEAD.
53
54       -c <new-branch>, --create <new-branch>
55           Create a new branch named <new-branch> starting at <start-point>
56           before switching to the branch. This is a convenient shortcut for:
57
58               $ git branch <new-branch>
59               $ git switch <new-branch>
60
61       -C <new-branch>, --force-create <new-branch>
62           Similar to --create except that if <new-branch> already exists, it
63           will be reset to <start-point>. This is a convenient shortcut for:
64
65               $ git branch -f <new-branch>
66               $ git switch <new-branch>
67
68       -d, --detach
69           Switch to a commit for inspection and discardable experiments. See
70           the "DETACHED HEAD" section in git-checkout(1) for details.
71
72       --guess, --no-guess
73           If <branch> is not found but there does exist a tracking branch in
74           exactly one remote (call it <remote>) with a matching name, treat
75           as equivalent to
76
77               $ git switch -c <branch> --track <remote>/<branch>
78
79           If the branch exists in multiple remotes and one of them is named
80           by the checkout.defaultRemote configuration variable, we’ll use
81           that one for the purposes of disambiguation, even if the <branch>
82           isn’t unique across all remotes. Set it to e.g.
83           checkout.defaultRemote=origin to always checkout remote branches
84           from there if <branch> is ambiguous but exists on the origin
85           remote. See also checkout.defaultRemote in git-config(1).
86
87           --guess is the default behavior. Use --no-guess to disable it.
88
89       -f, --force
90           An alias for --discard-changes.
91
92       --discard-changes
93           Proceed even if the index or the working tree differs from HEAD.
94           Both the index and working tree are restored to match the switching
95           target. If --recurse-submodules is specified, submodule content is
96           also restored to match the switching target. This is used to throw
97           away local changes.
98
99       -m, --merge
100           If you have local modifications to one or more files that are
101           different between the current branch and the branch to which you
102           are switching, the command refuses to switch branches in order to
103           preserve your modifications in context. However, with this option,
104           a three-way merge between the current branch, your working tree
105           contents, and the new branch is done, and you will be on the new
106           branch.
107
108           When a merge conflict happens, the index entries for conflicting
109           paths are left unmerged, and you need to resolve the conflicts and
110           mark the resolved paths with git add (or git rm if the merge should
111           result in deletion of the path).
112
113       --conflict=<style>
114           The same as --merge option above, but changes the way the
115           conflicting hunks are presented, overriding the merge.conflictStyle
116           configuration variable. Possible values are "merge" (default) and
117           "diff3" (in addition to what is shown by "merge" style, shows the
118           original contents).
119
120       -q, --quiet
121           Quiet, suppress feedback messages.
122
123       --progress, --no-progress
124           Progress status is reported on the standard error stream by default
125           when it is attached to a terminal, unless --quiet is specified.
126           This flag enables progress reporting even if not attached to a
127           terminal, regardless of --quiet.
128
129       -t, --track
130           When creating a new branch, set up "upstream" configuration.  -c is
131           implied. See --track in git-branch(1) for details.
132
133           If no -c option is given, the name of the new branch will be
134           derived from the remote-tracking branch, by looking at the local
135           part of the refspec configured for the corresponding remote, and
136           then stripping the initial part up to the "*". This would tell us
137           to use hack as the local branch when branching off of origin/hack
138           (or remotes/origin/hack, or even refs/remotes/origin/hack). If the
139           given name has no slash, or the above guessing results in an empty
140           name, the guessing is aborted. You can explicitly give a name with
141           -c in such a case.
142
143       --no-track
144           Do not set up "upstream" configuration, even if the
145           branch.autoSetupMerge configuration variable is true.
146
147       --orphan <new-branch>
148           Create a new orphan branch, named <new-branch>. All tracked files
149           are removed.
150
151       --ignore-other-worktrees
152           git switch refuses when the wanted ref is already checked out by
153           another worktree. This option makes it check the ref out anyway. In
154           other words, the ref can be held by more than one worktree.
155
156       --recurse-submodules, --no-recurse-submodules
157           Using --recurse-submodules will update the content of all
158           initialized submodules according to the commit recorded in the
159           superproject. If nothing (or --no-recurse-submodules) is used, the
160           work trees of submodules will not be updated. Just like git-
161           submodule(1), this will detach HEAD of the submodules.
162

EXAMPLES

164       The following command switches to the "master" branch:
165
166           $ git switch master
167
168       After working in the wrong branch, switching to the correct branch
169       would be done using:
170
171           $ git switch mytopic
172
173       However, your "wrong" branch and correct "mytopic" branch may differ in
174       files that you have modified locally, in which case the above switch
175       would fail like this:
176
177           $ git switch mytopic
178           error: You have local changes to 'frotz'; not switching branches.
179
180       You can give the -m flag to the command, which would try a three-way
181       merge:
182
183           $ git switch -m mytopic
184           Auto-merging frotz
185
186       After this three-way merge, the local modifications are not registered
187       in your index file, so git diff would show you what changes you made
188       since the tip of the new branch.
189
190       To switch back to the previous branch before we switched to mytopic
191       (i.e. "master" branch):
192
193           $ git switch -
194
195       You can grow a new branch from any commit. For example, switch to
196       "HEAD~3" and create branch "fixup":
197
198           $ git switch -c fixup HEAD~3
199           Switched to a new branch 'fixup'
200
201       If you want to start a new branch from a remote branch of the same
202       name:
203
204           $ git switch new-topic
205           Branch 'new-topic' set up to track remote branch 'new-topic' from 'origin'
206           Switched to a new branch 'new-topic'
207
208       To check out commit HEAD~3 for temporary inspection or experiment
209       without creating a new branch:
210
211           $ git switch --detach HEAD~3
212           HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'
213
214       If it turns out whatever you have done is worth keeping, you can always
215       create a new name for it (without switching away):
216
217           $ git switch -c good-surprises
218

SEE ALSO

220       git-checkout(1), git-branch(1)
221

GIT

223       Part of the git(1) suite
224
225
226
227Git 2.26.2                        2020-04-20                     GIT-SWITCH(1)
Impressum