doc/checkout: refer to git-branch(1) as appropriate
[git/spearce.git] / Documentation / git-checkout.txt
blob22ad10d9522c221a87c7177db7196ad95c6f9430
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] [-b <new_branch>] [<branch>]
12 'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
14 DESCRIPTION
15 -----------
17 When <paths> are not given, this command switches branches by
18 updating the index and working tree to reflect the specified
19 branch, <branch>, and updating HEAD to be <branch> or, if
20 specified, <new_branch>.  Using -b will cause <new_branch> to
21 be created as if linkgit:git-branch[1] were called; in this case you can
22 use the --track or --no-track options, which will be passed to `git
23 branch`.
25 As a convenience, --track will default to creating a branch whose
26 name is constructed from the specified branch name by stripping
27 the first namespace level.
29 When <paths> 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, suppress feedback messages.
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.
56 When checking out paths from the index, do not fail upon unmerged
57 entries; instead, unmerged entries are ignored.
59 --ours::
60 --theirs::
61         When checking out paths from the index, check out stage #2
62         ('ours') or #3 ('theirs') for unmerged paths.
64 -b::
65         Create a new branch named <new_branch> and start it at
66         <branch>; see linkgit:git-branch[1] for details.
68 -t::
69 --track::
70         When creating a new branch, set up "upstream" configuration. See
71         "--track" in linkgit:git-branch[1] for details.
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.
83 --no-track::
84         Do not set up "upstream" configuration, even if the
85         branch.autosetupmerge configuration variable is true.
87 -l::
88         Create the new branch's reflog; see linkgit:git-branch[1] for
89         details.
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.
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).
107 When checking out paths from the index, this option lets you recreate
108 the conflicted merge in the specified paths.
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).
117 <new_branch>::
118         Name for the new branch.
120 <tree-ish>::
121         Tree to checkout from (when paths are given). If not specified,
122         the index will be used.
124 <branch>::
125         Branch to checkout (when no paths are given); may be any object
126         ID that resolves to a commit.  Defaults to HEAD.
128 When this parameter names a non-branch (but still a valid commit object),
129 your HEAD becomes 'detached'.
131 As a special case, the `"@\{-N\}"` syntax for the N-th last branch
132 checks out the branch (instead of detaching).  You may also specify
133 `-` which is synonymous with `"@\{-1\}"`.
136 Detached HEAD
137 -------------
139 It is sometimes useful to be able to 'checkout' a commit that is
140 not at the tip of one of your branches.  The most obvious
141 example is to check out the commit at a tagged official release
142 point, like this:
144 ------------
145 $ git checkout v2.6.18
146 ------------
148 Earlier versions of git did not allow this and asked you to
149 create a temporary branch using the `-b` option, but starting from
150 version 1.5.0, the above command 'detaches' your HEAD from the
151 current branch and directly points at the commit named by the tag
152 (`v2.6.18` in the example above).
154 You can use all git commands while in this state.  You can use
155 `git reset --hard $othercommit` to further move around, for
156 example.  You can make changes and create a new commit on top of
157 a detached HEAD.  You can even create a merge by using `git
158 merge $othercommit`.
160 The state you are in while your HEAD is detached is not recorded
161 by any branch (which is natural --- you are not on any branch).
162 What this means is that you can discard your temporary commits
163 and merges by switching back to an existing branch (e.g. `git
164 checkout master`), and a later `git prune` or `git gc` would
165 garbage-collect them.  If you did this by mistake, you can ask
166 the reflog for HEAD where you were, e.g.
168 ------------
169 $ git log -g -2 HEAD
170 ------------
173 EXAMPLES
174 --------
176 . The following sequence checks out the `master` branch, reverts
177 the `Makefile` to two revisions back, deletes hello.c by
178 mistake, and gets it back from the index.
180 ------------
181 $ git checkout master             <1>
182 $ git checkout master~2 Makefile  <2>
183 $ rm -f hello.c
184 $ git checkout hello.c            <3>
185 ------------
187 <1> switch branch
188 <2> take a file out of another commit
189 <3> restore hello.c from the index
191 If you have an unfortunate branch that is named `hello.c`, this
192 step would be confused as an instruction to switch to that branch.
193 You should instead write:
195 ------------
196 $ git checkout -- hello.c
197 ------------
199 . After working in the wrong branch, switching to the correct
200 branch would be done using:
202 ------------
203 $ git checkout mytopic
204 ------------
206 However, your "wrong" branch and correct "mytopic" branch may
207 differ in files that you have modified locally, in which case
208 the above checkout would fail like this:
210 ------------
211 $ git checkout mytopic
212 fatal: Entry 'frotz' not uptodate. Cannot merge.
213 ------------
215 You can give the `-m` flag to the command, which would try a
216 three-way merge:
218 ------------
219 $ git checkout -m mytopic
220 Auto-merging frotz
221 ------------
223 After this three-way merge, the local modifications are _not_
224 registered in your index file, so `git diff` would show you what
225 changes you made since the tip of the new branch.
227 . When a merge conflict happens during switching branches with
228 the `-m` option, you would see something like this:
230 ------------
231 $ git checkout -m mytopic
232 Auto-merging frotz
233 ERROR: Merge conflict in frotz
234 fatal: merge program failed
235 ------------
237 At this point, `git diff` shows the changes cleanly merged as in
238 the previous example, as well as the changes in the conflicted
239 files.  Edit and resolve the conflict and mark it resolved with
240 `git add` as usual:
242 ------------
243 $ edit frotz
244 $ git add frotz
245 ------------
248 Author
249 ------
250 Written by Linus Torvalds <torvalds@osdl.org>
252 Documentation
253 --------------
254 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
258 Part of the linkgit:git[1] suite