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