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