interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / git-checkout.adoc

629 lines · 22586 bytes Raw Blame History
1git-checkout(1)
2===============
3
4NAME
5----
6git-checkout - Switch branches or restore working tree files
7
8SYNOPSIS
9--------
10[synopsis]
11git checkout [-q] [-f] [-m] [<branch>]
12git checkout [-q] [-f] [-m] --detach [<branch>]
13git checkout [-q] [-f] [-m] [--detach] <commit>
14git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>]
15git checkout <tree-ish> [--] <pathspec>...
16git checkout <tree-ish> --pathspec-from-file=<file> [--pathspec-file-nul]
17git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [--] <pathspec>...
18git checkout [-f|--ours|--theirs|-m|--conflict=<style>] --pathspec-from-file=<file> [--pathspec-file-nul]
19git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
20
21DESCRIPTION
22-----------
23
24`git checkout` has two main modes:
25
261. **Switch branches**, with `git checkout <branch>`
272. **Restore a different version of a file**, for example with
28 `git checkout <commit> <filename>` or `git checkout <filename>`
29
30See <<ARGUMENT_DISAMBIGUATION,ARGUMENT DISAMBIGUATION>> below
31for how Git decides which one to do.
32
33`git checkout [<branch>]`::
34 Switch to _<branch>_. This sets the current branch to _<branch>_ and
35 updates the files in your working directory. The checkout will fail
36 if there are uncommitted changes to any files where _<branch>_ and
37 your current commit have different content. Uncommitted changes will
38 otherwise be kept.
39+
40If _<branch>_ is not found but there does exist a tracking branch in
41exactly one remote (call it _<remote>_) with a matching name and
42`--no-guess` is not specified, treat as equivalent to
43+
44------------
45$ git checkout -b <branch> --track <remote>/<branch>
46------------
47+
48Running `git checkout` without specifying a branch has no effect except
49to print out the tracking information for the current branch.
50
51`git checkout -b <new-branch> [<start-point>]`::
52
53 Create a new branch named _<new-branch>_, start it at _<start-point>_
54 (defaults to the current commit), and check out the new branch.
55 You can use the `--track` or `--no-track` options to set the branch's
56 upstream tracking information.
57+
58This will fail if there's an error checking out _<new-branch>_, for
59example if checking out the `<start-point>` commit would overwrite your
60uncommitted changes.
61
62`git checkout -B <branch> [<start-point>]`::
63
64 The same as `-b`, except that if the branch already exists it
65 resets _<branch>_ to the start point instead of failing.
66
67`git checkout --detach [<branch>]`::
68`git checkout [--detach] <commit>`::
69
70 The same as `git checkout <branch>`, except that instead of pointing
71 `HEAD` at the branch, it points `HEAD` at the commit ID.
72 See the <<DETACHED_HEAD,"DETACHED HEAD">> section below for more.
73+
74Omitting _<branch>_ detaches `HEAD` at the tip of the current branch.
75
76`git checkout <tree-ish> [--] <pathspec>...`::
77`git checkout <tree-ish> --pathspec-from-file=<file> [--pathspec-file-nul]`::
78
79 Replace the specified files and/or directories with the version from
80 the given commit or tree and add them to the index
81 (also known as "staging area").
82+
83For example, `git checkout main file.txt` will replace `file.txt`
84with the version from `main`.
85
86`git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [--] <pathspec>...`::
87`git checkout [-f|--ours|--theirs|-m|--conflict=<style>] --pathspec-from-file=<file> [--pathspec-file-nul]`::
88
89 Replace the specified files and/or directories with the version from
90 the index.
91+
92For example, if you check out a commit, edit `file.txt`, and then
93decide those changes were a mistake, `git checkout file.txt` will
94discard any unstaged changes to `file.txt`.
95+
96This will fail if the file has a merge conflict and you haven't yet run
97`git add file.txt` (or something equivalent) to mark it as resolved.
98You can use `-f` to ignore the unmerged files instead of failing, use
99`--ours` or `--theirs` to replace them with the version from a specific
100side of the merge, or use `-m` to replace them with the original
101conflicted merge result.
102
103`git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]`::
104 This is similar to the previous two modes, 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.
108
109OPTIONS
110-------
111`-q`::
112`--quiet`::
113 Quiet, suppress feedback messages.
114
115`--progress`::
116`--no-progress`::
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
122`-f`::
123`--force`::
124 When switching branches, proceed even if the index or the
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.
128+
129When checking out paths from the index, do not fail upon unmerged
130entries; instead, unmerged entries are ignored.
131
132`--ours`::
133`--theirs`::
134 When checking out paths from the index, check out stage #2
135 (`ours`) or #3 (`theirs`) for unmerged paths.
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").
151
152`-b <new-branch>`::
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.
156
157`-B <new-branch>`::
158 The same as `-b`, except that if the branch already exists it
159 resets _<branch>_ to the start point instead of failing.
160
161`-t`::
162`--track[=(direct|inherit)]`::
163 When creating a new branch, set up "upstream" configuration. See
164 `--track` in linkgit:git-branch[1] for details. As a convenience,
165 --track without -b implies branch creation.
166+
167If no `-b` option is given, the name of the new branch will be
168derived from the remote-tracking branch, by looking at the local part of
169the refspec configured for the corresponding remote, and then stripping
170the initial part up to the "*".
171This would tell us to use `hack` as the local branch when branching
172off of `origin/hack` (or `remotes/origin/hack`, or even
173`refs/remotes/origin/hack`). If the given name has no slash, or the above
174guessing results in an empty name, the guessing is aborted. You can
175explicitly give a name with `-b` in such a case.
176
177`--no-track`::
178 Do not set up "upstream" configuration, even if the
179 `branch.autoSetupMerge` configuration variable is true.
180
181`--guess`::
182`--no-guess`::
183 If _<branch>_ is not found but there does exist a tracking
184 branch in exactly one remote (call it _<remote>_) with a
185 matching name, treat as equivalent to
186+
187------------
188$ git checkout -b <branch> --track <remote>/<branch>
189------------
190+
191If the branch exists in multiple remotes and one of them is named by
192the `checkout.defaultRemote` configuration variable, we'll use that
193one for the purposes of disambiguation, even if the _<branch>_ isn't
194unique across all remotes. Set it to
195e.g. `checkout.defaultRemote=origin` to always checkout remote
196branches from there if _<branch>_ is ambiguous but exists on the
197'origin' remote. See also `checkout.defaultRemote` in
198linkgit:git-config[1].
199+
200`--guess` is the default behavior. Use `--no-guess` to disable it.
201+
202The default behavior can be set via the `checkout.guess` configuration
203variable.
204
205`-l`::
206 Create the new branch's reflog; see linkgit:git-branch[1] for
207 details.
208
209`-d`::
210`--detach`::
211 Rather than checking out a branch to work on it, check out a
212 commit for inspection and discardable experiments.
213 This is the default behavior of `git checkout <commit>` when
214 _<commit>_ is not a branch name. See the
215 <<DETACHED_HEAD,"DETACHED HEAD">> section below for details.
216
217`--orphan <new-branch>`::
218 Create a new unborn branch, named _<new-branch>_, started from
219 _<start-point>_ and switch to it. The first commit made on this
220 new branch will have no parents and it will be the root of a new
221 history totally disconnected from all the other branches and
222 commits.
223+
224The index and the working tree are adjusted as if you had previously run
225`git checkout <start-point>`. This allows you to start a new history
226that records a set of paths similar to _<start-point>_ by easily running
227`git commit -a` to make the root commit.
228+
229This can be useful when you want to publish the tree from a commit
230without exposing its full history. You might want to do this to publish
231an open source branch of a project whose current tree is "clean", but
232whose full history contains proprietary or otherwise encumbered bits of
233code.
234+
235If you want to start a disconnected history that records a set of paths
236that is totally different from the one of _<start-point>_, then you should
237clear the index and the working tree right after creating the orphan
238branch by running `git rm -rf .` from the top level of the working tree.
239Afterwards you will be ready to prepare your new files, repopulating the
240working tree, by copying them from elsewhere, extracting a tarball, etc.
241
242`--ignore-skip-worktree-bits`::
243 In sparse checkout mode, `git checkout -- <path>...` would
244 update only entries matched by _<paths>_ and sparse patterns
245 in `$GIT_DIR/info/sparse-checkout`. This option ignores
246 the sparse patterns and adds back any files in `<path>...`.
247
248`-m`::
249`--merge`::
250 When switching branches,
251 if you have local modifications to one or more files that
252 are different between the current branch and the branch to
253 which you are switching, the command refuses to switch
254 branches in order to preserve your modifications in context.
255 With this option, the conflicting local changes are
256 automatically stashed before the switch and reapplied
257 afterwards. If the local changes do not overlap with the
258 differences between branches, the switch proceeds without
259 stashing. If reapplying the stash results in conflicts, the
260 entry is saved to the stash list. Resolve the conflicts
261 and run `git stash drop` when done, or clear the working
262 tree (e.g. with `git reset --hard`) before running `git stash
263 pop` later to re-apply your changes.
264+
265When checking out paths from the index, this option lets you recreate
266the conflicted merge in the specified paths. This option cannot be
267used when checking out paths from a tree-ish.
268
269`--conflict=<style>`::
270 The same as `--merge` option above, but changes the way the
271 conflicting hunks are presented, overriding the
272 `merge.conflictStyle` configuration variable. Possible values are
273 `merge` (default), `diff3`, and `zdiff3`.
274
275`-p`::
276`--patch`::
277 Interactively select hunks in the difference between the
278 _<tree-ish>_ (or the index, if unspecified) and the working
279 tree. The chosen hunks are then applied in reverse to the
280 working tree (and if a _<tree-ish>_ was specified, the index).
281+
282This means that you can use `git checkout -p` to selectively discard
283edits from your current working tree. See the "Interactive Mode"
284section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
285+
286Note that this option uses the no overlay mode by default (see also
287`--overlay`), and currently doesn't support overlay mode.
288
289include::diff-context-options.adoc[]
290
291`--ignore-other-worktrees`::
292 `git checkout` refuses when the wanted branch is already checked
293 out or otherwise in use by another worktree. This option makes
294 it check the branch out anyway. In other words, the branch can
295 be in use by more than one worktree.
296
297`--overwrite-ignore`::
298`--no-overwrite-ignore`::
299 Silently overwrite ignored files when switching branches. This
300 is the default behavior. Use `--no-overwrite-ignore` to abort
301 the operation when the new branch contains ignored files.
302
303`--recurse-submodules`::
304`--no-recurse-submodules`::
305 Using `--recurse-submodules` will update the content of all active
306 submodules according to the commit recorded in the superproject. If
307 local modifications in a submodule would be overwritten the checkout
308 will fail unless `-f` is used. If nothing (or `--no-recurse-submodules`)
309 is used, submodules working trees will not be updated.
310 Just like linkgit:git-submodule[1], this will detach `HEAD` of the
311 submodule.
312
313`--overlay`::
314`--no-overlay`::
315 In the default overlay mode, `git checkout` never
316 removes files from the index or the working tree. When
317 specifying `--no-overlay`, files that appear in the index and
318 working tree, but not in _<tree-ish>_ are removed, to make them
319 match _<tree-ish>_ exactly.
320
321`--pathspec-from-file=<file>`::
322 Pathspec is passed in _<file>_ instead of commandline args. If
323 _<file>_ is exactly `-` then standard input is used. Pathspec
324 elements are separated by _LF_ or _CR_/_LF_. Pathspec elements can be
325 quoted as explained for the configuration variable `core.quotePath`
326 (see linkgit:git-config[1]). See also `--pathspec-file-nul` and
327 global `--literal-pathspecs`.
328
329`--pathspec-file-nul`::
330 Only meaningful with `--pathspec-from-file`. Pathspec elements are
331 separated with _NUL_ character and all other characters are taken
332 literally (including newlines and quotes).
333
334`<branch>`::
335 Branch to checkout; if it refers to a branch (i.e., a name that,
336 when prepended with "refs/heads/", is a valid ref), then that
337 branch is checked out. Otherwise, if it refers to a valid
338 commit, your `HEAD` becomes "detached" and you are no longer on
339 any branch (see below for details).
340+
341You can use the `@{-N}` syntax to refer to the N-th last
342branch/commit checked out using "git checkout" operation. You may
343also specify `-` which is synonymous to `@{-1}`.
344+
345As a special case, you may use `<rev-a>...<rev-b>` as a shortcut for the
346merge base of _<rev-a>_ and _<rev-b>_ if there is exactly one merge base. You can
347leave out at most one of _<rev-a>_ and _<rev-b>_, in which case it defaults to `HEAD`.
348
349_<new-branch>_::
350 Name for the new branch.
351
352_<start-point>_::
353 The name of a commit at which to start the new branch; see
354 linkgit:git-branch[1] for details. Defaults to `HEAD`.
355+
356As a special case, you may use `<rev-a>...<rev-b>` as a shortcut for the
357merge base of _<rev-a>_ and _<rev-b>_ if there is exactly one merge base. You can
358leave out at most one of _<rev-a>_ and _<rev-b>_, in which case it defaults to `HEAD`.
359
360_<tree-ish>_::
361 Tree to checkout from (when paths are given). If not specified,
362 the index will be used.
363+
364As a special case, you may use `<rev-a>...<rev-b>` as a shortcut for the
365merge base of _<rev-a>_ and _<rev-b>_ if there is exactly one merge base. You can
366leave out at most one of _<rev-a>_ and _<rev-b>_, in which case it defaults to `HEAD`.
367
368`--`::
369 Do not interpret any more arguments as options.
370
371`<pathspec>...`::
372 Limits the paths affected by the operation.
373+
374For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
375
376[[DETACHED_HEAD]]
377DETACHED HEAD
378-------------
379`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
380branch refers to a specific commit. Let's look at a repo with three
381commits, one of them tagged, and with branch `master` checked out:
382
383------------
384 HEAD (refers to branch 'master')
385 |
386 v
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
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`:
398
399------------
400$ edit; git add; git commit
401
402 HEAD (refers to branch 'master')
403 |
404 v
405a---b---c---d branch 'master' (refers to commit 'd')
406 ^
407 |
408 tag 'v2.0' (refers to commit 'b')
409------------
410
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
414checkout commit `b` (here we show two ways this may be done):
415
416------------
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------------
428
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
432referring to a named branch. Let's see what happens when we create a commit:
433
434------------
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')
446------------
447
448There is now a new commit `e`, but it is referenced only by `HEAD`. We can
449of course add yet another commit in this state:
450
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
465In fact, we can perform all the normal Git operations. But, let's look
466at what happens when we then checkout `master`:
467
468------------
469$ git checkout master
470
471 HEAD (refers to branch 'master')
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
481`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
482by the routine Git garbage collection process, unless we create a reference
483before that happens. If we have not yet moved away from commit `f`,
484any of these will create a reference to it:
485
486------------
487$ git checkout -b foo # or "git switch -c foo" <1>
488$ git branch foo <2>
489$ git tag foo <3>
490------------
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.
494<2> similarly creates a new branch `foo`, which refers to commit `f`,
495 but leaves `HEAD` detached.
496<3> creates a new tag `foo`, which refers to commit `f`,
497 leaving `HEAD` detached.
498
499If we have moved away from commit `f`, then we must first recover its object
500name (typically by using git reflog), and then we can create a reference to
501it. For example, to see the last two commits to which `HEAD` referred, we
502can use either of these commands:
503
504------------
505$ git reflog -2 HEAD # or
506$ git log -g -2 HEAD
507------------
508
509[[ARGUMENT_DISAMBIGUATION]]
510ARGUMENT DISAMBIGUATION
511-----------------------
512
513When you run `git checkout <something>`, Git tries to guess whether
514_<something>_ is intended to be a branch, a commit, or a set of file(s),
515and then either switches to that branch or commit, or restores the
516specified files.
517
518If there's any ambiguity, Git will treat `<something>` as a branch or
519commit, but you can use the double dash `--` to force Git to treat the
520parameter as a list of files and/or directories, like this:
521
522----------
523git checkout -- file.txt
524----------
525
526EXAMPLES
527--------
528
529=== 1. Paths
530
531The following sequence checks out the `master` branch, reverts
532the `Makefile` to two revisions back, deletes `hello.c` by
533mistake, and gets it back from the index.
534
535------------
536$ git checkout master <1>
537$ git checkout master~2 Makefile <2>
538$ rm -f hello.c
539$ git checkout hello.c <3>
540------------
541<1> switch branch
542<2> take a file out of another commit
543<3> restore `hello.c` from the index
544
545If you want to check out _all_ C source files out of the index,
546you can say
547
548------------
549$ git checkout -- '*.c'
550------------
551
552Note the quotes around `*.c`. The file `hello.c` will also be
553checked out, even though it is no longer in the working tree,
554because the file globbing is used to match entries in the index
555(not in the working tree by the shell).
556
557If you have an unfortunate branch that is named `hello.c`, this
558step would be confused as an instruction to switch to that branch.
559You should instead write:
560
561------------
562$ git checkout -- hello.c
563------------
564
565=== 2. Merge
566
567After working in the wrong branch, switching to the correct
568branch would be done using:
569
570------------
571$ git checkout mytopic
572------------
573
574However, your "wrong" branch and correct `mytopic` branch may
575differ in files that you have modified locally, in which case
576the above checkout would fail like this:
577
578------------
579$ git checkout mytopic
580error: You have local changes to 'frotz'; not switching branches.
581------------
582
583You can give the `-m` flag to the command, which will carry your local
584changes to the new branch:
585
586------------
587$ git checkout -m mytopic
588Applied autostash.
589Switched to branch 'mytopic'
590The following paths have local changes:
591M frotz
592------------
593
594After the switch, the local modifications are reapplied and are _not_
595registered in your index file, so `git diff` would show you what
596changes you made since the tip of the new branch.
597
598=== 3. Merge conflict
599
600When the `--merge` (`-m`) option is given and the local changes
601overlap with the changes in the branch we're switching to, the
602changes are stashed and reapplied after the switch. If this
603process results in conflicts, the stash entry is saved and a
604message is printed:
605
606------------
607$ git checkout -m mytopic
608Your local changes are stashed, however applying them
609resulted in conflicts. You can either resolve the conflicts
610and then discard the stash with "git stash drop", or, if you
611do not want to resolve them now, run "git reset --hard" and
612apply the local changes later by running "git stash pop".
613------------
614
615CONFIGURATION
616-------------
617
618include::includes/cmd-config-section-all.adoc[]
619
620include::config/checkout.adoc[]
621
622SEE ALSO
623--------
624linkgit:git-switch[1],
625linkgit:git-restore[1]
626
627GIT
628---
629Part of the linkgit:git[1] suite