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

OPTIONS

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

DETACHED HEAD

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

ARGUMENT DISAMBIGUATION

409       When there is only one argument given and it is not -- (e.g. "git
410       checkout abc"), and when the argument is both a valid <tree-ish> (e.g.
411       a branch "abc" exists) and a valid <pathspec> (e.g. a file or a
412       directory whose name is "abc" exists), Git would usually ask you to
413       disambiguate. Because checking out a branch is so common an operation,
414       however, "git checkout abc" takes "abc" as a <tree-ish> in such a
415       situation. Use git checkout -- <pathspec> if you want to checkout these
416       paths out of the index.
417

EXAMPLES

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

GIT

489       Part of the git(1) suite
490
491
492
493Git 2.21.0                        02/24/2019                   GIT-CHECKOUT(1)
Impressum