1GIT-CHECKOUT(1) Git Manual GIT-CHECKOUT(1)
2
3
4
6 git-checkout - Checkout a branch or paths to the working tree
7
9 git checkout [-q] [-f] [-m] [<branch>]
10 git checkout [-q] [-f] [-m] [-b <new_branch>] [<start_point>]
11 git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
12 git checkout --patch [<tree-ish>] [--] [<paths>...]
13
14
16 When <paths> are not given, this command switches branches by updating
17 the index, working tree, and HEAD to reflect the specified branch.
18
19 If -b is given, a new branch is created and checked out, as if git-
20 branch(1) were called; in this case you can use the --track or
21 --no-track options, which will be passed to git branch. As a
22 convenience, --track without -b implies branch creation; see the
23 description of --track below.
24
25 When <paths> or --patch are given, this command does not switch
26 branches. It updates the named paths in the working tree from the index
27 file, or from a named <tree-ish> (most often a commit). In this case,
28 the -b and --track options are meaningless and giving either of them
29 results in an error. The <tree-ish> argument can be used to specify a
30 specific tree-ish (i.e. commit, tag or tree) to update the index for
31 the given paths before updating the working tree.
32
33 The index may contain unmerged entries after a failed merge. By
34 default, if you try to check out such an entry from the index, the
35 checkout operation will fail and nothing will be checked out. Using -f
36 will ignore these unmerged entries. The contents from a specific side
37 of the merge can be checked out of the index by using --ours or
38 --theirs. With -m, changes made to the working tree file can be
39 discarded to recreate the original conflicted merge result.
40
42 -q, --quiet
43 Quiet, suppress feedback messages.
44
45 -f, --force
46 When switching branches, proceed even if the index or the working
47 tree differs from HEAD. This is used to throw away local changes.
48
49 When checking out paths from the index, do not fail upon unmerged
50 entries; instead, unmerged entries are ignored.
51
52 --ours, --theirs
53 When checking out paths from the index, check out stage #2 (ours)
54 or #3 (theirs) for unmerged paths.
55
56 -b
57 Create a new branch named <new_branch> and start it at
58 <start_point>; see git-branch(1) for details.
59
60 -t, --track
61 When creating a new branch, set up "upstream" configuration. See
62 "--track" in git-branch(1) for details.
63
64 If no -b option is given, the name of the new branch will be
65 derived from the remote branch. If "remotes/" or "refs/remotes/" is
66 prefixed it is stripped away, and then the part up to the next
67 slash (which would be the nickname of the remote) is removed. This
68 would tell us to use "hack" as the local branch when branching off
69 of "origin/hack" (or "remotes/origin/hack", or even
70 "refs/remotes/origin/hack"). If the given name has no slash, or the
71 above guessing results in an empty name, the guessing is aborted.
72 You can explicitly give a name with -b in such a case.
73
74 --no-track
75 Do not set up "upstream" configuration, even if the
76 branch.autosetupmerge configuration variable is true.
77
78 -l
79 Create the new branch’s reflog; see git-branch(1) for details.
80
81 -m, --merge
82 When switching branches, if you have local modifications to one or
83 more files that are different between the current branch and the
84 branch to which you are switching, the command refuses to switch
85 branches in order to preserve your modifications in context.
86 However, with this option, a three-way merge between the current
87 branch, your working tree contents, and the new branch is done, and
88 you will be on the new branch.
89
90 When a merge conflict happens, the index entries for conflicting
91 paths are left unmerged, and you need to resolve the conflicts and
92 mark the resolved paths with git add (or git rm if the merge should
93 result in deletion of the path).
94
95 When checking out paths from the index, this option lets you
96 recreate the conflicted merge in the specified paths.
97
98 --conflict=<style>
99 The same as --merge option above, but changes the way the
100 conflicting hunks are presented, overriding the merge.conflictstyle
101 configuration variable. Possible values are "merge" (default) and
102 "diff3" (in addition to what is shown by "merge" style, shows the
103 original contents).
104
105 -p, --patch
106 Interactively select hunks in the difference between the <tree-ish>
107 (or the index, if unspecified) and the working tree. The chosen
108 hunks are then applied in reverse to the working tree (and if a
109 <tree-ish> was specified, the index).
110
111 This means that you can use git checkout -p to selectively discard
112 edits from your current working tree.
113
114 <branch>
115 Branch to checkout; if it refers to a branch (i.e., a name that,
116 when prepended with "refs/heads/", is a valid ref), then that
117 branch is checked out. Otherwise, if it refers to a valid commit,
118 your HEAD becomes "detached" and you are no longer on any branch
119 (see below for details).
120
121 As a special case, the "@{-N}" syntax for the N-th last branch
122 checks out the branch (instead of detaching). You may also specify
123 - which is synonymous with "@{-1}".
124
125 <new_branch>
126 Name for the new branch.
127
128 <start_point>
129 The name of a commit at which to start the new branch; see git-
130 branch(1) for details. Defaults to HEAD.
131
132 <tree-ish>
133 Tree to checkout from (when paths are given). If not specified, the
134 index will be used.
135
137 It is sometimes useful to be able to checkout a commit that is not at
138 the tip of one of your branches. The most obvious example is to check
139 out the commit at a tagged official release point, like this:
140
141 $ git checkout v2.6.18
142
143
144 Earlier versions of git did not allow this and asked you to create a
145 temporary branch using the -b option, but starting from version 1.5.0,
146 the above command detaches your HEAD from the current branch and
147 directly points at the commit named by the tag (v2.6.18 in the example
148 above).
149
150 You can use all git commands while in this state. You can use git reset
151 --hard $othercommit to further move around, for example. You can make
152 changes and create a new commit on top of a detached HEAD. You can even
153 create a merge by using git merge $othercommit.
154
155 The state you are in while your HEAD is detached is not recorded by any
156 branch (which is natural --- you are not on any branch). What this
157 means is that you can discard your temporary commits and merges by
158 switching back to an existing branch (e.g. git checkout master), and a
159 later git prune or git gc would garbage-collect them. If you did this
160 by mistake, you can ask the reflog for HEAD where you were, e.g.
161
162 $ git log -g -2 HEAD
163
164
166 1. The following sequence checks out the master branch, reverts the
167 Makefile to two revisions back, deletes hello.c by mistake, and
168 gets it back from the index.
169
170 $ git checkout master [1m(1)
171 $ git checkout master~2 Makefile [1m(2)
172 $ rm -f hello.c
173 $ git checkout hello.c [1m(3)
174
175 1. switch branch
176 2. take a file out of another commit
177 3. restore hello.c from the index
178
179 If you have an unfortunate branch that is named hello.c, this step
180 would be confused as an instruction to switch to that branch. You
181 should instead write:
182
183 $ git checkout -- hello.c
184
185
186 2. After working in the wrong branch, switching to the correct branch
187 would be done using:
188
189 $ git checkout mytopic
190
191 However, your "wrong" branch and correct "mytopic" branch may
192 differ in files that you have modified locally, in which case the
193 above checkout would fail like this:
194
195 $ git checkout mytopic
196 fatal: Entry ´frotz´ not uptodate. Cannot merge.
197
198 You can give the -m flag to the command, which would try a
199 three-way merge:
200
201 $ git checkout -m mytopic
202 Auto-merging frotz
203
204 After this three-way merge, the local modifications are not
205 registered in your index file, so git diff would show you what
206 changes you made since the tip of the new branch.
207
208 3. When a merge conflict happens during switching branches with the -m
209 option, you would see something like this:
210
211 $ git checkout -m mytopic
212 Auto-merging frotz
213 ERROR: Merge conflict in frotz
214 fatal: merge program failed
215
216 At this point, git diff shows the changes cleanly merged as in the
217 previous example, as well as the changes in the conflicted files.
218 Edit and resolve the conflict and mark it resolved with git add as
219 usual:
220
221 $ edit frotz
222 $ git add frotz
223
224
226 Written by Linus Torvalds <torvalds@osdl.org[1]>
227
229 Documentation by Junio C Hamano and the git-list
230 <git@vger.kernel.org[2]>.
231
233 Part of the git(1) suite
234
236 1. torvalds@osdl.org
237 mailto:torvalds@osdl.org
238
239 2. git@vger.kernel.org
240 mailto:git@vger.kernel.org
241
242
243
244Git 1.7.1 08/16/2017 GIT-CHECKOUT(1)