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