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