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

NAME

6       git-checkout - Checkout a branch or paths to the working tree
7

SYNOPSIS

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

DESCRIPTION

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

OPTIONS

86       -q, --quiet
87           Quiet, suppress feedback messages.
88
89       -f, --force
90           When switching branches, proceed even if the index or the working
91           tree differs from HEAD. This is used to throw away local changes.
92
93           When checking out paths from the index, do not fail upon unmerged
94           entries; instead, unmerged entries are ignored.
95
96       --ours, --theirs
97           When checking out paths from the index, check out stage #2 (ours)
98           or #3 (theirs) for unmerged paths.
99
100       -b <new_branch>
101           Create a new branch named <new_branch> and start it at
102           <start_point>; see git-branch(1) for details.
103
104       -B <new_branch>
105           Creates the branch <new_branch> and start it at <start_point>; if
106           it already exists, then reset it to <start_point>. This is
107           equivalent to running "git branch" with "-f"; see git-branch(1) for
108           details.
109
110       -t, --track
111           When creating a new branch, set up "upstream" configuration. See
112           "--track" in git-branch(1) for details.
113
114           If no -b option is given, the name of the new branch will be
115           derived from the remote-tracking branch. If "remotes/" or
116           "refs/remotes/" is prefixed it is stripped away, and then the part
117           up to the next slash (which would be the nickname of the remote) is
118           removed. This would tell us to use "hack" as the local branch when
119           branching off of "origin/hack" (or "remotes/origin/hack", or even
120           "refs/remotes/origin/hack"). If the given name has no slash, or the
121           above guessing results in an empty name, the guessing is aborted.
122           You can explicitly give a name with -b in such a case.
123
124       --no-track
125           Do not set up "upstream" configuration, even if the
126           branch.autosetupmerge configuration variable is true.
127
128       -l
129           Create the new branch’s reflog; see git-branch(1) for details.
130
131       --detach
132           Rather than checking out a branch to work on it, check out a commit
133           for inspection and discardable experiments. This is the default
134           behavior of "git checkout <commit>" when <commit> is not a branch
135           name. See the "DETACHED HEAD" section below for details.
136
137       --orphan <new_branch>
138           Create a new orphan branch, named <new_branch>, started from
139           <start_point> and switch to it. The first commit made on this new
140           branch will have no parents and it will be the root of a new
141           history totally disconnected from all the other branches and
142           commits.
143
144           The index and the working tree are adjusted as if you had
145           previously run "git checkout <start_point>". This allows you to
146           start a new history that records a set of paths similar to
147           <start_point> by easily running "git commit -a" to make the root
148           commit.
149
150           This can be useful when you want to publish the tree from a commit
151           without exposing its full history. You might want to do this to
152           publish an open source branch of a project whose current tree is
153           "clean", but whose full history contains proprietary or otherwise
154           encumbered bits of code.
155
156           If you want to start a disconnected history that records a set of
157           paths that is totally different from the one of <start_point>, then
158           you should clear the index and the working tree right after
159           creating the orphan branch by running "git rm -rf ." from the top
160           level of the working tree. Afterwards you will be ready to prepare
161           your new files, repopulating the working tree, by copying them from
162           elsewhere, extracting a tarball, etc.
163
164       --ignore-skip-worktree-bits
165           In sparse checkout mode, git checkout -- <paths> would update only
166           entries matched by <paths> and sparse patterns in
167           $GIT_DIR/info/sparse-checkout. This option ignores the sparse
168           patterns and adds back any files in <paths>.
169
170       -m, --merge
171           When switching branches, if you have local modifications to one or
172           more files that are different between the current branch and the
173           branch to which you are switching, the command refuses to switch
174           branches in order to preserve your modifications in context.
175           However, with this option, a three-way merge between the current
176           branch, your working tree contents, and the new branch is done, and
177           you will be on the new branch.
178
179           When a merge conflict happens, the index entries for conflicting
180           paths are left unmerged, and you need to resolve the conflicts and
181           mark the resolved paths with git add (or git rm if the merge should
182           result in deletion of the path).
183
184           When checking out paths from the index, this option lets you
185           recreate the conflicted merge in the specified paths.
186
187       --conflict=<style>
188           The same as --merge option above, but changes the way the
189           conflicting hunks are presented, overriding the merge.conflictstyle
190           configuration variable. Possible values are "merge" (default) and
191           "diff3" (in addition to what is shown by "merge" style, shows the
192           original contents).
193
194       -p, --patch
195           Interactively select hunks in the difference between the <tree-ish>
196           (or the index, if unspecified) and the working tree. The chosen
197           hunks are then applied in reverse to the working tree (and if a
198           <tree-ish> was specified, the index).
199
200           This means that you can use git checkout -p to selectively discard
201           edits from your current working tree. See the “Interactive Mode”
202           section of git-add(1) to learn how to operate the --patch mode.
203
204       <branch>
205           Branch to checkout; if it refers to a branch (i.e., a name that,
206           when prepended with "refs/heads/", is a valid ref), then that
207           branch is checked out. Otherwise, if it refers to a valid commit,
208           your HEAD becomes "detached" and you are no longer on any branch
209           (see below for details).
210
211           As a special case, the "@{-N}" syntax for the N-th last branch
212           checks out the branch (instead of detaching). You may also specify
213           - which is synonymous with "@{-1}".
214
215           As a further special case, you may use "A...B" as a shortcut for
216           the merge base of A and B if there is exactly one merge base. You
217           can leave out at most one of A and B, in which case it defaults to
218           HEAD.
219
220       <new_branch>
221           Name for the new branch.
222
223       <start_point>
224           The name of a commit at which to start the new branch; see git-
225           branch(1) for details. Defaults to HEAD.
226
227       <tree-ish>
228           Tree to checkout from (when paths are given). If not specified, the
229           index will be used.
230

