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