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),
120 "diff3", and "zdiff3".
121
122 -q, --quiet
123 Quiet, suppress feedback messages.
124
125 --progress, --no-progress
126 Progress status is reported on the standard error stream by default
127 when it is attached to a terminal, unless --quiet is specified.
128 This flag enables progress reporting even if not attached to a
129 terminal, regardless of --quiet.
130
131 -t, --track [direct|inherit]
132 When creating a new branch, set up "upstream" configuration. -c is
133 implied. See --track in git-branch(1) for details.
134
135 If no -c option is given, the name of the new branch will be
136 derived from the remote-tracking branch, by looking at the local
137 part of the refspec configured for the corresponding remote, and
138 then stripping the initial part up to the "*". This would tell us
139 to use hack as the local branch when branching off of origin/hack
140 (or remotes/origin/hack, or even refs/remotes/origin/hack). If the
141 given name has no slash, or the above guessing results in an empty
142 name, the guessing is aborted. You can explicitly give a name with
143 -c in such a case.
144
145 --no-track
146 Do not set up "upstream" configuration, even if the
147 branch.autoSetupMerge configuration variable is true.
148
149 --orphan <new-branch>
150 Create a new orphan branch, named <new-branch>. All tracked files
151 are removed.
152
153 --ignore-other-worktrees
154 git switch refuses when the wanted ref is already checked out by
155 another worktree. This option makes it check the ref out anyway. In
156 other words, the ref can be held by more than one worktree.
157
158 --recurse-submodules, --no-recurse-submodules
159 Using --recurse-submodules will update the content of all active
160 submodules according to the commit recorded in the superproject. If
161 nothing (or --no-recurse-submodules) is used, submodules working
162 trees will not be updated. Just like git-submodule(1), this will
163 detach HEAD of the submodules.
164
166 The following command switches to the "master" branch:
167
168 $ git switch master
169
170 After working in the wrong branch, switching to the correct branch
171 would be done using:
172
173 $ git switch mytopic
174
175 However, your "wrong" branch and correct "mytopic" branch may differ in
176 files that you have modified locally, in which case the above switch
177 would fail like this:
178
179 $ git switch mytopic
180 error: You have local changes to 'frotz'; not switching branches.
181
182 You can give the -m flag to the command, which would try a three-way
183 merge:
184
185 $ git switch -m mytopic
186 Auto-merging frotz
187
188 After this three-way merge, the local modifications are not registered
189 in your index file, so git diff would show you what changes you made
190 since the tip of the new branch.
191
192 To switch back to the previous branch before we switched to mytopic
193 (i.e. "master" branch):
194
195 $ git switch -
196
197 You can grow a new branch from any commit. For example, switch to
198 "HEAD~3" and create branch "fixup":
199
200 $ git switch -c fixup HEAD~3
201 Switched to a new branch 'fixup'
202
203 If you want to start a new branch from a remote branch of the same
204 name:
205
206 $ git switch new-topic
207 Branch 'new-topic' set up to track remote branch 'new-topic' from 'origin'
208 Switched to a new branch 'new-topic'
209
210 To check out commit HEAD~3 for temporary inspection or experiment
211 without creating a new branch:
212
213 $ git switch --detach HEAD~3
214 HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'
215
216 If it turns out whatever you have done is worth keeping, you can always
217 create a new name for it (without switching away):
218
219 $ git switch -c good-surprises
220
222 git-checkout(1), git-branch(1)
223
225 Part of the git(1) suite
226
227
228
229Git 2.36.1 2022-05-05 GIT-SWITCH(1)