DETACHED HEAD

232       HEAD normally refers to a named branch (e.g. master). Meanwhile, each
233       branch refers to a specific commit. Let’s look at a repo with three
234       commits, one of them tagged, and with branch master checked out:
235
236                      HEAD (refers to branch 'master')
237                       |
238                       v
239           a---b---c  branch 'master' (refers to commit 'c')
240               ^
241               |
242             tag 'v2.0' (refers to commit 'b')
243
244
245       When a commit is created in this state, the branch is updated to refer
246       to the new commit. Specifically, git commit creates a new commit d,
247       whose parent is commit c, and then updates branch master to refer to
248       new commit d. HEAD still refers to branch master and so indirectly now
249       refers to commit d:
250
251           $ edit; git add; git commit
252
253                          HEAD (refers to branch 'master')
254                           |
255                           v
256           a---b---c---d  branch 'master' (refers to commit 'd')
257               ^
258               |
259             tag 'v2.0' (refers to commit 'b')
260
261
262       It is sometimes useful to be able to checkout a commit that is not at
263       the tip of any named branch, or even to create a new commit that is not
264       referenced by a named branch. Let’s look at what happens when we
265       checkout commit b (here we show two ways this may be done):
266
267           $ git checkout v2.0  # or
268           $ git checkout master^^
269
270              HEAD (refers to commit 'b')
271               |
272               v
273           a---b---c---d  branch 'master' (refers to commit 'd')
274               ^
275               |
276             tag 'v2.0' (refers to commit 'b')
277
278
279       Notice that regardless of which checkout command we use, HEAD now
280       refers directly to commit b. This is known as being in detached HEAD
281       state. It means simply that HEAD refers to a specific commit, as
282       opposed to referring to a named branch. Let’s see what happens when we
283       create a commit:
284
285           $ edit; git add; git commit
286
287                HEAD (refers to commit 'e')
288                 |
289                 v
290                 e
291                /
292           a---b---c---d  branch 'master' (refers to commit 'd')
293               ^
294               |
295             tag 'v2.0' (refers to commit 'b')
296
297
298       There is now a new commit e, but it is referenced only by HEAD. We can
299       of course add yet another commit in this state:
300
301           $ edit; git add; git commit
302
303                    HEAD (refers to commit 'f')
304                     |
305                     v
306                 e---f
307                /
308           a---b---c---d  branch 'master' (refers to commit 'd')
309               ^
310               |
311             tag 'v2.0' (refers to commit 'b')
312
313
314       In fact, we can perform all the normal Git operations. But, let’s look
315       at what happens when we then checkout master:
316
317           $ git checkout master
318
319                          HEAD (refers to branch 'master')
320                 e---f     |
321                /          v
322           a---b---c---d  branch 'master' (refers to commit 'd')
323               ^
324               |
325             tag 'v2.0' (refers to commit 'b')
326
327
328       It is important to realize that at this point nothing refers to commit
329       f. Eventually commit f (and by extension commit e) will be deleted by
330       the routine Git garbage collection process, unless we create a
331       reference before that happens. If we have not yet moved away from
332       commit f, any of these will create a reference to it:
333
334           $ git checkout -b foo   (1)
335           $ git branch foo        (2)
336           $ git tag foo           (3)
337
338
339       1. creates a new branch foo, which refers to commit f, and then updates
340       HEAD to refer to branch foo. In other words, we’ll no longer be in
341       detached HEAD state after this command.
342       2. similarly creates a new branch foo, which refers to commit f, but
343       leaves HEAD detached.
344       3. creates a new tag foo, which refers to commit f, leaving HEAD
345       detached.
346
347       If we have moved away from commit f, then we must first recover its
348       object name (typically by using git reflog), and then we can create a
349       reference to it. For example, to see the last two commits to which HEAD
350       referred, we can use either of these commands:
351
352           $ git reflog -2 HEAD # or
353           $ git log -g -2 HEAD
354
355

