]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-checkout.txt
Start the 2.46 cycle
[thirdparty/git.git] / Documentation / git-checkout.txt
CommitLineData
215a7ad1
JH
1git-checkout(1)
2===============
7fc9d69f
JH
3
4NAME
5----
c4ac525c 6git-checkout - Switch branches or restore working tree files
7fc9d69f
JH
7
8SYNOPSIS
9--------
71bb1033 10[verse]
76cfadfc 11'git checkout' [-q] [-f] [-m] [<branch>]
26776c97
JH
12'git checkout' [-q] [-f] [-m] --detach [<branch>]
13'git checkout' [-q] [-f] [-m] [--detach] <commit>
133db54d 14'git checkout' [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>]
54f98fee
JH
15'git checkout' [-f] <tree-ish> [--] <pathspec>...
16'git checkout' [-f] <tree-ish> --pathspec-from-file=<file> [--pathspec-file-nul]
17'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [--] <pathspec>...
18'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] --pathspec-from-file=<file> [--pathspec-file-nul]
8ea1189e 19'git checkout' (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
7fc9d69f
JH
20
21DESCRIPTION
22-----------
b831deda 23Updates files in the working tree to match the version in the index
8ea1189e 24or the specified tree. If no pathspec was given, 'git checkout' will
b831deda 25also update `HEAD` to set the specified branch as the current
76cfadfc 26branch.
4aaa7027 27
37f80025 28'git checkout' [<branch>]::
181e3725 29 To prepare for working on `<branch>`, switch to it by updating
e1cdf633 30 the index and the files in the working tree, and by pointing
181e3725 31 `HEAD` at the branch. Local modifications to the files in the
e1cdf633 32 working tree are kept, so that they can be committed to the
181e3725 33 `<branch>`.
e1cdf633 34+
181e3725 35If `<branch>` is not found but there does exist a tracking branch in
ccb111b3
NTND
36exactly one remote (call it `<remote>`) with a matching name and
37`--no-guess` is not specified, treat as equivalent to
00bb4378
CR
38+
39------------
40$ git checkout -b <branch> --track <remote>/<branch>
41------------
42+
181e3725 43You could omit `<branch>`, in which case the command degenerates to
be94568b 44"check out the current branch", which is a glorified no-op with
e1cdf633 45rather expensive side-effects to show only the tracking information,
89363522 46if it exists, for the current branch.
e1cdf633 47
133db54d 48'git checkout' -b|-B <new-branch> [<start-point>]::
4aaa7027 49
e1cdf633
CR
50 Specifying `-b` causes a new branch to be created as if
51 linkgit:git-branch[1] were called and then checked out. In
52 this case you can use the `--track` or `--no-track` options,
53 which will be passed to 'git branch'. As a convenience,
54 `--track` without `-b` implies branch creation; see the
55 description of `--track` below.
02ac9837 56+
133db54d 57If `-B` is given, `<new-branch>` is created if it doesn't exist; otherwise, it
02ac9837
TRC
58is reset. This is the transactional equivalent of
59+
60------------
133db54d 61$ git branch -f <branch> [<start-point>]
02ac9837
TRC
62$ git checkout <branch>
63------------
64+
65that is to say, the branch is not reset/created unless "git checkout" is
b23285a9
JH
66successful (e.g., when the branch is in use in another worktree, not
67just the current branch stays the same, but the branch is not reset to
68the start-point, either).
bb0ceb62 69
e1cdf633 70'git checkout' --detach [<branch>]::
26776c97 71'git checkout' [--detach] <commit>::
e1cdf633 72
181e3725 73 Prepare to work on top of `<commit>`, by detaching `HEAD` at it
e1cdf633
CR
74 (see "DETACHED HEAD" section), and updating the index and the
75 files in the working tree. Local modifications to the files
76 in the working tree are kept, so that the resulting working
77 tree will be the state recorded in the commit plus the local
78 modifications.
79+
181e3725
NTND
80When the `<commit>` argument is a branch name, the `--detach` option can
81be used to detach `HEAD` at the tip of the branch (`git checkout
82<branch>` would check out that branch without detaching `HEAD`).
26776c97 83+
181e3725 84Omitting `<branch>` detaches `HEAD` at the tip of the current branch.
e1cdf633 85
8ea1189e 86'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>...::
a9aecc7a 87'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]::
4aaa7027 88
8ea1189e
AM
89 Overwrite the contents of the files that match the pathspec.
90 When the `<tree-ish>` (most often a commit) is not given,
91 overwrite working tree with the contents in the index.
92 When the `<tree-ish>` is given, overwrite both the index and
93 the working tree with the contents at the `<tree-ish>`.
c4ac525c 94+
b831deda
JN
95The index may contain unmerged entries because of a previous failed merge.
96By default, if you try to check out such an entry from the index, the
db941099 97checkout operation will fail and nothing will be checked out.
b831deda 98Using `-f` will ignore these unmerged entries. The contents from a
38901a48 99specific side of the merge can be checked out of the index by
b831deda
JN
100using `--ours` or `--theirs`. With `-m`, changes made to the working tree
101file can be discarded to re-create the original conflicted merge result.
7fc9d69f 102
b59698ae 103'git checkout' (-p|--patch) [<tree-ish>] [--] [<pathspec>...]::
6fdc9ad2
AM
104 This is similar to the previous mode, but lets you use the
105 interactive interface to show the "diff" output and choose which
106 hunks to use in the result. See below for the description of
107 `--patch` option.
b59698ae 108
7fc9d69f
JH
109OPTIONS
110-------
6124aee5 111-q::
f7aec129 112--quiet::
2be7fcb4 113 Quiet, suppress feedback messages.
6124aee5 114
d333f672
NTND
115--progress::
116--no-progress::
870ebdb9
ECA
117 Progress status is reported on the standard error stream
118 by default when it is attached to a terminal, unless `--quiet`
119 is specified. This flag enables progress reporting even if not
120 attached to a terminal, regardless of `--quiet`.
121
0270f7c5 122-f::
f7aec129 123--force::
db941099 124 When switching branches, proceed even if the index or the
0e29222e
EN
125 working tree differs from `HEAD`, and even if there are untracked
126 files in the way. This is used to throw away local changes and
127 any untracked files or directories that are in the way.
db941099
JH
128+
129When checking out paths from the index, do not fail upon unmerged
130entries; instead, unmerged entries are ignored.
0270f7c5 131
38901a48
JH
132--ours::
133--theirs::
134 When checking out paths from the index, check out stage #2
135 ('ours') or #3 ('theirs') for unmerged paths.
f3030165
SE
136+
137Note that during `git rebase` and `git pull --rebase`, 'ours' and
138'theirs' may appear swapped; `--ours` gives the version from the
139branch the changes are rebased onto, while `--theirs` gives the
140version from the branch that holds your work that is being rebased.
141+
142This is because `rebase` is used in a workflow that treats the
143history at the remote as the shared canonical one, and treats the
144work done on the branch you are rebasing as the third-party work to
145be integrated, and you are temporarily assuming the role of the
146keeper of the canonical history during the rebase. As the keeper of
147the canonical history, you need to view the history from the remote
148as `ours` (i.e. "our shared canonical history"), while what you did
149on your side branch as `theirs` (i.e. "one contributor's work on top
150of it").
0270f7c5 151
133db54d 152-b <new-branch>::
fedb8ea2
JH
153 Create a new branch named `<new-branch>`, start it at
154 `<start-point>`, and check the resulting branch out;
155 see linkgit:git-branch[1] for details.
7fc9d69f 156
133db54d 157-B <new-branch>::
fedb8ea2
JH
158 Creates the branch `<new-branch>`, start it at `<start-point>`;
159 if it already exists, then reset it to `<start-point>`. And then
160 check the resulting branch out. This is equivalent to running
161 "git branch" with "-f" followed by "git checkout" of that branch;
162 see linkgit:git-branch[1] for details.
02ac9837 163
3240240f 164-t::
6327f0ef 165--track[=(direct|inherit)]::
26d22dc6
JK
166 When creating a new branch, set up "upstream" configuration. See
167 "--track" in linkgit:git-branch[1] for details.
bb0ceb62 168+
23f8239b 169If no `-b` option is given, the name of the new branch will be
fa83a33b
JH
170derived from the remote-tracking branch, by looking at the local part of
171the refspec configured for the corresponding remote, and then stripping
172the initial part up to the "*".
181e3725
NTND
173This would tell us to use `hack` as the local branch when branching
174off of `origin/hack` (or `remotes/origin/hack`, or even
175`refs/remotes/origin/hack`). If the given name has no slash, or the above
9188ed89 176guessing results in an empty name, the guessing is aborted. You can
23f8239b 177explicitly give a name with `-b` in such a case.
0746d19a
PB
178
179--no-track::
167d7445 180 Do not set up "upstream" configuration, even if the
181e3725 181 `branch.autoSetupMerge` configuration variable is true.
0746d19a 182
ccb111b3
NTND
183--guess::
184--no-guess::
185 If `<branch>` is not found but there does exist a tracking
186 branch in exactly one remote (call it `<remote>`) with a
187 matching name, treat as equivalent to
188+
189------------
190$ git checkout -b <branch> --track <remote>/<branch>
191------------
192+
193If the branch exists in multiple remotes and one of them is named by
194the `checkout.defaultRemote` configuration variable, we'll use that
195one for the purposes of disambiguation, even if the `<branch>` isn't
196unique across all remotes. Set it to
197e.g. `checkout.defaultRemote=origin` to always checkout remote
198branches from there if `<branch>` is ambiguous but exists on the
199'origin' remote. See also `checkout.defaultRemote` in
200linkgit:git-config[1].
201+
64f1f58f
DL
202`--guess` is the default behavior. Use `--no-guess` to disable it.
203+
204The default behavior can be set via the `checkout.guess` configuration
205variable.
0746d19a 206
969d326d 207-l::
26d22dc6
JK
208 Create the new branch's reflog; see linkgit:git-branch[1] for
209 details.
969d326d 210
07351d98 211-d::
32669671
JH
212--detach::
213 Rather than checking out a branch to work on it, check out a
214 commit for inspection and discardable experiments.
181e3725
NTND
215 This is the default behavior of `git checkout <commit>` when
216 `<commit>` is not a branch name. See the "DETACHED HEAD" section
32669671
JH
217 below for details.
218
133db54d 219--orphan <new-branch>::
49dc1563 220 Create a new unborn branch, named `<new-branch>`, started from
133db54d 221 `<start-point>` and switch to it. The first commit made on this
feb98d13
EM
222 new branch will have no parents and it will be the root of a new
223 history totally disconnected from all the other branches and
224 commits.
9db5ebf4 225+
feb98d13 226The index and the working tree are adjusted as if you had previously run
133db54d
JNA
227`git checkout <start-point>`. This allows you to start a new history
228that records a set of paths similar to `<start-point>` by easily running
181e3725 229`git commit -a` to make the root commit.
9db5ebf4 230+
feb98d13
EM
231This can be useful when you want to publish the tree from a commit
232without exposing its full history. You might want to do this to publish
233an open source branch of a project whose current tree is "clean", but
234whose full history contains proprietary or otherwise encumbered bits of
235code.
236+
237If you want to start a disconnected history that records a set of paths
133db54d 238that is totally different from the one of `<start-point>`, then you should
feb98d13 239clear the index and the working tree right after creating the orphan
181e3725 240branch by running `git rm -rf .` from the top level of the working tree.
feb98d13
EM
241Afterwards you will be ready to prepare your new files, repopulating the
242working tree, by copying them from elsewhere, extracting a tarball, etc.
9db5ebf4 243
08d595dc
NTND
244--ignore-skip-worktree-bits::
245 In sparse checkout mode, `git checkout -- <paths>` would
181e3725
NTND
246 update only entries matched by `<paths>` and sparse patterns
247 in `$GIT_DIR/info/sparse-checkout`. This option ignores
248 the sparse patterns and adds back any files in `<paths>`.
08d595dc 249
1be0659e 250-m::
eac5a401 251--merge::
0cf8581e
JH
252 When switching branches,
253 if you have local modifications to one or more files that
71bb1033
JL
254 are different between the current branch and the branch to
255 which you are switching, the command refuses to switch
256 branches in order to preserve your modifications in context.
257 However, with this option, a three-way merge between the current
1be0659e
JH
258 branch, your working tree contents, and the new branch
259 is done, and you will be on the new branch.
260+
261When a merge conflict happens, the index entries for conflicting
262paths are left unmerged, and you need to resolve the conflicts
d7f078b8
SP
263and mark the resolved paths with `git add` (or `git rm` if the merge
264should result in deletion of the path).
0cf8581e
JH
265+
266When checking out paths from the index, this option lets you recreate
54f98fee
JH
267the conflicted merge in the specified paths. This option cannot be
268used when checking out paths from a tree-ish.
a7256deb
NTND
269+
270When switching branches with `--merge`, staged changes may be lost.
1be0659e 271
eac5a401 272--conflict=<style>::
181e3725 273 The same as `--merge` option above, but changes the way the
eac5a401 274 conflicting hunks are presented, overriding the
181e3725 275 `merge.conflictStyle` configuration variable. Possible values are
ddfc44a8 276 "merge" (default), "diff3", and "zdiff3".
1be0659e 277
4f353658
TR
278-p::
279--patch::
280 Interactively select hunks in the difference between the
181e3725 281 `<tree-ish>` (or the index, if unspecified) and the working
4f353658 282 tree. The chosen hunks are then applied in reverse to the
181e3725 283 working tree (and if a `<tree-ish>` was specified, the index).
4f353658
TR
284+
285This means that you can use `git checkout -p` to selectively discard
a31538e2 286edits from your current working tree. See the ``Interactive Mode''
6cf378f0 287section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
091e04bc
TG
288+
289Note that this option uses the no overlay mode by default (see also
d333f672 290`--overlay`), and currently doesn't support overlay mode.
4f353658 291
1d0fa898
NTND
292--ignore-other-worktrees::
293 `git checkout` refuses when the wanted ref is already checked
294 out by another worktree. This option makes it check the ref
295 out anyway. In other words, the ref can be held by more than one
296 worktree.
297
9d223d43
NTND
298--overwrite-ignore::
299--no-overwrite-ignore::
300 Silently overwrite ignored files when switching branches. This
301 is the default behavior. Use `--no-overwrite-ignore` to abort
302 the operation when the new branch contains ignored files.
303
d333f672
NTND
304--recurse-submodules::
305--no-recurse-submodules::
acbfae32 306 Using `--recurse-submodules` will update the content of all active
1fc458d9
SB
307 submodules according to the commit recorded in the superproject. If
308 local modifications in a submodule would be overwritten the checkout
181e3725 309 will fail unless `-f` is used. If nothing (or `--no-recurse-submodules`)
b3cec573 310 is used, submodules working trees will not be updated.
181e3725
NTND
311 Just like linkgit:git-submodule[1], this will detach `HEAD` of the
312 submodule.
be4908f1 313
d333f672
NTND
314--overlay::
315--no-overlay::
091e04bc
TG
316 In the default overlay mode, `git checkout` never
317 removes files from the index or the working tree. When
318 specifying `--no-overlay`, files that appear in the index and
181e3725
NTND
319 working tree, but not in `<tree-ish>` are removed, to make them
320 match `<tree-ish>` exactly.
091e04bc 321
a9aecc7a
AM
322--pathspec-from-file=<file>::
323 Pathspec is passed in `<file>` instead of commandline args. If
324 `<file>` is exactly `-` then standard input is used. Pathspec
325 elements are separated by LF or CR/LF. Pathspec elements can be
326 quoted as explained for the configuration variable `core.quotePath`
327 (see linkgit:git-config[1]). See also `--pathspec-file-nul` and
328 global `--literal-pathspecs`.
329
330--pathspec-file-nul::
331 Only meaningful with `--pathspec-from-file`. Pathspec elements are
332 separated with NUL character and all other characters are taken
333 literally (including newlines and quotes).
334
0270f7c5 335<branch>::
0808723b
JK
336 Branch to checkout; if it refers to a branch (i.e., a name that,
337 when prepended with "refs/heads/", is a valid ref), then that
338 branch is checked out. Otherwise, if it refers to a valid
181e3725 339 commit, your `HEAD` becomes "detached" and you are no longer on
0808723b 340 any branch (see below for details).
696acf45 341+
181e3725 342You can use the `@{-N}` syntax to refer to the N-th last
75ce1495 343branch/commit checked out using "git checkout" operation. You may
181e3725 344also specify `-` which is synonymous to `@{-1}`.
873c3472 345+
181e3725 346As a special case, you may use `A...B` as a shortcut for the
873c3472
MG
347merge base of `A` and `B` if there is exactly one merge base. You can
348leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
5e1a2e8c 349
133db54d 350<new-branch>::
76cfadfc
JK
351 Name for the new branch.
352
133db54d 353<start-point>::
76cfadfc 354 The name of a commit at which to start the new branch; see
181e3725 355 linkgit:git-branch[1] for details. Defaults to `HEAD`.
e3d6539d
DL
356+
357As a special case, you may use `"A...B"` as a shortcut for the
358merge base of `A` and `B` if there is exactly one merge base. You can
359leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
76cfadfc
JK
360
361<tree-ish>::
362 Tree to checkout from (when paths are given). If not specified,
363 the index will be used.
c693ef78
DL
364+
365As a special case, you may use `"A...B"` as a shortcut for the
366merge base of `A` and `B` if there is exactly one merge base. You can
367leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
76cfadfc 368
8ea1189e
AM
369\--::
370 Do not interpret any more arguments as options.
76cfadfc 371
8ea1189e
AM
372<pathspec>...::
373 Limits the paths affected by the operation.
374+
375For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
5e1a2e8c 376
32669671 377DETACHED HEAD
5e1a2e8c 378-------------
181e3725 379`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
be8ef33c 380branch refers to a specific commit. Let's look at a repo with three
181e3725 381commits, one of them tagged, and with branch `master` checked out:
5e1a2e8c 382
be8ef33c 383------------
39a36827
AH
384 HEAD (refers to branch 'master')
385 |
386 v
be8ef33c
JS
387a---b---c branch 'master' (refers to commit 'c')
388 ^
389 |
390 tag 'v2.0' (refers to commit 'b')
391------------
392
393When a commit is created in this state, the branch is updated to refer to
181e3725
NTND
394the new commit. Specifically, 'git commit' creates a new commit `d`, whose
395parent is commit `c`, and then updates branch `master` to refer to new
396commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers
397to commit `d`:
5e1a2e8c
JH
398
399------------
be8ef33c
JS
400$ edit; git add; git commit
401
39a36827
AH
402 HEAD (refers to branch 'master')
403 |
404 v
be8ef33c
JS
405a---b---c---d branch 'master' (refers to commit 'd')
406 ^
407 |
408 tag 'v2.0' (refers to commit 'b')
5e1a2e8c
JH
409------------
410
be8ef33c
JS
411It is sometimes useful to be able to checkout a commit that is not at
412the tip of any named branch, or even to create a new commit that is not
413referenced by a named branch. Let's look at what happens when we
181e3725 414checkout commit `b` (here we show two ways this may be done):
cec8d146
JH
415
416------------
be8ef33c
JS
417$ git checkout v2.0 # or
418$ git checkout master^^
419
420 HEAD (refers to commit 'b')
421 |
422 v
423a---b---c---d branch 'master' (refers to commit 'd')
424 ^
425 |
426 tag 'v2.0' (refers to commit 'b')
427------------
5e1a2e8c 428
181e3725
NTND
429Notice that regardless of which checkout command we use, `HEAD` now refers
430directly to commit `b`. This is known as being in detached `HEAD` state.
431It means simply that `HEAD` refers to a specific commit, as opposed to
be8ef33c 432referring to a named branch. Let's see what happens when we create a commit:
cec8d146 433
cec8d146 434------------
be8ef33c
JS
435$ edit; git add; git commit
436
437 HEAD (refers to commit 'e')
438 |
439 v
440 e
441 /
442a---b---c---d branch 'master' (refers to commit 'd')
443 ^
444 |
445 tag 'v2.0' (refers to commit 'b')
cec8d146 446------------
7fc9d69f 447
181e3725 448There is now a new commit `e`, but it is referenced only by `HEAD`. We can
be8ef33c 449of course add yet another commit in this state:
7fc9d69f 450
be8ef33c
JS
451------------
452$ edit; git add; git commit
453
454 HEAD (refers to commit 'f')
455 |
456 v
457 e---f
458 /
459a---b---c---d branch 'master' (refers to commit 'd')
460 ^
461 |
462 tag 'v2.0' (refers to commit 'b')
463------------
464
2de9b711 465In fact, we can perform all the normal Git operations. But, let's look
181e3725 466at what happens when we then checkout `master`:
be8ef33c
JS
467
468------------
469$ git checkout master
470
39a36827 471 HEAD (refers to branch 'master')
be8ef33c
JS
472 e---f |
473 / v
474a---b---c---d branch 'master' (refers to commit 'd')
475 ^
476 |
477 tag 'v2.0' (refers to commit 'b')
478------------
479
480It is important to realize that at this point nothing refers to commit
181e3725 481`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
2de9b711 482by the routine Git garbage collection process, unless we create a reference
181e3725 483before that happens. If we have not yet moved away from commit `f`,
be8ef33c
JS
484any of these will create a reference to it:
485
486------------
9e37969e
YO
487$ git checkout -b foo # or "git switch -c foo" <1>
488$ git branch foo <2>
489$ git tag foo <3>
be8ef33c 490------------
181e3725
NTND
491<1> creates a new branch `foo`, which refers to commit `f`, and then
492 updates `HEAD` to refer to branch `foo`. In other words, we'll no longer
493 be in detached `HEAD` state after this command.
181e3725
NTND
494<2> similarly creates a new branch `foo`, which refers to commit `f`,
495 but leaves `HEAD` detached.
181e3725
NTND
496<3> creates a new tag `foo`, which refers to commit `f`,
497 leaving `HEAD` detached.
be8ef33c 498
181e3725 499If we have moved away from commit `f`, then we must first recover its object
be8ef33c 500name (typically by using git reflog), and then we can create a reference to
181e3725 501it. For example, to see the last two commits to which `HEAD` referred, we
be8ef33c
JS
502can use either of these commands:
503
504------------
505$ git reflog -2 HEAD # or
506$ git log -g -2 HEAD
507------------
4aaa7027 508
19e56563
NTND
509ARGUMENT DISAMBIGUATION
510-----------------------
511
181e3725
NTND
512When there is only one argument given and it is not `--` (e.g. `git
513checkout abc`), and when the argument is both a valid `<tree-ish>`
514(e.g. a branch `abc` exists) and a valid `<pathspec>` (e.g. a file
19e56563
NTND
515or a directory whose name is "abc" exists), Git would usually ask
516you to disambiguate. Because checking out a branch is so common an
181e3725 517operation, however, `git checkout abc` takes "abc" as a `<tree-ish>`
19e56563
NTND
518in such a situation. Use `git checkout -- <pathspec>` if you want
519to checkout these paths out of the index.
520
1be0659e
JH
521EXAMPLES
522--------
4aaa7027 523
8dda6c3d
FC
524=== 1. Paths
525
526The following sequence checks out the `master` branch, reverts
527the `Makefile` to two revisions back, deletes `hello.c` by
528mistake, and gets it back from the index.
529
4aaa7027 530------------
48aeecdc
SE
531$ git checkout master <1>
532$ git checkout master~2 Makefile <2>
4aaa7027 533$ rm -f hello.c
48aeecdc
SE
534$ git checkout hello.c <3>
535------------
1e2ccd3a 536<1> switch branch
c7cb12b8 537<2> take a file out of another commit
181e3725 538<3> restore `hello.c` from the index
8dda6c3d 539
caae319e
JH
540If you want to check out _all_ C source files out of the index,
541you can say
8dda6c3d 542
caae319e
JH
543------------
544$ git checkout -- '*.c'
545------------
8dda6c3d 546
caae319e
JH
547Note the quotes around `*.c`. The file `hello.c` will also be
548checked out, even though it is no longer in the working tree,
549because the file globbing is used to match entries in the index
550(not in the working tree by the shell).
8dda6c3d 551
48aeecdc
SE
552If you have an unfortunate branch that is named `hello.c`, this
553step would be confused as an instruction to switch to that branch.
554You should instead write:
8dda6c3d 555
4aaa7027
JH
556------------
557$ git checkout -- hello.c
558------------
559
8dda6c3d
FC
560=== 2. Merge
561
562After working in the wrong branch, switching to the correct
563branch would be done using:
564
1be0659e
JH
565------------
566$ git checkout mytopic
567------------
8dda6c3d 568
181e3725 569However, your "wrong" branch and correct `mytopic` branch may
c7cb12b8 570differ in files that you have modified locally, in which case
1be0659e 571the above checkout would fail like this:
8dda6c3d 572
1be0659e
JH
573------------
574$ git checkout mytopic
142183d0 575error: You have local changes to 'frotz'; not switching branches.
1be0659e 576------------
8dda6c3d 577
1be0659e
JH
578You can give the `-m` flag to the command, which would try a
579three-way merge:
8dda6c3d 580
1be0659e
JH
581------------
582$ git checkout -m mytopic
583Auto-merging frotz
584------------
8dda6c3d 585
1be0659e
JH
586After this three-way merge, the local modifications are _not_
587registered in your index file, so `git diff` would show you what
588changes you made since the tip of the new branch.
589
8dda6c3d
FC
590=== 3. Merge conflict
591
592When a merge conflict happens during switching branches with
593the `-m` option, you would see something like this:
594
1be0659e
JH
595------------
596$ git checkout -m mytopic
597Auto-merging frotz
1be0659e
JH
598ERROR: Merge conflict in frotz
599fatal: merge program failed
600------------
8dda6c3d 601
1be0659e
JH
602At this point, `git diff` shows the changes cleanly merged as in
603the previous example, as well as the changes in the conflicted
604files. Edit and resolve the conflict and mark it resolved with
d7f078b8 605`git add` as usual:
8dda6c3d 606
1be0659e
JH
607------------
608$ edit frotz
d7f078b8 609$ git add frotz
1be0659e
JH
610------------
611
9274dea3
ÆAB
612CONFIGURATION
613-------------
614
615include::includes/cmd-config-section-all.txt[]
616
617include::config/checkout.txt[]
618
d787d311
NTND
619SEE ALSO
620--------
46e91b66
NTND
621linkgit:git-switch[1],
622linkgit:git-restore[1]
d787d311 623
7fc9d69f
JH
624GIT
625---
9e1f0a85 626Part of the linkgit:git[1] suite