3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets v1.79.2 <http://docbook.sf.net/>
7 .\" Source: Git 2.46.0.164.g477ce5ccd6
10 .TH "GIT\-SWITCH" "1" "2024-08-14" "Git 2\&.46\&.0\&.164\&.g477ce5" "Git Manual"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 git-switch \- Switch branches
35 \fIgit switch\fR [<options>] [\-\-no\-guess] <branch>
36 \fIgit switch\fR [<options>] \-\-detach [<start\-point>]
37 \fIgit switch\fR [<options>] (\-c|\-C) <new\-branch> [<start\-point>]
38 \fIgit switch\fR [<options>] \-\-orphan <new\-branch>
42 Switch to a specified branch\&. The working tree and the index are updated to match the branch\&. All new commits will be added to the tip of this branch\&.
44 Optionally a new branch could be created with either \fB\-c\fR, \fB\-C\fR, automatically from a remote branch of same name (see \fB\-\-guess\fR), or detach the working tree from any branch with \fB\-\-detach\fR, along with switching\&.
46 Switching branches does not require a clean index and working tree (i\&.e\&. no differences compared to \fBHEAD\fR)\&. The operation is aborted however if the operation leads to loss of local changes, unless told otherwise with \fB\-\-discard\-changes\fR or \fB\-\-merge\fR\&.
48 THIS COMMAND IS EXPERIMENTAL\&. THE BEHAVIOR MAY CHANGE\&.
53 Branch to switch to\&.
58 Name for the new branch\&.
63 The starting point for the new branch\&. Specifying a
65 allows you to create a branch based on some other point in history than where HEAD currently points\&. (Or, in the case of
66 \fB\-\-detach\fR, allows you to inspect and detach from some other point\&.)
70 syntax to refer to the N\-th last branch/commit switched to using "git switch" or "git checkout" operation\&. You may also specify
72 which is synonymous to
73 \fB@{\-1}\fR\&. This is often used to switch quickly between two branches, or to undo a branch switch by mistake\&.
75 As a special case, you may use
77 as a shortcut for the merge base of
81 if there is exactly one merge base\&. You can leave out at most one of
84 \fBB\fR, in which case it defaults to
88 \-c <new\-branch>, \-\-create <new\-branch>
90 Create a new branch named
94 before switching to the branch\&. This is the transactional equivalent of
100 $ git branch <new\-branch>
101 $ git switch <new\-branch>
107 that is to say, the branch is not reset/created unless "git switch" is successful (e\&.g\&., when the branch is in use in another worktree, not just the current branch stays the same, but the branch is not reset to the start\-point, either)\&.
110 \-C <new\-branch>, \-\-force\-create <new\-branch>
116 already exists, it will be reset to
117 \fB<start\-point>\fR\&. This is a convenient shortcut for:
123 $ git branch \-f <new\-branch>
124 $ git switch <new\-branch>
133 Switch to a commit for inspection and discardable experiments\&. See the "DETACHED HEAD" section in
134 \fBgit-checkout\fR(1)
138 \-\-guess, \-\-no\-guess
142 is not found but there does exist a tracking branch in exactly one remote (call it
143 \fB<remote>\fR) with a matching name, treat as equivalent to
149 $ git switch \-c <branch> \-\-track <remote>/<branch>
155 If the branch exists in multiple remotes and one of them is named by the
156 \fBcheckout\&.defaultRemote\fR
157 configuration variable, we\(cqll use that one for the purposes of disambiguation, even if the
159 isn\(cqt unique across all remotes\&. Set it to e\&.g\&.
160 \fBcheckout\&.defaultRemote=origin\fR
161 to always checkout remote branches from there if
163 is ambiguous but exists on the
166 \fBcheckout\&.defaultRemote\fR
168 \fBgit-config\fR(1)\&.
171 is the default behavior\&. Use
175 The default behavior can be set via the
176 \fBcheckout\&.guess\fR
177 configuration variable\&.
183 \fB\-\-discard\-changes\fR\&.
188 Proceed even if the index or the working tree differs from
189 \fBHEAD\fR\&. Both the index and working tree are restored to match the switching target\&. If
190 \fB\-\-recurse\-submodules\fR
191 is specified, submodule content is also restored to match the switching target\&. This is used to throw away local changes\&.
196 If you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context\&. However, with this option, a three\-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch\&.
198 When a merge conflict happens, the index entries for conflicting paths are left unmerged, and you need to resolve the conflicts and mark the resolved paths with
202 if the merge should result in deletion of the path)\&.
209 option above, but changes the way the conflicting hunks are presented, overriding the
210 \fBmerge\&.conflictStyle\fR
211 configuration variable\&. Possible values are "merge" (default), "diff3", and "zdiff3"\&.
216 Quiet, suppress feedback messages\&.
219 \-\-progress, \-\-no\-progress
221 Progress status is reported on the standard error stream by default when it is attached to a terminal, unless
223 is specified\&. This flag enables progress reporting even if not attached to a terminal, regardless of
227 \-t, \-\-track [direct|inherit]
229 When creating a new branch, set up "upstream" configuration\&.
239 option is given, the name of the new branch will be derived from the remote\-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping the initial part up to the "*"\&. This would tell us to use
241 as the local branch when branching off of
244 \fBremotes/origin/hack\fR, or even
245 \fBrefs/remotes/origin/hack\fR)\&. If the given name has no slash, or the above guessing results in an empty name, the guessing is aborted\&. You can explicitly give a name with
252 Do not set up "upstream" configuration, even if the
253 \fBbranch\&.autoSetupMerge\fR
254 configuration variable is true\&.
257 \-\-orphan <new\-branch>
259 Create a new unborn branch, named
260 \fB<new\-branch>\fR\&. All tracked files are removed\&.
263 \-\-ignore\-other\-worktrees
266 refuses when the wanted ref is already checked out by another worktree\&. This option makes it check the ref out anyway\&. In other words, the ref can be held by more than one worktree\&.
269 \-\-recurse\-submodules, \-\-no\-recurse\-submodules
272 \fB\-\-recurse\-submodules\fR
273 will update the content of all active submodules according to the commit recorded in the superproject\&. If nothing (or
274 \fB\-\-no\-recurse\-submodules\fR) is used, submodules working trees will not be updated\&. Just like
275 \fBgit-submodule\fR(1), this will detach
281 The following command switches to the "master" branch:
293 After working in the wrong branch, switching to the correct branch would be done using:
305 However, your "wrong" branch and correct "mytopic" branch may differ in files that you have modified locally, in which case the above switch would fail like this:
312 error: You have local changes to \*(Aqfrotz\*(Aq; not switching branches\&.
318 You can give the \fB\-m\fR flag to the command, which would try a three\-way merge:
324 $ git switch \-m mytopic
331 After this three\-way merge, the local modifications are \fInot\fR registered in your index file, so \fBgit diff\fR would show you what changes you made since the tip of the new branch\&.
333 To switch back to the previous branch before we switched to mytopic (i\&.e\&. "master" branch):
345 You can grow a new branch from any commit\&. For example, switch to "HEAD~3" and create branch "fixup":
351 $ git switch \-c fixup HEAD~3
352 Switched to a new branch \*(Aqfixup\*(Aq
358 If you want to start a new branch from a remote branch of the same name:
364 $ git switch new\-topic
365 Branch \*(Aqnew\-topic\*(Aq set up to track remote branch \*(Aqnew\-topic\*(Aq from \*(Aqorigin\*(Aq
366 Switched to a new branch \*(Aqnew\-topic\*(Aq
372 To check out commit \fBHEAD~3\fR for temporary inspection or experiment without creating a new branch:
378 $ git switch \-\-detach HEAD~3
379 HEAD is now at 9fc9555312 Merge branch \*(Aqcc/shared\-index\-permbits\*(Aq
385 If it turns out whatever you have done is worth keeping, you can always create a new name for it (without switching away):
391 $ git switch \-c good\-surprises
398 Everything below this line in this section is selectively included from the \fBgit-config\fR(1) documentation\&. The content is the same as what\(cqs found there:
400 checkout\&.defaultRemote
403 \fBgit checkout <something>\fR
405 \fBgit switch <something>\fR
406 and only have one remote, it may implicitly fall back on checking out and tracking e\&.g\&.
407 \fBorigin/<something>\fR\&. This stops working as soon as you have more than one remote with a
409 reference\&. This setting allows for setting the name of a preferred remote that should always win when it comes to disambiguation\&. The typical use\-case is to set this to
412 Currently this is used by
415 \fBgit-checkout\fR(1)
417 \fBgit checkout <something>\fR
419 \fBgit switch <something>\fR
422 branch on another remote, and by
423 \fBgit-worktree\fR(1)
425 \fBgit worktree add\fR
426 refers to a remote branch\&. This setting might be used for other checkout\-like commands or functionality in the future\&.
431 Provides the default value for the
438 \fBgit switch\fR\&. See
441 \fBgit-checkout\fR(1)\&.
446 The number of parallel workers to use when updating the working tree\&. The default is one, i\&.e\&. sequential execution\&. If set to a value less than one, Git will use as many workers as the number of logical cores available\&. This setting and
447 \fBcheckout\&.thresholdForParallelism\fR
448 affect all commands that perform checkout\&. E\&.g\&. checkout, clone, reset, sparse\-checkout, etc\&.
450 Note: Parallel checkout usually delivers better performance for repositories located on SSDs or over NFS\&. For repositories on spinning disks and/or machines with a small number of cores, the default sequential checkout often performs better\&. The size and compression level of a repository might also influence how well the parallel version performs\&.
453 checkout\&.thresholdForParallelism
455 When running parallel checkout with a small number of files, the cost of subprocess spawning and inter\-process communication might outweigh the parallelization gains\&. This setting allows you to define the minimum number of files for which parallel checkout should be attempted\&. The default is 100\&.
459 \fBgit-checkout\fR(1), \fBgit-branch\fR(1)
462 Part of the \fBgit\fR(1) suite