OSDN Git Service

Add Git official document to help
[tortoisegit/TortoiseGitJp.git] / doc / source / en / TortoiseGit / git_doc / git-checkout.html.xml
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">\r
3 \r
4 <article lang="en" id="git-checkout(1)">\r
5 <articleinfo>\r
6     <title>git-checkout(1)</title>\r
7         <indexterm>\r
8                 <primary>git-checkout(1)</primary>\r
9         </indexterm>\r
10 </articleinfo>\r
11 <simplesect id="_name">\r
12 <title>NAME</title>\r
13 <simpara>git-checkout - Checkout a branch or paths to the working tree</simpara>\r
14 </simplesect>\r
15 <simplesect id="_synopsis">\r
16 <title>SYNOPSIS</title>\r
17 <blockquote>\r
18 <literallayout><emphasis>git checkout</emphasis> [-q] [-f] [--track | --no-track] [-b &lt;new_branch&gt; [-l]] [-m] [&lt;branch&gt;]\r
19 <emphasis>git checkout</emphasis> [-f|--ours|--theirs|-m|--conflict=&lt;style&gt;] [&lt;tree-ish&gt;] [--] &lt;paths&gt;&#8230;</literallayout>\r
20 </blockquote>\r
21 </simplesect>\r
22 <simplesect id="_description">\r
23 <title>DESCRIPTION</title>\r
24 <simpara>When &lt;paths&gt; are not given, this command switches branches by\r
25 updating the index and working tree to reflect the specified\r
26 branch, &lt;branch&gt;, and updating HEAD to be &lt;branch&gt; or, if\r
27 specified, &lt;new_branch&gt;.  Using -b will cause &lt;new_branch&gt; to\r
28 be created; in this case you can use the --track or --no-track\r
29 options, which will be passed to <literal>git branch</literal>.</simpara>\r
30 <simpara>As a convenience, --track will default to create a branch whose\r
31 name is constructed from the specified branch name by stripping\r
32 the first namespace level.</simpara>\r
33 <simpara>When &lt;paths&gt; are given, this command does <emphasis role="strong">not</emphasis> switch\r
34 branches.  It updates the named paths in the working tree from\r
35 the index file, or from a named &lt;tree-ish&gt; (most often a commit).  In\r
36 this case, the <literal>-b</literal> options is meaningless and giving\r
37 either of them results in an error.  &lt;tree-ish&gt; argument can be\r
38 used to specify a specific tree-ish (i.e. commit, tag or tree)\r
39 to update the index for the given paths before updating the\r
40 working tree.</simpara>\r
41 <simpara>The index may contain unmerged entries after a failed merge.  By\r
42 default, if you try to check out such an entry from the index, the\r
43 checkout operation will fail and nothing will be checked out.\r
44 Using -f will ignore these unmerged entries.  The contents from a\r
45 specific side of the merge can be checked out of the index by\r
46 using --ours or --theirs.  With -m, changes made to the working tree\r
47 file can be discarded to recreate the original conflicted merge result.</simpara>\r
48 </simplesect>\r
49 <simplesect id="_options">\r
50 <title>OPTIONS</title>\r
51 <variablelist>\r
52 <varlistentry>\r
53 <term>\r
54 -q\r
55 </term>\r
56 <listitem>\r
57 <simpara>\r
58         Quiet, suppress feedback messages.\r
59 </simpara>\r
60 </listitem>\r
61 </varlistentry>\r
62 <varlistentry>\r
63 <term>\r
64 -f\r
65 </term>\r
66 <listitem>\r
67 <simpara>\r
68         When switching branches, proceed even if the index or the\r
69         working tree differs from HEAD.  This is used to throw away\r
70         local changes.\r
71 </simpara>\r
72 <simpara>When checking out paths from the index, do not fail upon unmerged\r
73 entries; instead, unmerged entries are ignored.</simpara>\r
74 </listitem>\r
75 </varlistentry>\r
76 <varlistentry>\r
77 <term>\r
78 --ours\r
79 </term>\r
80 <term>\r
81 --theirs\r
82 </term>\r
83 <listitem>\r
84 <simpara>\r
85         When checking out paths from the index, check out stage #2\r
86         (<emphasis>ours</emphasis>) or #3 (<emphasis>theirs</emphasis>) for unmerged paths.\r
87 </simpara>\r
88 </listitem>\r
89 </varlistentry>\r
90 <varlistentry>\r
91 <term>\r
92 -b\r
93 </term>\r
94 <listitem>\r
95 <simpara>\r
96         Create a new branch named &lt;new_branch&gt; and start it at\r
97         &lt;branch&gt;.  The new branch name must pass all checks defined\r
98         by <xref linkend="git-check-ref-format(1)"/>.  Some of these checks\r
99         may restrict the characters allowed in a branch name.\r
100 </simpara>\r
101 </listitem>\r
102 </varlistentry>\r
103 <varlistentry>\r
104 <term>\r
105 -t\r
106 </term>\r
107 <term>\r
108 --track\r
109 </term>\r
110 <listitem>\r
111 <simpara>\r
112         When creating a new branch, set up configuration so that <emphasis>git-pull</emphasis>\r
113         will automatically retrieve data from the start point, which must be\r
114         a branch. Use this if you always pull from the same upstream branch\r
115         into the new branch, and if you don&#8217;t want to use "git pull\r
116         &lt;repository&gt; &lt;refspec&gt;" explicitly. This behavior is the default\r
117         when the start point is a remote branch. Set the\r
118         branch.autosetupmerge configuration variable to <literal>false</literal> if you want\r
119         <emphasis>git-checkout</emphasis> and <emphasis>git-branch</emphasis> to always behave as if <emphasis>--no-track</emphasis> were\r
120         given. Set it to <literal>always</literal> if you want this behavior when the\r
121         start-point is either a local or remote branch.\r
122 </simpara>\r
123 <simpara>If no <emphasis>-b</emphasis> option was given, the name of the new branch will be\r
124 derived from the remote branch, by attempting to guess the name\r
125 of the branch on remote system.  If "remotes/" or "refs/remotes/"\r
126 are prefixed, it is stripped away, and then the part up to the\r
127 next slash (which would be the nickname of the remote) is removed.\r
128 This would tell us to use "hack" as the local branch when branching\r
129 off of "origin/hack" (or "remotes/origin/hack", or even\r
130 "refs/remotes/origin/hack").  If the given name has no slash, or the above\r
131 guessing results in an empty name, the guessing is aborted.  You can\r
132 explicitly give a name with <emphasis>-b</emphasis> in such a case.</simpara>\r
133 </listitem>\r
134 </varlistentry>\r
135 <varlistentry>\r
136 <term>\r
137 --no-track\r
138 </term>\r
139 <listitem>\r
140 <simpara>\r
141         Ignore the branch.autosetupmerge configuration variable.\r
142 </simpara>\r
143 </listitem>\r
144 </varlistentry>\r
145 <varlistentry>\r
146 <term>\r
147 -l\r
148 </term>\r
149 <listitem>\r
150 <simpara>\r
151         Create the new branch&#8217;s reflog.  This activates recording of\r
152         all changes made to the branch ref, enabling use of date\r
153         based sha1 expressions such as "&lt;branchname&gt;@{yesterday}".\r
154 </simpara>\r
155 </listitem>\r
156 </varlistentry>\r
157 <varlistentry>\r
158 <term>\r
159 -m\r
160 </term>\r
161 <term>\r
162 --merge\r
163 </term>\r
164 <listitem>\r
165 <simpara>\r
166         When switching branches,\r
167         if you have local modifications to one or more files that\r
168         are different between the current branch and the branch to\r
169         which you are switching, the command refuses to switch\r
170         branches in order to preserve your modifications in context.\r
171         However, with this option, a three-way merge between the current\r
172         branch, your working tree contents, and the new branch\r
173         is done, and you will be on the new branch.\r
174 </simpara>\r
175 <simpara>When a merge conflict happens, the index entries for conflicting\r
176 paths are left unmerged, and you need to resolve the conflicts\r
177 and mark the resolved paths with <literal>git add</literal> (or <literal>git rm</literal> if the merge\r
178 should result in deletion of the path).</simpara>\r
179 <simpara>When checking out paths from the index, this option lets you recreate\r
180 the conflicted merge in the specified paths.</simpara>\r
181 </listitem>\r
182 </varlistentry>\r
183 <varlistentry>\r
184 <term>\r
185 --conflict=&lt;style&gt;\r
186 </term>\r
187 <listitem>\r
188 <simpara>\r
189         The same as --merge option above, but changes the way the\r
190         conflicting hunks are presented, overriding the\r
191         merge.conflictstyle configuration variable.  Possible values are\r
192         "merge" (default) and "diff3" (in addition to what is shown by\r
193         "merge" style, shows the original contents).\r
194 </simpara>\r
195 </listitem>\r
196 </varlistentry>\r
197 <varlistentry>\r
198 <term>\r
199 &lt;new_branch&gt;\r
200 </term>\r
201 <listitem>\r
202 <simpara>\r
203         Name for the new branch.\r
204 </simpara>\r
205 </listitem>\r
206 </varlistentry>\r
207 <varlistentry>\r
208 <term>\r
209 &lt;branch&gt;\r
210 </term>\r
211 <listitem>\r
212 <simpara>\r
213         Branch to checkout; may be any object ID that resolves to a\r
214         commit.  Defaults to HEAD.\r
215 </simpara>\r
216 <simpara>When this parameter names a non-branch (but still a valid commit object),\r
217 your HEAD becomes <emphasis>detached</emphasis>.</simpara>\r
218 <simpara>As a special case, the "<literal>@{-N}</literal>" syntax for the N-th last branch\r
219 checks out the branch (instead of detaching).  You may also specify\r
220 "<literal>-</literal>" which is synonymous with "<literal>@{-1}</literal>".</simpara>\r
221 </listitem>\r
222 </varlistentry>\r
223 </variablelist>\r
224 </simplesect>\r
225 <simplesect id="_detached_head">\r
226 <title>Detached HEAD</title>\r
227 <simpara>It is sometimes useful to be able to <emphasis>checkout</emphasis> a commit that is\r
228 not at the tip of one of your branches.  The most obvious\r
229 example is to check out the commit at a tagged official release\r
230 point, like this:</simpara>\r
231 <literallayout>$ git checkout v2.6.18</literallayout>\r
232 <simpara>Earlier versions of git did not allow this and asked you to\r
233 create a temporary branch using <literal>-b</literal> option, but starting from\r
234 version 1.5.0, the above command <emphasis>detaches</emphasis> your HEAD from the\r
235 current branch and directly point at the commit named by the tag\r
236 (<literal>v2.6.18</literal> in the above example).</simpara>\r
237 <simpara>You can use usual git commands while in this state.  You can use\r
238 <literal>git reset --hard $othercommit</literal> to further move around, for\r
239 example.  You can make changes and create a new commit on top of\r
240 a detached HEAD.  You can even create a merge by using <literal>git\r
241 merge $othercommit</literal>.</simpara>\r
242 <simpara>The state you are in while your HEAD is detached is not recorded\r
243 by any branch (which is natural --- you are not on any branch).\r
244 What this means is that you can discard your temporary commits\r
245 and merges by switching back to an existing branch (e.g. <literal>git\r
246 checkout master</literal>), and a later <literal>git prune</literal> or <literal>git gc</literal> would\r
247 garbage-collect them.  If you did this by mistake, you can ask\r
248 the reflog for HEAD where you were, e.g.</simpara>\r
249 <literallayout>$ git log -g -2 HEAD</literallayout>\r
250 </simplesect>\r
251 <simplesect id="_examples">\r
252 <title>EXAMPLES</title>\r
253 <orderedlist numeration="arabic">\r
254 <listitem>\r
255 <simpara>\r
256 The following sequence checks out the <literal>master</literal> branch, reverts\r
257 the <literal>Makefile</literal> to two revisions back, deletes hello.c by\r
258 mistake, and gets it back from the index.\r
259 </simpara>\r
260 <literallayout>$ git checkout master             <co id="CO1-1"/>\r
261 $ git checkout master~2 Makefile  <co id="CO1-2"/>\r
262 $ rm -f hello.c\r
263 $ git checkout hello.c            <co id="CO1-3"/></literallayout>\r
264 <calloutlist>\r
265 <callout arearefs="CO1-1">\r
266 <simpara>\r
267 switch branch\r
268 </simpara>\r
269 </callout>\r
270 <callout arearefs="CO1-2">\r
271 <simpara>\r
272 take out a file out of other commit\r
273 </simpara>\r
274 </callout>\r
275 <callout arearefs="CO1-3">\r
276 <simpara>\r
277 restore hello.c from HEAD of current branch\r
278 </simpara>\r
279 <simpara>If you have an unfortunate branch that is named <literal>hello.c</literal>, this\r
280 step would be confused as an instruction to switch to that branch.\r
281 You should instead write:</simpara>\r
282 <literallayout>$ git checkout -- hello.c</literallayout>\r
283 </callout>\r
284 </calloutlist>\r
285 </listitem>\r
286 <listitem>\r
287 <simpara>\r
288 After working in a wrong branch, switching to the correct\r
289 branch would be done using:\r
290 </simpara>\r
291 <literallayout>$ git checkout mytopic</literallayout>\r
292 <simpara>However, your "wrong" branch and correct "mytopic" branch may\r
293 differ in files that you have locally modified, in which case,\r
294 the above checkout would fail like this:</simpara>\r
295 <literallayout>$ git checkout mytopic\r
296 fatal: Entry 'frotz' not uptodate. Cannot merge.</literallayout>\r
297 <simpara>You can give the <literal>-m</literal> flag to the command, which would try a\r
298 three-way merge:</simpara>\r
299 <literallayout>$ git checkout -m mytopic\r
300 Auto-merging frotz</literallayout>\r
301 <simpara>After this three-way merge, the local modifications are <emphasis>not</emphasis>\r
302 registered in your index file, so <literal>git diff</literal> would show you what\r
303 changes you made since the tip of the new branch.</simpara>\r
304 </listitem>\r
305 <listitem>\r
306 <simpara>\r
307 When a merge conflict happens during switching branches with\r
308 the <literal>-m</literal> option, you would see something like this:\r
309 </simpara>\r
310 <literallayout>$ git checkout -m mytopic\r
311 Auto-merging frotz\r
312 ERROR: Merge conflict in frotz\r
313 fatal: merge program failed</literallayout>\r
314 <simpara>At this point, <literal>git diff</literal> shows the changes cleanly merged as in\r
315 the previous example, as well as the changes in the conflicted\r
316 files.  Edit and resolve the conflict and mark it resolved with\r
317 <literal>git add</literal> as usual:</simpara>\r
318 <literallayout>$ edit frotz\r
319 $ git add frotz</literallayout>\r
320 </listitem>\r
321 </orderedlist>\r
322 </simplesect>\r
323 <simplesect id="_author">\r
324 <title>Author</title>\r
325 <simpara>Written by Linus Torvalds &lt;<ulink url="mailto:torvalds@osdl.org">torvalds@osdl.org</ulink>&gt;</simpara>\r
326 </simplesect>\r
327 <simplesect id="_documentation">\r
328 <title>Documentation</title>\r
329 <simpara>Documentation by Junio C Hamano and the git-list &lt;<ulink url="mailto:git@vger.kernel.org">git@vger.kernel.org</ulink>&gt;.</simpara>\r
330 </simplesect>\r
331 <simplesect id="_git">\r
332 <title>GIT</title>\r
333 <simpara>Part of the <xref linkend="git(1)"/> suite</simpara>\r
334 </simplesect>\r
335 </article>\r