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 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
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 <branch>
254 Branch to checkout; if it refers to a branch (i.e., a name that,
255 when prepended with "refs/heads/", is a valid ref), then that
256 branch is checked out. Otherwise, if it refers to a valid commit,
257 your HEAD becomes "detached" and you are no longer on any branch
258 (see below for details).
259
260 You can use the "@{-N}" syntax to refer to the N-th last
261 branch/commit checked out using "git checkout" operation. You may
262 also specify - which is synonymous to "@{-1}.
263
264 As a special case, you may use "A...B" as a shortcut for the merge
265 base of A and B if there is exactly one merge base. You can leave
266 out at most one of A and B, in which case it defaults to HEAD.
267
268 <new_branch>
269 Name for the new branch.
270
271 <start_point>
272 The name of a commit at which to start the new branch; see git-
273 branch(1) for details. Defaults to HEAD.
274
275 <tree-ish>
276 Tree to checkout from (when paths are given). If not specified, the
277 index will be used.
278
280 HEAD normally refers to a named branch (e.g. master). Meanwhile, each
281 branch refers to a specific commit. Let’s look at a repo with three
282 commits, one of them tagged, and with branch master checked out:
283
284 HEAD (refers to branch 'master')
285 |
286 v
287 a---b---c branch 'master' (refers to commit 'c')
288 ^
289 |
290 tag 'v2.0' (refers to commit 'b')
291
292
293 When a commit is created in this state, the branch is updated to refer
294 to the new commit. Specifically, git commit creates a new commit d,
295 whose parent is commit c, and then updates branch master to refer to
296 new commit d. HEAD still refers to branch master and so indirectly now
297 refers to commit d:
298
299 $ edit; git add; git commit
300
301 HEAD (refers to branch 'master')
302 |
303 v
304 a---b---c---d branch 'master' (refers to commit 'd')
305 ^
306 |
307 tag 'v2.0' (refers to commit 'b')
308
309
310 It is sometimes useful to be able to checkout a commit that is not at
311 the tip of any named branch, or even to create a new commit that is not
312 referenced by a named branch. Let’s look at what happens when we
313 checkout commit b (here we show two ways this may be done):
314
315 $ git checkout v2.0 # or
316 $ git checkout master^^
317
318 HEAD (refers to commit 'b')
319 |
320 v
321 a---b---c---d branch 'master' (refers to commit 'd')
322 ^
323 |
324 tag 'v2.0' (refers to commit 'b')
325
326
327 Notice that regardless of which checkout command we use, HEAD now
328 refers directly to commit b. This is known as being in detached HEAD
329 state. It means simply that HEAD refers to a specific commit, as
330 opposed to referring to a named branch. Let’s see what happens when we
331 create a commit:
332
333 $ edit; git add; git commit
334
335 HEAD (refers to commit 'e')
336 |
337 v
338 e
339 /
340 a---b---c---d branch 'master' (refers to commit 'd')
341 ^
342 |
343 tag 'v2.0' (refers to commit 'b')
344
345
346 There is now a new commit e, but it is referenced only by HEAD. We can
347 of course add yet another commit in this state:
348
349 $ edit; git add; git commit
350
351 HEAD (refers to commit 'f')
352 |
353 v
354 e---f
355 /
356 a---b---c---d branch 'master' (refers to commit 'd')
357 ^
358 |
359 tag 'v2.0' (refers to commit 'b')
360
361
362 In fact, we can perform all the normal Git operations. But, let’s look
363 at what happens when we then checkout master:
364
365 $ git checkout master
366
367 HEAD (refers to branch 'master')
368 e---f |
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
376 It is important to realize that at this point nothing refers to commit
377 f. Eventually commit f (and by extension commit e) will be deleted by
378 the routine Git garbage collection process, unless we create a
379 reference before that happens. If we have not yet moved away from
380 commit f, any of these will create a reference to it:
381
382 $ git checkout -b foo [1m(1)
383 $ git branch foo [1m(2)
384 $ git tag foo [1m(3)
385
386
387 1. creates a new branch foo, which refers to commit f, and then updates
388 HEAD to refer to branch foo. In other words, we’ll no longer be in
389 detached HEAD state after this command.
390 2. similarly creates a new branch foo, which refers to commit f, but
391 leaves HEAD detached.
392 3. creates a new tag foo, which refers to commit f, leaving HEAD
393 detached.
394
395 If we have moved away from commit f, then we must first recover its
396 object name (typically by using git reflog), and then we can create a
397 reference to it. For example, to see the last two commits to which HEAD
398 referred, we can use either of these commands:
399
400 $ git reflog -2 HEAD # or
401 $ git log -g -2 HEAD
402
403
405 When there is only one argument given and it is not -- (e.g. "git
406 checkout abc"), and when the argument is both a valid <tree-ish> (e.g.
407 a branch "abc" exists) and a valid <pathspec> (e.g. a file or a
408 directory whose name is "abc" exists), Git would usually ask you to
409 disambiguate. Because checking out a branch is so common an operation,
410 however, "git checkout abc" takes "abc" as a <tree-ish> in such a
411 situation. Use git checkout -- <pathspec> if you want to checkout these
412 paths out of the index.
413
415 1. The following sequence checks out the master branch, reverts the
416 Makefile to two revisions back, deletes hello.c by mistake, and
417 gets it back from the index.
418
419 $ git checkout master [1m(1)
420 $ git checkout master~2 Makefile [1m(2)
421 $ rm -f hello.c
422 $ git checkout hello.c [1m(3)
423
424 1. switch branch
425 2. take a file out of another commit
426 3. restore hello.c from the index
427
428 If you want to check out all C source files out of the index, you
429 can say
430
431 $ git checkout -- '*.c'
432
433 Note the quotes around *.c. The file hello.c will also be checked
434 out, even though it is no longer in the working tree, because the
435 file globbing is used to match entries in the index (not in the
436 working tree by the shell).
437
438 If you have an unfortunate branch that is named hello.c, this step
439 would be confused as an instruction to switch to that branch. You
440 should instead write:
441
442 $ git checkout -- hello.c
443
444
445 2. After working in the wrong branch, switching to the correct branch
446 would be done using:
447
448 $ git checkout mytopic
449
450 However, your "wrong" branch and correct "mytopic" branch may
451 differ in files that you have modified locally, in which case the
452 above checkout would fail like this:
453
454 $ git checkout mytopic
455 error: You have local changes to 'frotz'; not switching branches.
456
457 You can give the -m flag to the command, which would try a
458 three-way merge:
459
460 $ git checkout -m mytopic
461 Auto-merging frotz
462
463 After this three-way merge, the local modifications are not
464 registered in your index file, so git diff would show you what
465 changes you made since the tip of the new branch.
466
467 3. When a merge conflict happens during switching branches with the -m
468 option, you would see something like this:
469
470 $ git checkout -m mytopic
471 Auto-merging frotz
472 ERROR: Merge conflict in frotz
473 fatal: merge program failed
474
475 At this point, git diff shows the changes cleanly merged as in the
476 previous example, as well as the changes in the conflicted files.
477 Edit and resolve the conflict and mark it resolved with git add as
478 usual:
479
480 $ edit frotz
481 $ git add frotz
482
483
485 Part of the git(1) suite
486
487
488
489Git 2.20.1 12/15/2018 GIT-CHECKOUT(1)