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 --guess is the default behavior. Use --no-guess to disable it.
173
174 The default behavior can be set via the checkout.guess
175 configuration variable.
176
177 -l
178 Create the new branch’s reflog; see git-branch(1) for details.
179
180 -d, --detach
181 Rather than checking out a branch to work on it, check out a commit
182 for inspection and discardable experiments. This is the default
183 behavior of git checkout <commit> when <commit> is not a branch
184 name. See the "DETACHED HEAD" section below for details.
185
186 --orphan <new_branch>
187 Create a new orphan branch, named <new_branch>, started from
188 <start_point> and switch to it. The first commit made on this new
189 branch will have no parents and it will be the root of a new
190 history totally disconnected from all the other branches and
191 commits.
192
193 The index and the working tree are adjusted as if you had
194 previously run git checkout <start_point>. This allows you to start
195 a new history that records a set of paths similar to <start_point>
196 by easily running git commit -a to make the root commit.
197
198 This can be useful when you want to publish the tree from a commit
199 without exposing its full history. You might want to do this to
200 publish an open source branch of a project whose current tree is
201 "clean", but whose full history contains proprietary or otherwise
202 encumbered bits of code.
203
204 If you want to start a disconnected history that records a set of
205 paths that is totally different from the one of <start_point>, then
206 you should clear the index and the working tree right after
207 creating the orphan branch by running git rm -rf . from the top
208 level of the working tree. Afterwards you will be ready to prepare
209 your new files, repopulating the working tree, by copying them from
210 elsewhere, extracting a tarball, etc.
211
212 --ignore-skip-worktree-bits
213 In sparse checkout mode, git checkout -- <paths> would update only
214 entries matched by <paths> and sparse patterns in
215 $GIT_DIR/info/sparse-checkout. This option ignores the sparse
216 patterns and adds back any files in <paths>.
217
218 -m, --merge
219 When switching branches, if you have local modifications to one or
220 more files that are different between the current branch and the
221 branch to which you are switching, the command refuses to switch
222 branches in order to preserve your modifications in context.
223 However, with this option, a three-way merge between the current
224 branch, your working tree contents, and the new branch is done, and
225 you will be on the new branch.
226
227 When a merge conflict happens, the index entries for conflicting
228 paths are left unmerged, and you need to resolve the conflicts and
229 mark the resolved paths with git add (or git rm if the merge should
230 result in deletion of the path).
231
232 When checking out paths from the index, this option lets you
233 recreate the conflicted merge in the specified paths.
234
235 When switching branches with --merge, staged changes may be lost.
236
237 --conflict=<style>
238 The same as --merge option above, but changes the way the
239 conflicting hunks are presented, overriding the merge.conflictStyle
240 configuration variable. Possible values are "merge" (default) and
241 "diff3" (in addition to what is shown by "merge" style, shows the
242 original contents).
243
244 -p, --patch
245 Interactively select hunks in the difference between the <tree-ish>
246 (or the index, if unspecified) and the working tree. The chosen
247 hunks are then applied in reverse to the working tree (and if a
248 <tree-ish> was specified, the index).
249
250 This means that you can use git checkout -p to selectively discard
251 edits from your current working tree. See the “Interactive Mode”
252 section of git-add(1) to learn how to operate the --patch mode.
253
254 Note that this option uses the no overlay mode by default (see also
255 --overlay), and currently doesn’t support overlay mode.
256
257 --ignore-other-worktrees
258 git checkout refuses when the wanted ref is already checked out by
259 another worktree. This option makes it check the ref out anyway. In
260 other words, the ref can be held by more than one worktree.
261
262 --overwrite-ignore, --no-overwrite-ignore
263 Silently overwrite ignored files when switching branches. This is
264 the default behavior. Use --no-overwrite-ignore to abort the
265 operation when the new branch contains ignored files.
266
267 --recurse-submodules, --no-recurse-submodules
268 Using --recurse-submodules will update the content of all active
269 submodules according to the commit recorded in the superproject. If
270 local modifications in a submodule would be overwritten the
271 checkout will fail unless -f is used. If nothing (or
272 --no-recurse-submodules) is used, submodules working trees will not
273 be updated. Just like git-submodule(1), this will detach HEAD of
274 the submodule.
275
276 --overlay, --no-overlay
277 In the default overlay mode, git checkout never removes files from
278 the index or the working tree. When specifying --no-overlay, files
279 that appear in the index and working tree, but not in <tree-ish>
280 are removed, to make them match <tree-ish> exactly.
281
282 --pathspec-from-file=<file>
283 Pathspec is passed in <file> instead of commandline args. If <file>
284 is exactly - then standard input is used. Pathspec elements are
285 separated by LF or CR/LF. Pathspec elements can be quoted as
286 explained for the configuration variable core.quotePath (see git-
287 config(1)). See also --pathspec-file-nul and global
288 --literal-pathspecs.
289
290 --pathspec-file-nul
291 Only meaningful with --pathspec-from-file. Pathspec elements are
292 separated with NUL character and all other characters are taken
293 literally (including newlines and quotes).
294
295 <branch>
296 Branch to checkout; if it refers to a branch (i.e., a name that,
297 when prepended with "refs/heads/", is a valid ref), then that
298 branch is checked out. Otherwise, if it refers to a valid commit,
299 your HEAD becomes "detached" and you are no longer on any branch
300 (see below for details).
301
302 You can use the @{-N} syntax to refer to the N-th last
303 branch/commit checked out using "git checkout" operation. You may
304 also specify - which is synonymous to @{-1}.
305
306 As a special case, you may use A...B as a shortcut for the merge
307 base of A and B if there is exactly one merge base. You can leave
308 out at most one of A and B, in which case it defaults to HEAD.
309
310 <new_branch>
311 Name for the new branch.
312
313 <start_point>
314 The name of a commit at which to start the new branch; see git-
315 branch(1) for details. Defaults to HEAD.
316
317 As a special case, you may use "A...B" as a shortcut for the merge
318 base of A and B if there is exactly one merge base. You can leave
319 out at most one of A and B, in which case it defaults to HEAD.
320
321 <tree-ish>
322 Tree to checkout from (when paths are given). If not specified, the
323 index will be used.
324
325 As a special case, you may use "A...B" as a shortcut for the merge
326 base of A and B if there is exactly one merge base. You can leave
327 out at most one of A and B, in which case it defaults to HEAD.
328
329 --
330 Do not interpret any more arguments as options.
331
332 <pathspec>...
333 Limits the paths affected by the operation.
334
335 For more details, see the pathspec entry in gitglossary(7).
336
338 HEAD normally refers to a named branch (e.g. master). Meanwhile, each
339 branch refers to a specific commit. Let’s look at a repo with three
340 commits, one of them tagged, and with branch master checked out:
341
342 HEAD (refers to branch 'master')
343 |
344 v
345 a---b---c branch 'master' (refers to commit 'c')
346 ^
347 |
348 tag 'v2.0' (refers to commit 'b')
349
350 When a commit is created in this state, the branch is updated to refer
351 to the new commit. Specifically, git commit creates a new commit d,
352 whose parent is commit c, and then updates branch master to refer to
353 new commit d. HEAD still refers to branch master and so indirectly now
354 refers to commit d:
355
356 $ edit; git add; git commit
357
358 HEAD (refers to branch 'master')
359 |
360 v
361 a---b---c---d branch 'master' (refers to commit 'd')
362 ^
363 |
364 tag 'v2.0' (refers to commit 'b')
365
366 It is sometimes useful to be able to checkout a commit that is not at
367 the tip of any named branch, or even to create a new commit that is not
368 referenced by a named branch. Let’s look at what happens when we
369 checkout commit b (here we show two ways this may be done):
370
371 $ git checkout v2.0 # or
372 $ git checkout master^^
373
374 HEAD (refers to commit 'b')
375 |
376 v
377 a---b---c---d branch 'master' (refers to commit 'd')
378 ^
379 |
380 tag 'v2.0' (refers to commit 'b')
381
382 Notice that regardless of which checkout command we use, HEAD now
383 refers directly to commit b. This is known as being in detached HEAD
384 state. It means simply that HEAD refers to a specific commit, as
385 opposed to referring to a named branch. Let’s see what happens when we
386 create a commit:
387
388 $ edit; git add; git commit
389
390 HEAD (refers to commit 'e')
391 |
392 v
393 e
394 /
395 a---b---c---d branch 'master' (refers to commit 'd')
396 ^
397 |
398 tag 'v2.0' (refers to commit 'b')
399
400 There is now a new commit e, but it is referenced only by HEAD. We can
401 of course add yet another commit in this state:
402
403 $ edit; git add; git commit
404
405 HEAD (refers to commit 'f')
406 |
407 v
408 e---f
409 /
410 a---b---c---d branch 'master' (refers to commit 'd')
411 ^
412 |
413 tag 'v2.0' (refers to commit 'b')
414
415 In fact, we can perform all the normal Git operations. But, let’s look
416 at what happens when we then checkout master:
417
418 $ git checkout master
419
420 HEAD (refers to branch 'master')
421 e---f |
422 / v
423 a---b---c---d branch 'master' (refers to commit 'd')
424 ^
425 |
426 tag 'v2.0' (refers to commit 'b')
427
428 It is important to realize that at this point nothing refers to commit
429 f. Eventually commit f (and by extension commit e) will be deleted by
430 the routine Git garbage collection process, unless we create a
431 reference before that happens. If we have not yet moved away from
432 commit f, any of these will create a reference to it:
433
434 $ git checkout -b foo [1m(1)
435 $ git branch foo [1m(2)
436 $ git tag foo [1m(3)
437
438
439 1. creates a new branch foo, which refers to commit f, and
440 then updates HEAD to refer to branch foo. In other words,
441 we’ll no longer be in detached HEAD state after this
442 command.
443 2. similarly creates a new branch foo, which refers to commit
444 f, but leaves HEAD detached.
445 3. creates a new tag foo, which refers to commit f, leaving
446 HEAD detached.
447
448 If we have moved away from commit f, then we must first recover its
449 object name (typically by using git reflog), and then we can create a
450 reference to it. For example, to see the last two commits to which HEAD
451 referred, we can use either of these commands:
452
453 $ git reflog -2 HEAD # or
454 $ git log -g -2 HEAD
455
457 When there is only one argument given and it is not -- (e.g. git
458 checkout abc), and when the argument is both a valid <tree-ish> (e.g. a
459 branch abc exists) and a valid <pathspec> (e.g. a file or a directory
460 whose name is "abc" exists), Git would usually ask you to disambiguate.
461 Because checking out a branch is so common an operation, however, git
462 checkout abc takes "abc" as a <tree-ish> in such a situation. Use git
463 checkout -- <pathspec> if you want to checkout these paths out of the
464 index.
465
467 1. The following sequence checks out the master branch, reverts the
468 Makefile to two revisions back, deletes hello.c by mistake, and
469 gets it back from the index.
470
471 $ git checkout master [1m(1)
472 $ git checkout master~2 Makefile [1m(2)
473 $ rm -f hello.c
474 $ git checkout hello.c [1m(3)
475
476
477 1. switch branch
478 2. take a file out of another commit
479 3. restore hello.c from the index
480 If you want to check out all C source files out of the index, you
481 can say
482
483 $ git checkout -- '*.c'
484
485 Note the quotes around *.c. The file hello.c will also be checked
486 out, even though it is no longer in the working tree, because the
487 file globbing is used to match entries in the index (not in the
488 working tree by the shell).
489
490 If you have an unfortunate branch that is named hello.c, this step
491 would be confused as an instruction to switch to that branch. You
492 should instead write:
493
494 $ git checkout -- hello.c
495
496 2. After working in the wrong branch, switching to the correct branch
497 would be done using:
498
499 $ git checkout mytopic
500
501 However, your "wrong" branch and correct mytopic branch may differ
502 in files that you have modified locally, in which case the above
503 checkout would fail like this:
504
505 $ git checkout mytopic
506 error: You have local changes to 'frotz'; not switching branches.
507
508 You can give the -m flag to the command, which would try a
509 three-way merge:
510
511 $ git checkout -m mytopic
512 Auto-merging frotz
513
514 After this three-way merge, the local modifications are not
515 registered in your index file, so git diff would show you what
516 changes you made since the tip of the new branch.
517
518 3. When a merge conflict happens during switching branches with the -m
519 option, you would see something like this:
520
521 $ git checkout -m mytopic
522 Auto-merging frotz
523 ERROR: Merge conflict in frotz
524 fatal: merge program failed
525
526 At this point, git diff shows the changes cleanly merged as in the
527 previous example, as well as the changes in the conflicted files.
528 Edit and resolve the conflict and mark it resolved with git add as
529 usual:
530
531 $ edit frotz
532 $ git add frotz
533
535 git-switch(1), git-restore(1)
536
538 Part of the git(1) suite
539
540
541
542Git 2.33.1 2021-10-12 GIT-CHECKOUT(1)