Merge branch 'em/checkout-orphan'
[git/mingw.git] / Documentation / git-checkout.txt
blob1548312b3581450dab5e446935afba97616124b1
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 -----------
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.
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.
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.
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.
46 OPTIONS
47 -------
48 -q::
49 --quiet::
50         Quiet, suppress feedback messages.
52 -f::
53 --force::
54         When switching branches, proceed even if the index or the
55         working tree differs from HEAD.  This is used to throw away
56         local changes.
58 When checking out paths from the index, do not fail upon unmerged
59 entries; instead, unmerged entries are ignored.
61 --ours::
62 --theirs::
63         When checking out paths from the index, check out stage #2
64         ('ours') or #3 ('theirs') for unmerged paths.
66 -b::
67         Create a new branch named <new_branch> and start it at
68         <start_point>; see linkgit:git-branch[1] for details.
70 -t::
71 --track::
72         When creating a new branch, set up "upstream" configuration. See
73         "--track" in linkgit:git-branch[1] for details.
75 If no '-b' option is given, the name of the new branch will be
76 derived from the remote branch.  If "remotes/" or "refs/remotes/"
77 is prefixed it is stripped away, and then the part up to the
78 next slash (which would be the nickname of the remote) is removed.
79 This would tell us to use "hack" as the local branch when branching
80 off of "origin/hack" (or "remotes/origin/hack", or even
81 "refs/remotes/origin/hack").  If the given name has no slash, or the above
82 guessing results in an empty name, the guessing is aborted.  You can
83 explicitly give a name with '-b' in such a case.
85 --no-track::
86         Do not set up "upstream" configuration, even if the
87         branch.autosetupmerge configuration variable is true.
89 -l::
90         Create the new branch's reflog; see linkgit:git-branch[1] for
91         details.
93 --orphan::
94         Create a new 'orphan' branch, named <new_branch>, started from
95         <start_point> and switch to it.  The first commit made on this
96         new branch will have no parents and it will be the root of a new
97         history totally disconnected from all the other branches and
98         commits.
100 The index and the working tree are adjusted as if you had previously run
101 "git checkout <start_point>".  This allows you to start a new history
102 that records a set of paths similar to <start_point> by easily running
103 "git commit -a" to make the root commit.
105 This can be useful when you want to publish the tree from a commit
106 without exposing its full history. You might want to do this to publish
107 an open source branch of a project whose current tree is "clean", but
108 whose full history contains proprietary or otherwise encumbered bits of
109 code.
111 If you want to start a disconnected history that records a set of paths
112 that is totally different from the one of <start_point>, then you should
113 clear the index and the working tree right after creating the orphan
114 branch by running "git rm -rf ." from the top level of the working tree.
115 Afterwards you will be ready to prepare your new files, repopulating the
116 working tree, by copying them from elsewhere, extracting a tarball, etc.
118 -m::
119 --merge::
120         When switching branches,
121         if you have local modifications to one or more files that
122         are different between the current branch and the branch to
123         which you are switching, the command refuses to switch
124         branches in order to preserve your modifications in context.
125         However, with this option, a three-way merge between the current
126         branch, your working tree contents, and the new branch
127         is done, and you will be on the new branch.
129 When a merge conflict happens, the index entries for conflicting
130 paths are left unmerged, and you need to resolve the conflicts
131 and mark the resolved paths with `git add` (or `git rm` if the merge
132 should result in deletion of the path).
134 When checking out paths from the index, this option lets you recreate
135 the conflicted merge in the specified paths.
137 --conflict=<style>::
138         The same as --merge option above, but changes the way the
139         conflicting hunks are presented, overriding the
140         merge.conflictstyle configuration variable.  Possible values are
141         "merge" (default) and "diff3" (in addition to what is shown by
142         "merge" style, shows the original contents).
144 -p::
145 --patch::
146         Interactively select hunks in the difference between the
147         <tree-ish> (or the index, if unspecified) and the working
148         tree.  The chosen hunks are then applied in reverse to the
149         working tree (and if a <tree-ish> was specified, the index).
151 This means that you can use `git checkout -p` to selectively discard
152 edits from your current working tree.
154 <branch>::
155         Branch to checkout; if it refers to a branch (i.e., a name that,
156         when prepended with "refs/heads/", is a valid ref), then that
157         branch is checked out. Otherwise, if it refers to a valid
158         commit, your HEAD becomes "detached" and you are no longer on
159         any branch (see below for details).
161 As a special case, the `"@\{-N\}"` syntax for the N-th last branch
162 checks out the branch (instead of detaching).  You may also specify
163 `-` which is synonymous with `"@\{-1\}"`.
165 As a further special case, you may use `"A...B"` as a shortcut for the
166 merge base of `A` and `B` if there is exactly one merge base. You can
167 leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
169 <new_branch>::
170         Name for the new branch.
172 <start_point>::
173         The name of a commit at which to start the new branch; see
174         linkgit:git-branch[1] for details. Defaults to HEAD.
176 <tree-ish>::
177         Tree to checkout from (when paths are given). If not specified,
178         the index will be used.
182 Detached HEAD
183 -------------
185 It is sometimes useful to be able to 'checkout' a commit that is
186 not at the tip of one of your branches.  The most obvious
187 example is to check out the commit at a tagged official release
188 point, like this:
190 ------------
191 $ git checkout v2.6.18
192 ------------
194 Earlier versions of git did not allow this and asked you to
195 create a temporary branch using the `-b` option, but starting from
196 version 1.5.0, the above command 'detaches' your HEAD from the
197 current branch and directly points at the commit named by the tag
198 (`v2.6.18` in the example above).
200 You can use all git commands while in this state.  You can use
201 `git reset --hard $othercommit` to further move around, for
202 example.  You can make changes and create a new commit on top of
203 a detached HEAD.  You can even create a merge by using `git
204 merge $othercommit`.
206 The state you are in while your HEAD is detached is not recorded
207 by any branch (which is natural --- you are not on any branch).
208 What this means is that you can discard your temporary commits
209 and merges by switching back to an existing branch (e.g. `git
210 checkout master`), and a later `git prune` or `git gc` would
211 garbage-collect them.  If you did this by mistake, you can ask
212 the reflog for HEAD where you were, e.g.
214 ------------
215 $ git log -g -2 HEAD
216 ------------
219 EXAMPLES
220 --------
222 . The following sequence checks out the `master` branch, reverts
223 the `Makefile` to two revisions back, deletes hello.c by
224 mistake, and gets it back from the index.
226 ------------
227 $ git checkout master             <1>
228 $ git checkout master~2 Makefile  <2>
229 $ rm -f hello.c
230 $ git checkout hello.c            <3>
231 ------------
233 <1> switch branch
234 <2> take a file out of another commit
235 <3> restore hello.c from the index
237 If you have an unfortunate branch that is named `hello.c`, this
238 step would be confused as an instruction to switch to that branch.
239 You should instead write:
241 ------------
242 $ git checkout -- hello.c
243 ------------
245 . After working in the wrong branch, switching to the correct
246 branch would be done using:
248 ------------
249 $ git checkout mytopic
250 ------------
252 However, your "wrong" branch and correct "mytopic" branch may
253 differ in files that you have modified locally, in which case
254 the above checkout would fail like this:
256 ------------
257 $ git checkout mytopic
258 fatal: Entry 'frotz' not uptodate. Cannot merge.
259 ------------
261 You can give the `-m` flag to the command, which would try a
262 three-way merge:
264 ------------
265 $ git checkout -m mytopic
266 Auto-merging frotz
267 ------------
269 After this three-way merge, the local modifications are _not_
270 registered in your index file, so `git diff` would show you what
271 changes you made since the tip of the new branch.
273 . When a merge conflict happens during switching branches with
274 the `-m` option, you would see something like this:
276 ------------
277 $ git checkout -m mytopic
278 Auto-merging frotz
279 ERROR: Merge conflict in frotz
280 fatal: merge program failed
281 ------------
283 At this point, `git diff` shows the changes cleanly merged as in
284 the previous example, as well as the changes in the conflicted
285 files.  Edit and resolve the conflict and mark it resolved with
286 `git add` as usual:
288 ------------
289 $ edit frotz
290 $ git add frotz
291 ------------
294 Author
295 ------
296 Written by Linus Torvalds <torvalds@osdl.org>
298 Documentation
299 --------------
300 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
304 Part of the linkgit:git[1] suite