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