1GIT-CHECKOUT(1)                   Git Manual                   GIT-CHECKOUT(1)
2
3
4

NAME

6       git-checkout - Switch branches or restore working tree files
7

SYNOPSIS

9       git checkout [-q] [-f] [-m] [<branch>]
10       git checkout [-q] [-f] [-m] --detach [<branch>]
11       git checkout [-q] [-f] [-m] [--detach] <commit>
12       git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
13       git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
14       git checkout [<tree-ish>] [--] <pathspec>...
15       git checkout (-p|--patch) [<tree-ish>] [--] [<paths>...]
16
17

DESCRIPTION

19       Updates files in the working tree to match the version in the index or
20       the specified tree. If no paths are given, git checkout will also
21       update HEAD to set the specified branch as the current branch.
22
23       git checkout [<branch>]
24           To prepare for working on <branch>, switch to it by updating the
25           index and the files in the working tree, and by pointing HEAD at
26           the branch. Local modifications to the files in the working tree
27           are kept, so that they can be committed to the <branch>.
28
29           If <branch> is not found but there does exist a tracking branch in
30           exactly one remote (call it <remote>) with a matching name and
31           --no-guess is not specified, treat as equivalent to
32
33               $ git checkout -b <branch> --track <remote>/<branch>
34
35           You could omit <branch>, in which case the command degenerates to
36           "check out the current branch", which is a glorified no-op with
37           rather expensive side-effects to show only the tracking
38           information, if exists, for the current branch.
39
40       git checkout -b|-B <new_branch> [<start point>]
41           Specifying -b causes a new branch to be created as if git-branch(1)
42           were called and then checked out. In this case you can use the
43           --track or --no-track options, which will be passed to git branch.
44           As a convenience, --track without -b implies branch creation; see
45           the description of --track below.
46
47           If -B is given, <new_branch> is created if it doesn’t exist;
48           otherwise, it is reset. This is the transactional equivalent of
49
50               $ git branch -f <branch> [<start point>]
51               $ git checkout <branch>
52
53           that is to say, the branch is not reset/created unless "git
54           checkout" is successful.
55
56       git checkout --detach [<branch>], git checkout [--detach] <commit>
57           Prepare to work on top of <commit>, by detaching HEAD at it (see
58           "DETACHED HEAD" section), and updating the index and the files in
59           the working tree. Local modifications to the files in the working
60           tree are kept, so that the resulting working tree will be the state
61           recorded in the commit plus the local modifications.
62
63           When the <commit> argument is a branch name, the --detach option
64           can be used to detach HEAD at the tip of the branch (git checkout
65           <branch> would check out that branch without detaching HEAD).
66
67           Omitting <branch> detaches HEAD at the tip of the current branch.
68
69       git checkout [<tree-ish>] [--] <pathspec>...
70           Overwrite paths in the working tree by replacing with the contents
71           in the index or in the <tree-ish> (most often a commit). When a
72           <tree-ish> is given, the paths that match the <pathspec> are
73           updated both in the index and in the working tree.
74
75           The index may contain unmerged entries because of a previous failed
76           merge. By default, if you try to check out such an entry from the
77           index, the checkout operation will fail and nothing will be checked
78           out. Using -f will ignore these unmerged entries. The contents from
79           a specific side of the merge can be checked out of the index by
80           using --ours or --theirs. With -m, changes made to the working tree
81           file can be discarded to re-create the original conflicted merge
82           result.
83
84       git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
85           This is similar to the "check out paths to the working tree from
86           either the index or from a tree-ish" mode described above, but lets
87           you use the interactive interface to show the "diff" output and
88           choose which hunks to use in the result. See below for the
89           description of --patch option.
90

OPTIONS

