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
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
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 The default behavior can be set via the checkout.guess
90 configuration variable.
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 active
161 submodules according to the commit recorded in the superproject. If
162 nothing (or --no-recurse-submodules) is used, submodules working
163 trees will not be updated. Just like git-submodule(1), this will
164 detach HEAD of the submodules.
165
167 The following command switches to the "master" branch:
168
169 $ git switch master
170
171 After working in the wrong branch, switching to the correct branch
172 would be done using:
173
174 $ git switch mytopic
175
176 However, your "wrong" branch and correct "mytopic" branch may differ in
177 files that you have modified locally, in which case the above switch
178 would fail like this:
179
180 $ git switch mytopic
181 error: You have local changes to 'frotz'; not switching branches.
182
183 You can give the -m flag to the command, which would try a three-way
184 merge:
185
186 $ git switch -m mytopic
187 Auto-merging frotz
188
189 After this three-way merge, the local modifications are not registered
190 in your index file, so git diff would show you what changes you made
191 since the tip of the new branch.
192
193 To switch back to the previous branch before we switched to mytopic
194 (i.e. "master" branch):
195
196 $ git switch -
197
198 You can grow a new branch from any commit. For example, switch to
199 "HEAD~3" and create branch "fixup":
200
201 $ git switch -c fixup HEAD~3
202 Switched to a new branch 'fixup'
203
204 If you want to start a new branch from a remote branch of the same
205 name:
206
207 $ git switch new-topic
208 Branch 'new-topic' set up to track remote branch 'new-topic' from 'origin'
209 Switched to a new branch 'new-topic'
210
211 To check out commit HEAD~3 for temporary inspection or experiment
212 without creating a new branch:
213
214 $ git switch --detach HEAD~3
215 HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'
216
217 If it turns out whatever you have done is worth keeping, you can always
218 create a new name for it (without switching away):
219
220 $ git switch -c good-surprises
221
223 git-checkout(1), git-branch(1)
224
226 Part of the git(1) suite
227
228
229
230Git 2.31.1 2021-03-26 GIT-SWITCH(1)