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