92       -q, --quiet
93           Quiet, suppress feedback messages.
94
95       --progress, --no-progress
96           Progress status is reported on the standard error stream by default
97           when it is attached to a terminal, unless --quiet is specified.
98           This flag enables progress reporting even if not attached to a
99           terminal, regardless of --quiet.
100
101       -f, --force
102           When switching branches, proceed even if the index or the working
103           tree differs from HEAD. This is used to throw away local changes.
104
105           When checking out paths from the index, do not fail upon unmerged
106           entries; instead, unmerged entries are ignored.
107
108       --ours, --theirs
109           When checking out paths from the index, check out stage #2 (ours)
110           or #3 (theirs) for unmerged paths.
111
112           Note that during git rebase and git pull --rebase, ours and theirs
113           may appear swapped; --ours gives the version from the branch the
114           changes are rebased onto, while --theirs gives the version from the
115           branch that holds your work that is being rebased.
116
117           This is because rebase is used in a workflow that treats the
118           history at the remote as the shared canonical one, and treats the
119           work done on the branch you are rebasing as the third-party work to
120           be integrated, and you are temporarily assuming the role of the
121           keeper of the canonical history during the rebase. As the keeper of
122           the canonical history, you need to view the history from the remote
123           as ours (i.e. "our shared canonical history"), while what you did
124           on your side branch as theirs (i.e. "one contributor’s work on top
125           of it").
126
127       -b <new_branch>
128           Create a new branch named <new_branch> and start it at
129           <start_point>; see git-branch(1) for details.
130
131       -B <new_branch>
132           Creates the branch <new_branch> and start it at <start_point>; if
133           it already exists, then reset it to <start_point>. This is
134           equivalent to running "git branch" with "-f"; see git-branch(1) for
135           details.
136
137       -t, --track
138           When creating a new branch, set up "upstream" configuration. See
139           "--track" in git-branch(1) for details.
140
141           If no -b option is given, the name of the new branch will be
142           derived from the remote-tracking branch, by looking at the local
143           part of the refspec configured for the corresponding remote, and
144           then stripping the initial part up to the "*". This would tell us
145           to use hack as the local branch when branching off of origin/hack
146           (or remotes/origin/hack, or even refs/remotes/origin/hack). If the
147           given name has no slash, or the above guessing results in an empty
148           name, the guessing is aborted. You can explicitly give a name with
149           -b in such a case.
150
151       --no-track
152           Do not set up "upstream" configuration, even if the
153           branch.autoSetupMerge configuration variable is true.
154
155       --guess, --no-guess
156           If <branch> is not found but there does exist a tracking branch in
157           exactly one remote (call it <remote>) with a matching name, treat
158           as equivalent to
159
160               $ git checkout -b <branch> --track <remote>/<branch>
161
162           If the branch exists in multiple remotes and one of them is named
163           by the checkout.defaultRemote configuration variable, we’ll use
164           that one for the purposes of disambiguation, even if the <branch>
165           isn’t unique across all remotes. Set it to e.g.
166           checkout.defaultRemote=origin to always checkout remote branches
167           from there if <branch> is ambiguous but exists on the origin
168           remote. See also checkout.defaultRemote in git-config(1).
169
170           Use --no-guess to disable this.
171
172       -l
173           Create the new branch’s reflog; see git-branch(1) for details.
174
175       --detach
176           Rather than checking out a branch to work on it, check out a commit
177           for inspection and discardable experiments. This is the default
178           behavior of git checkout <commit> when <commit> is not a branch
179           name. See the "DETACHED HEAD" section below for details.
180
181       --orphan <new_branch>
182           Create a new orphan branch, named <new_branch>, started from
183           <start_point> and switch to it. The first commit made on this new
184           branch will have no parents and it will be the root of a new
185           history totally disconnected from all the other branches and
186           commits.
187
188           The index and the working tree are adjusted as if you had
189           previously run git checkout <start_point>. This allows you to start
190           a new history that records a set of paths similar to <start_point>
191           by easily running git commit -a to make the root commit.
192
193           This can be useful when you want to publish the tree from a commit
194           without exposing its full history. You might want to do this to
195           publish an open source branch of a project whose current tree is
196           "clean", but whose full history contains proprietary or otherwise
197           encumbered bits of code.
198
199           If you want to start a disconnected history that records a set of
200           paths that is totally different from the one of <start_point>, then
201           you should clear the index and the working tree right after
202           creating the orphan branch by running git rm -rf .  from the top
203           level of the working tree. Afterwards you will be ready to prepare
204           your new files, repopulating the working tree, by copying them from
205           elsewhere, extracting a tarball, etc.
206
207       --ignore-skip-worktree-bits
208           In sparse checkout mode, git checkout -- <paths> would update only
209           entries matched by <paths> and sparse patterns in
210           $GIT_DIR/info/sparse-checkout. This option ignores the sparse
211           patterns and adds back any files in <paths>.
212
213       -m, --merge
214           When switching branches, if you have local modifications to one or
215           more files that are different between the current branch and the
216           branch to which you are switching, the command refuses to switch
217           branches in order to preserve your modifications in context.
218           However, with this option, a three-way merge between the current
219           branch, your working tree contents, and the new branch is done, and
220           you will be on the new branch.
221
222           When a merge conflict happens, the index entries for conflicting
223           paths are left unmerged, and you need to resolve the conflicts and
224           mark the resolved paths with git add (or git rm if the merge should
225           result in deletion of the path).
226
227           When checking out paths from the index, this option lets you
228           recreate the conflicted merge in the specified paths.
229
230           When switching branches with --merge, staged changes may be lost.
231
232       --conflict=<style>
233           The same as --merge option above, but changes the way the
234           conflicting hunks are presented, overriding the merge.conflictStyle
235           configuration variable. Possible values are "merge" (default) and
236           "diff3" (in addition to what is shown by "merge" style, shows the
237           original contents).
238
239       -p, --patch
240           Interactively select hunks in the difference between the <tree-ish>
241           (or the index, if unspecified) and the working tree. The chosen
242           hunks are then applied in reverse to the working tree (and if a
243           <tree-ish> was specified, the index).
244
245           This means that you can use git checkout -p to selectively discard
246           edits from your current working tree. See the “Interactive Mode”
247           section of git-add(1) to learn how to operate the --patch mode.
248
249           Note that this option uses the no overlay mode by default (see also
250           --overlay), and currently doesn’t support overlay mode.
251
252       --ignore-other-worktrees
253           git checkout refuses when the wanted ref is already checked out by
254           another worktree. This option makes it check the ref out anyway. In
255           other words, the ref can be held by more than one worktree.
256
257       --overwrite-ignore, --no-overwrite-ignore
258           Silently overwrite ignored files when switching branches. This is
259           the default behavior. Use --no-overwrite-ignore to abort the
260           operation when the new branch contains ignored files.
261
262       --recurse-submodules, --no-recurse-submodules
263           Using --recurse-submodules will update the content of all
264           initialized submodules according to the commit recorded in the
265           superproject. If local modifications in a submodule would be
266           overwritten the checkout will fail unless -f is used. If nothing
267           (or --no-recurse-submodules) is used, the work trees of submodules
268           will not be updated. Just like git-submodule(1), this will detach
269           HEAD of the submodule.
270
271       --overlay, --no-overlay
272           In the default overlay mode, git checkout never removes files from
273           the index or the working tree. When specifying --no-overlay, files
274           that appear in the index and working tree, but not in <tree-ish>
275           are removed, to make them match <tree-ish> exactly.
276
277       <branch>
278           Branch to checkout; if it refers to a branch (i.e., a name that,
279           when prepended with "refs/heads/", is a valid ref), then that
280           branch is checked out. Otherwise, if it refers to a valid commit,
281           your HEAD becomes "detached" and you are no longer on any branch
282           (see below for details).
283
284           You can use the @{-N} syntax to refer to the N-th last
285           branch/commit checked out using "git checkout" operation. You may
286           also specify - which is synonymous to @{-1}.
287
288           As a special case, you may use A...B as a shortcut for the merge
289           base of A and B if there is exactly one merge base. You can leave
290           out at most one of A and B, in which case it defaults to HEAD.
291
292       <new_branch>
293           Name for the new branch.
294
295       <start_point>
296           The name of a commit at which to start the new branch; see git-
297           branch(1) for details. Defaults to HEAD.
298
299           As a special case, you may use "A...B" as a shortcut for the merge
300           base of A and B if there is exactly one merge base. You can leave
301           out at most one of A and B, in which case it defaults to HEAD.
302
303       <tree-ish>
304           Tree to checkout from (when paths are given). If not specified, the
305           index will be used.
306

DETACHED HEAD

308       HEAD normally refers to a named branch (e.g. master). Meanwhile, each
309       branch refers to a specific commit. Let’s look at a repo with three
310       commits, one of them tagged, and with branch master checked out:
311
312                      HEAD (refers to branch 'master')
313                       |
314                       v
315           a---b---c  branch 'master' (refers to commit 'c')
316               ^
317               |
318             tag 'v2.0' (refers to commit 'b')
319
320
321       When a commit is created in this state, the branch is updated to refer
322       to the new commit. Specifically, git commit creates a new commit d,
323       whose parent is commit c, and then updates branch master to refer to
324       new commit d. HEAD still refers to branch master and so indirectly now
325       refers to commit d:
326
327           $ edit; git add; git commit
328
329                          HEAD (refers to branch 'master')
330                           |
331                           v
332           a---b---c---d  branch 'master' (refers to commit 'd')
333               ^
334               |
335             tag 'v2.0' (refers to commit 'b')
336
337
338       It is sometimes useful to be able to checkout a commit that is not at
339       the tip of any named branch, or even to create a new commit that is not
340       referenced by a named branch. Let’s look at what happens when we
341       checkout commit b (here we show two ways this may be done):
342
343           $ git checkout v2.0  # or
344           $ git checkout master^^
345
346              HEAD (refers to commit 'b')
347               |
348               v
349           a---b---c---d  branch 'master' (refers to commit 'd')
350               ^
351               |
352             tag 'v2.0' (refers to commit 'b')
353
354
355       Notice that regardless of which checkout command we use, HEAD now
356       refers directly to commit b. This is known as being in detached HEAD
357       state. It means simply that HEAD refers to a specific commit, as
358       opposed to referring to a named branch. Let’s see what happens when we
359       create a commit:
360
361           $ edit; git add; git commit
362
363                HEAD (refers to commit 'e')
364                 |
365                 v
366                 e
367                /
368           a---b---c---d  branch 'master' (refers to commit 'd')
369               ^
370               |
371             tag 'v2.0' (refers to commit 'b')
372
373
374       There is now a new commit e, but it is referenced only by HEAD. We can
375       of course add yet another commit in this state:
376
377           $ edit; git add; git commit
378
379                    HEAD (refers to commit 'f')
380                     |
381                     v
382                 e---f
383                /
384           a---b---c---d  branch 'master' (refers to commit 'd')
385               ^
386               |
387             tag 'v2.0' (refers to commit 'b')
388
389
390       In fact, we can perform all the normal Git operations. But, let’s look
391       at what happens when we then checkout master:
392
393           $ git checkout master
394
395                          HEAD (refers to branch 'master')
396                 e---f     |
397                /          v
398           a---b---c---d  branch 'master' (refers to commit 'd')
399               ^
400               |
401             tag 'v2.0' (refers to commit 'b')
402
403
404       It is important to realize that at this point nothing refers to commit
405       f. Eventually commit f (and by extension commit e) will be deleted by
406       the routine Git garbage collection process, unless we create a
407       reference before that happens. If we have not yet moved away from
408       commit f, any of these will create a reference to it:
409
410           $ git checkout -b foo   (1)
411           $ git branch foo        (2)
412           $ git tag foo           (3)
413
414
415       1. creates a new branch foo, which refers to commit f, and then updates
416       HEAD to refer to branch foo. In other words, we’ll no longer be in
417       detached HEAD state after this command.
418       2. similarly creates a new branch foo, which refers to commit f, but
419       leaves HEAD detached.
420       3. creates a new tag foo, which refers to commit f, leaving HEAD
421       detached.
422
423       If we have moved away from commit f, then we must first recover its
424       object name (typically by using git reflog), and then we can create a
425       reference to it. For example, to see the last two commits to which HEAD
426       referred, we can use either of these commands:
427
428           $ git reflog -2 HEAD # or
429           $ git log -g -2 HEAD
430
431

ARGUMENT DISAMBIGUATION

433       When there is only one argument given and it is not -- (e.g. git
434       checkout abc), and when the argument is both a valid <tree-ish> (e.g. a
435       branch abc exists) and a valid <pathspec> (e.g. a file or a directory
436       whose name is "abc" exists), Git would usually ask you to disambiguate.
437       Because checking out a branch is so common an operation, however, git
438       checkout abc takes "abc" as a <tree-ish> in such a situation. Use git
439       checkout -- <pathspec> if you want to checkout these paths out of the
440       index.
441

EXAMPLES

443        1. The following sequence checks out the master branch, reverts the
444           Makefile to two revisions back, deletes hello.c by mistake, and
445           gets it back from the index.
446
447               $ git checkout master             (1)
448               $ git checkout master~2 Makefile  (2)
449               $ rm -f hello.c
450               $ git checkout hello.c            (3)
451
452           1. switch branch
453           2. take a file out of another commit
454           3. restore hello.c from the index
455
456           If you want to check out all C source files out of the index, you
457           can say
458
459               $ git checkout -- '*.c'
460
461           Note the quotes around *.c. The file hello.c will also be checked
462           out, even though it is no longer in the working tree, because the
463           file globbing is used to match entries in the index (not in the
464           working tree by the shell).
465
466           If you have an unfortunate branch that is named hello.c, this step
467           would be confused as an instruction to switch to that branch. You
468           should instead write:
469
470               $ git checkout -- hello.c
471
472
473        2. After working in the wrong branch, switching to the correct branch
474           would be done using:
475
476               $ git checkout mytopic
477
478           However, your "wrong" branch and correct mytopic branch may differ
479           in files that you have modified locally, in which case the above
480           checkout would fail like this:
481
482               $ git checkout mytopic
483               error: You have local changes to 'frotz'; not switching branches.
484
485           You can give the -m flag to the command, which would try a
486           three-way merge:
487
488               $ git checkout -m mytopic
489               Auto-merging frotz
490
491           After this three-way merge, the local modifications are not
492           registered in your index file, so git diff would show you what
493           changes you made since the tip of the new branch.
494
495        3. When a merge conflict happens during switching branches with the -m
496           option, you would see something like this:
497
498               $ git checkout -m mytopic
499               Auto-merging frotz
500               ERROR: Merge conflict in frotz
501               fatal: merge program failed
502
503           At this point, git diff shows the changes cleanly merged as in the
504           previous example, as well as the changes in the conflicted files.
505           Edit and resolve the conflict and mark it resolved with git add as
506           usual:
507
508               $ edit frotz
509               $ git add frotz
510
511

SEE ALSO

513       git-switch(1), git-restore(1)
514

GIT

516       Part of the git(1) suite
517
518
519
520Git 2.24.1                        12/10/2019                   GIT-CHECKOUT(1)
Impressum