EXAMPLES

357        1. The following sequence checks out the master branch, reverts the
358           Makefile to two revisions back, deletes hello.c by mistake, and
359           gets it back from the index.
360
361               $ git checkout master             (1)
362               $ git checkout master~2 Makefile  (2)
363               $ rm -f hello.c
364               $ git checkout hello.c            (3)
365
366           1. switch branch
367           2. take a file out of another commit
368           3. restore hello.c from the index
369
370           If you want to check out all C source files out of the index, you
371           can say
372
373               $ git checkout -- '*.c'
374
375           Note the quotes around *.c. The file hello.c will also be checked
376           out, even though it is no longer in the working tree, because the
377           file globbing is used to match entries in the index (not in the
378           working tree by the shell).
379
380           If you have an unfortunate branch that is named hello.c, this step
381           would be confused as an instruction to switch to that branch. You
382           should instead write:
383
384               $ git checkout -- hello.c
385
386
387        2. After working in the wrong branch, switching to the correct branch
388           would be done using:
389
390               $ git checkout mytopic
391
392           However, your "wrong" branch and correct "mytopic" branch may
393           differ in files that you have modified locally, in which case the
394           above checkout would fail like this:
395
396               $ git checkout mytopic
397               error: You have local changes to 'frotz'; not switching branches.
398
399           You can give the -m flag to the command, which would try a
400           three-way merge:
401
402               $ git checkout -m mytopic
403               Auto-merging frotz
404
405           After this three-way merge, the local modifications are not
406           registered in your index file, so git diff would show you what
407           changes you made since the tip of the new branch.
408
409        3. When a merge conflict happens during switching branches with the -m
410           option, you would see something like this:
411
412               $ git checkout -m mytopic
413               Auto-merging frotz
414               ERROR: Merge conflict in frotz
415               fatal: merge program failed
416
417           At this point, git diff shows the changes cleanly merged as in the
418           previous example, as well as the changes in the conflicted files.
419           Edit and resolve the conflict and mark it resolved with git add as
420           usual:
421
422               $ edit frotz
423               $ git add frotz
424
425

GIT

427       Part of the git(1) suite
428
429
430
431Git 1.8.3.1                       11/19/2018                   GIT-CHECKOUT(1)
Impressum