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