1GIT-SWITCH(1) Git Manual GIT-SWITCH(1)
2
3
4
6 git-switch - Switch branches
7
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
14
16 Switch to a specified branch. The working tree and the index are
17 updated to match the branch. All new commits will be added to the tip
18 of this branch.
19
20 Optionally a new branch could be created with either -c, -C,
21 automatically from a remote branch of same name (see --guess), or
22 detach the working tree from any branch with --detach, along with
23 switching.
24
25 Switching branches does not require a clean index and working tree
26 (i.e. no differences compared to HEAD). The operation is aborted
27 however if the operation leads to loss of local changes, unless told
28 otherwise with --discard-changes or --merge.
29
30 THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
31
33 <branch>
34 Branch to switch to.
35
36 <new-branch>
37 Name for the new branch.
38
39 <start-point>
40 The starting point for the new branch. Specifying a <start-point>
41 allows you to create a branch based on some other point in history
42 than where HEAD currently points. (Or, in the case of --detach,
43 allows you to inspect and detach from some other point.)
44
45 You can use the @{-N} syntax to refer to the N-th last
46 branch/commit switched to using "git switch" or "git checkout"
47 operation. You may also specify - which is synonymous to @{-1}.
48 This is often used to switch quickly between two branches, or to
49 undo a branch switch by mistake.
50
51 As a special case, you may use A...B as a shortcut for the merge
52 base of A and B if there is exactly one merge base. You can leave
53 out at most one of A and B, in which case it defaults to HEAD.
54
55 -c <new-branch>, --create <new-branch>
56 Create a new branch named <new-branch> starting at <start-point>
57 before switching to the branch. This is a convenient shortcut for:
58
59 $ git branch <new-branch>
60 $ git switch <new-branch>
61
62
63 -C <new-branch>, --force-create <new-branch>
64 Similar to --create except that if <new-branch> already exists, it
65 will be reset to <start-point>. This is a convenient shortcut for:
66
67 $ git branch -f <new-branch>
68 $ git switch <new-branch>
69
70
71 -d, --detach
72 Switch to a commit for inspection and discardable experiments. See
73 the "DETACHED HEAD" section in git-checkout(1) for details.
74
75 --guess, --no-guess
76 If <branch> is not found but there does exist a tracking branch in
77 exactly one remote (call it <remote>) with a matching name, treat
78 as equivalent to
79
80 $ git switch -c <branch> --track <remote>/<branch>
81
82 If the branch exists in multiple remotes and one of them is named
83 by the checkout.defaultRemote configuration variable, we’ll use
84 that one for the purposes of disambiguation, even if the <branch>
85 isn’t unique across all remotes. Set it to e.g.
86 checkout.defaultRemote=origin to always checkout remote branches
87 from there if <branch> is ambiguous but exists on the origin
88 remote. See also checkout.defaultRemote in git-config(1).
89
90 --guess is the default behavior. Use --no-guess to disable it.
91
92 -f, --force
93 An alias for --discard-changes.
94
95 --discard-changes
96 Proceed even if the index or the working tree differs from HEAD.
97 Both the index and working tree are restored to match the switching
98 target. If --recurse-submodules is specified, submodule content is
99 also restored to match the switching target. This is used to throw
100 away local changes.
101
102 -m, --merge
103 If you have local modifications to one or more files that are
104 different between the current branch and the branch to which you
105 are switching, the command refuses to switch branches in order to
106 preserve your modifications in context. However, with this option,
107 a three-way merge between the current branch, your working tree
108 contents, and the new branch is done, and you will be on the new
109 branch.
110
111 When a merge conflict happens, the index entries for conflicting
112 paths are left unmerged, and you need to resolve the conflicts and
113 mark the resolved paths with git add (or git rm if the merge should
114 result in deletion of the path).
115
116 --conflict=<style>
117 The same as --merge option above, but changes the way the
118 conflicting hunks are presented, overriding the merge.conflictStyle
119 configuration variable. Possible values are "merge" (default) and
120 "diff3" (in addition to what is shown by "merge" style, shows the
121 original contents).
122
123 -q, --quiet
124 Quiet, suppress feedback messages.
125
126 --progress, --no-progress
127 Progress status is reported on the standard error stream by default
128 when it is attached to a terminal, unless --quiet is specified.
129 This flag enables progress reporting even if not attached to a
130 terminal, regardless of --quiet.
131
132 -t, --track
133 When creating a new branch, set up "upstream" configuration. -c is
134 implied. See --track in git-branch(1) for details.
135
136 If no -c option is given, the name of the new branch will be
137 derived from the remote-tracking branch, by looking at the local
138 part of the refspec configured for the corresponding remote, and
139 then stripping the initial part up to the "*". This would tell us
140 to use hack as the local branch when branching off of origin/hack
141 (or remotes/origin/hack, or even refs/remotes/origin/hack). If the
142 given name has no slash, or the above guessing results in an empty
143 name, the guessing is aborted. You can explicitly give a name with
144 -c in such a case.
145
146 --no-track
147 Do not set up "upstream" configuration, even if the
148 branch.autoSetupMerge configuration variable is true.
149
150 --orphan <new-branch>
151 Create a new orphan branch, named <new-branch>. All tracked files
152 are removed.
153
154 --ignore-other-worktrees
155 git switch refuses when the wanted ref is already checked out by
156 another worktree. This option makes it check the ref out anyway. In
157 other words, the ref can be held by more than one worktree.
158
159 --recurse-submodules, --no-recurse-submodules
160 Using --recurse-submodules will update the content of all
161 initialized submodules according to the commit recorded in the
162 superproject. If nothing (or --no-recurse-submodules) is used, the
163 work trees of submodules will not be updated. Just like git-
164 submodule(1), this will detach HEAD of the submodules.
165
167 The following command switches to the "master" branch:
168
169 $ git switch master
170
171
172 After working in the wrong branch, switching to the correct branch
173 would be done using:
174
175 $ git switch mytopic
176
177
178 However, your "wrong" branch and correct "mytopic" branch may differ in
179 files that you have modified locally, in which case the above switch
180 would fail like this:
181
182 $ git switch mytopic
183 error: You have local changes to 'frotz'; not switching branches.
184
185
186 You can give the -m flag to the command, which would try a three-way
187 merge:
188
189 $ git switch -m mytopic
190 Auto-merging frotz
191
192
193 After this three-way merge, the local modifications are not registered
194 in your index file, so git diff would show you what changes you made
195 since the tip of the new branch.
196
197 To switch back to the previous branch before we switched to mytopic
198 (i.e. "master" branch):
199
200 $ git switch -
201
202
203 You can grow a new branch from any commit. For example, switch to
204 "HEAD~3" and create branch "fixup":
205
206 $ git switch -c fixup HEAD~3
207 Switched to a new branch 'fixup'
208
209
210 If you want to start a new branch from a remote branch of the same
211 name:
212
213 $ git switch new-topic
214 Branch 'new-topic' set up to track remote branch 'new-topic' from 'origin'
215 Switched to a new branch 'new-topic'
216
217
218 To check out commit HEAD~3 for temporary inspection or experiment
219 without creating a new branch:
220
221 $ git switch --detach HEAD~3
222 HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'
223
224
225 If it turns out whatever you have done is worth keeping, you can always
226 create a new name for it (without switching away):
227
228 $ git switch -c good-surprises
229
230
232 git-checkout(1), git-branch(1)
233
235 Part of the git(1) suite
236
237
238
239Git 2.24.1 12/10/2019 GIT-SWITCH(1)