Autogenerated HTML docs for v1.7.4.1-266-ga62ea
[git/jnareb-git.git] / git-checkout.html
blob96201509a09bb8138ec2660f6cfacdaf37c0482e
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <meta name="generator" content="AsciiDoc 8.4.5" />
7 <title>git-checkout(1)</title>
8 <style type="text/css">
9 /* Debug borders */
10 p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
12 border: 1px solid red;
16 body {
17 margin: 1em 5% 1em 5%;
20 a {
21 color: blue;
22 text-decoration: underline;
24 a:visited {
25 color: fuchsia;
28 em {
29 font-style: italic;
30 color: navy;
33 strong {
34 font-weight: bold;
35 color: #083194;
38 tt {
39 color: navy;
42 h1, h2, h3, h4, h5, h6 {
43 color: #527bbd;
44 font-family: sans-serif;
45 margin-top: 1.2em;
46 margin-bottom: 0.5em;
47 line-height: 1.3;
50 h1, h2, h3 {
51 border-bottom: 2px solid silver;
53 h2 {
54 padding-top: 0.5em;
56 h3 {
57 float: left;
59 h3 + * {
60 clear: left;
63 div.sectionbody {
64 font-family: serif;
65 margin-left: 0;
68 hr {
69 border: 1px solid silver;
72 p {
73 margin-top: 0.5em;
74 margin-bottom: 0.5em;
77 ul, ol, li > p {
78 margin-top: 0;
81 pre {
82 padding: 0;
83 margin: 0;
86 span#author {
87 color: #527bbd;
88 font-family: sans-serif;
89 font-weight: bold;
90 font-size: 1.1em;
92 span#email {
94 span#revnumber, span#revdate, span#revremark {
95 font-family: sans-serif;
98 div#footer {
99 font-family: sans-serif;
100 font-size: small;
101 border-top: 2px solid silver;
102 padding-top: 0.5em;
103 margin-top: 4.0em;
105 div#footer-text {
106 float: left;
107 padding-bottom: 0.5em;
109 div#footer-badges {
110 float: right;
111 padding-bottom: 0.5em;
114 div#preamble {
115 margin-top: 1.5em;
116 margin-bottom: 1.5em;
118 div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
119 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
120 div.admonitionblock {
121 margin-top: 1.5em;
122 margin-bottom: 1.5em;
124 div.admonitionblock {
125 margin-top: 2.5em;
126 margin-bottom: 2.5em;
129 div.content { /* Block element content. */
130 padding: 0;
133 /* Block element titles. */
134 div.title, caption.title {
135 color: #527bbd;
136 font-family: sans-serif;
137 font-weight: bold;
138 text-align: left;
139 margin-top: 1.0em;
140 margin-bottom: 0.5em;
142 div.title + * {
143 margin-top: 0;
146 td div.title:first-child {
147 margin-top: 0.0em;
149 div.content div.title:first-child {
150 margin-top: 0.0em;
152 div.content + div.title {
153 margin-top: 0.0em;
156 div.sidebarblock > div.content {
157 background: #ffffee;
158 border: 1px solid silver;
159 padding: 0.5em;
162 div.listingblock > div.content {
163 border: 1px solid silver;
164 background: #f4f4f4;
165 padding: 0.5em;
168 div.quoteblock {
169 padding-left: 2.0em;
170 margin-right: 10%;
172 div.quoteblock > div.attribution {
173 padding-top: 0.5em;
174 text-align: right;
177 div.verseblock {
178 padding-left: 2.0em;
179 margin-right: 10%;
181 div.verseblock > div.content {
182 white-space: pre;
184 div.verseblock > div.attribution {
185 padding-top: 0.75em;
186 text-align: left;
188 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
189 div.verseblock + div.attribution {
190 text-align: left;
193 div.admonitionblock .icon {
194 vertical-align: top;
195 font-size: 1.1em;
196 font-weight: bold;
197 text-decoration: underline;
198 color: #527bbd;
199 padding-right: 0.5em;
201 div.admonitionblock td.content {
202 padding-left: 0.5em;
203 border-left: 2px solid silver;
206 div.exampleblock > div.content {
207 border-left: 2px solid silver;
208 padding: 0.5em;
211 div.imageblock div.content { padding-left: 0; }
212 span.image img { border-style: none; }
213 a.image:visited { color: white; }
215 dl {
216 margin-top: 0.8em;
217 margin-bottom: 0.8em;
219 dt {
220 margin-top: 0.5em;
221 margin-bottom: 0;
222 font-style: normal;
223 color: navy;
225 dd > *:first-child {
226 margin-top: 0.1em;
229 ul, ol {
230 list-style-position: outside;
232 ol.arabic {
233 list-style-type: decimal;
235 ol.loweralpha {
236 list-style-type: lower-alpha;
238 ol.upperalpha {
239 list-style-type: upper-alpha;
241 ol.lowerroman {
242 list-style-type: lower-roman;
244 ol.upperroman {
245 list-style-type: upper-roman;
248 div.compact ul, div.compact ol,
249 div.compact p, div.compact p,
250 div.compact div, div.compact div {
251 margin-top: 0.1em;
252 margin-bottom: 0.1em;
255 div.tableblock > table {
256 border: 3px solid #527bbd;
258 thead {
259 font-family: sans-serif;
260 font-weight: bold;
262 tfoot {
263 font-weight: bold;
265 td > div.verse {
266 white-space: pre;
268 p.table {
269 margin-top: 0;
271 /* Because the table frame attribute is overriden by CSS in most browsers. */
272 div.tableblock > table[frame="void"] {
273 border-style: none;
275 div.tableblock > table[frame="hsides"] {
276 border-left-style: none;
277 border-right-style: none;
279 div.tableblock > table[frame="vsides"] {
280 border-top-style: none;
281 border-bottom-style: none;
285 div.hdlist {
286 margin-top: 0.8em;
287 margin-bottom: 0.8em;
289 div.hdlist tr {
290 padding-bottom: 15px;
292 dt.hdlist1.strong, td.hdlist1.strong {
293 font-weight: bold;
295 td.hdlist1 {
296 vertical-align: top;
297 font-style: normal;
298 padding-right: 0.8em;
299 color: navy;
301 td.hdlist2 {
302 vertical-align: top;
304 div.hdlist.compact tr {
305 margin: 0;
306 padding-bottom: 0;
309 .comment {
310 background: yellow;
313 @media print {
314 div#footer-badges { display: none; }
317 div#toctitle {
318 color: #527bbd;
319 font-family: sans-serif;
320 font-size: 1.1em;
321 font-weight: bold;
322 margin-top: 1.0em;
323 margin-bottom: 0.1em;
326 div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
327 margin-top: 0;
328 margin-bottom: 0;
330 div.toclevel2 {
331 margin-left: 2em;
332 font-size: 0.9em;
334 div.toclevel3 {
335 margin-left: 4em;
336 font-size: 0.9em;
338 div.toclevel4 {
339 margin-left: 6em;
340 font-size: 0.9em;
342 /* Overrides for manpage documents */
343 h1 {
344 padding-top: 0.5em;
345 padding-bottom: 0.5em;
346 border-top: 2px solid silver;
347 border-bottom: 2px solid silver;
349 h2 {
350 border-style: none;
352 div.sectionbody {
353 margin-left: 5%;
356 @media print {
357 div#toc { display: none; }
360 /* Workarounds for IE6's broken and incomplete CSS2. */
362 div.sidebar-content {
363 background: #ffffee;
364 border: 1px solid silver;
365 padding: 0.5em;
367 div.sidebar-title, div.image-title {
368 color: #527bbd;
369 font-family: sans-serif;
370 font-weight: bold;
371 margin-top: 0.0em;
372 margin-bottom: 0.5em;
375 div.listingblock div.content {
376 border: 1px solid silver;
377 background: #f4f4f4;
378 padding: 0.5em;
381 div.quoteblock-attribution {
382 padding-top: 0.5em;
383 text-align: right;
386 div.verseblock-content {
387 white-space: pre;
389 div.verseblock-attribution {
390 padding-top: 0.75em;
391 text-align: left;
394 div.exampleblock-content {
395 border-left: 2px solid silver;
396 padding-left: 0.5em;
399 /* IE6 sets dynamically generated links as visited. */
400 div#toc a:visited { color: blue; }
401 </style>
402 </head>
403 <body>
404 <div id="header">
405 <h1>
406 git-checkout(1) Manual Page
407 </h1>
408 <h2>NAME</h2>
409 <div class="sectionbody">
410 <p>git-checkout -
411 Checkout a branch or paths to the working tree
412 </p>
413 </div>
414 </div>
415 <h2 id="_synopsis">SYNOPSIS</h2>
416 <div class="sectionbody">
417 <div class="verseblock">
418 <div class="verseblock-content"><em>git checkout</em> [-q] [-f] [-m] [&lt;branch&gt;]
419 <em>git checkout</em> [-q] [-f] [-m] [--detach] [&lt;commit&gt;]
420 <em>git checkout</em> [-q] [-f] [-m] [[-b|-B|--orphan] &lt;new_branch&gt;] [&lt;start_point&gt;]
421 <em>git checkout</em> [-f|--ours|--theirs|-m|--conflict=&lt;style&gt;] [&lt;tree-ish&gt;] [--] &lt;paths&gt;&#8230;
422 <em>git checkout</em> --patch [&lt;tree-ish&gt;] [--] [&lt;paths&gt;&#8230;]</div>
423 <div class="verseblock-attribution">
424 </div></div>
425 </div>
426 <h2 id="_description">DESCRIPTION</h2>
427 <div class="sectionbody">
428 <div class="paragraph"><p>Updates files in the working tree to match the version in the index
429 or the specified tree. If no paths are given, <em>git checkout</em> will
430 also update <tt>HEAD</tt> to set the specified branch as the current
431 branch.</p></div>
432 <div class="dlist"><dl>
433 <dt class="hdlist1">
434 <em>git checkout</em> [&lt;branch&gt;]
435 </dt>
436 <dt class="hdlist1">
437 <em>git checkout</em> -b|-B &lt;new_branch&gt; [&lt;start point&gt;]
438 </dt>
439 <dt class="hdlist1">
440 <em>git checkout</em> [--detach] [&lt;commit&gt;]
441 </dt>
442 <dd>
444 This form switches branches by updating the index, working
445 tree, and HEAD to reflect the specified branch or commit.
446 </p>
447 <div class="paragraph"><p>If <tt>-b</tt> is given, a new branch is created as if <a href="git-branch.html">git-branch(1)</a>
448 were called and then checked out; in this case you can
449 use the <tt>--track</tt> or <tt>--no-track</tt> options, which will be passed to
450 <em>git branch</em>. As a convenience, <tt>--track</tt> without <tt>-b</tt> implies branch
451 creation; see the description of <tt>--track</tt> below.</p></div>
452 <div class="paragraph"><p>If <tt>-B</tt> is given, &lt;new_branch&gt; is created if it doesn&#8217;t exist; otherwise, it
453 is reset. This is the transactional equivalent of</p></div>
454 <div class="listingblock">
455 <div class="content">
456 <pre><tt>$ git branch -f &lt;branch&gt; [&lt;start point&gt;]
457 $ git checkout &lt;branch&gt;</tt></pre>
458 </div></div>
459 <div class="paragraph"><p>that is to say, the branch is not reset/created unless "git checkout" is
460 successful.</p></div>
461 </dd>
462 <dt class="hdlist1">
463 <em>git checkout</em> [--patch] [&lt;tree-ish&gt;] [--] &lt;pathspec&gt;&#8230;
464 </dt>
465 <dd>
467 When &lt;paths&gt; or <tt>--patch</tt> are given, <em>git checkout</em> does <strong>not</strong>
468 switch branches. It updates the named paths in the working tree
469 from the index file or from a named &lt;tree-ish&gt; (most often a
470 commit). In this case, the <tt>-b</tt> and <tt>--track</tt> options are
471 meaningless and giving either of them results in an error. The
472 &lt;tree-ish&gt; argument can be used to specify a specific tree-ish
473 (i.e. commit, tag or tree) to update the index for the given
474 paths before updating the working tree.
475 </p>
476 <div class="paragraph"><p>The index may contain unmerged entries because of a previous failed merge.
477 By default, if you try to check out such an entry from the index, the
478 checkout operation will fail and nothing will be checked out.
479 Using <tt>-f</tt> will ignore these unmerged entries. The contents from a
480 specific side of the merge can be checked out of the index by
481 using <tt>--ours</tt> or <tt>--theirs</tt>. With <tt>-m</tt>, changes made to the working tree
482 file can be discarded to re-create the original conflicted merge result.</p></div>
483 </dd>
484 </dl></div>
485 </div>
486 <h2 id="_options">OPTIONS</h2>
487 <div class="sectionbody">
488 <div class="dlist"><dl>
489 <dt class="hdlist1">
491 </dt>
492 <dt class="hdlist1">
493 --quiet
494 </dt>
495 <dd>
497 Quiet, suppress feedback messages.
498 </p>
499 </dd>
500 <dt class="hdlist1">
502 </dt>
503 <dt class="hdlist1">
504 --force
505 </dt>
506 <dd>
508 When switching branches, proceed even if the index or the
509 working tree differs from HEAD. This is used to throw away
510 local changes.
511 </p>
512 <div class="paragraph"><p>When checking out paths from the index, do not fail upon unmerged
513 entries; instead, unmerged entries are ignored.</p></div>
514 </dd>
515 <dt class="hdlist1">
516 --ours
517 </dt>
518 <dt class="hdlist1">
519 --theirs
520 </dt>
521 <dd>
523 When checking out paths from the index, check out stage #2
524 (<em>ours</em>) or #3 (<em>theirs</em>) for unmerged paths.
525 </p>
526 </dd>
527 <dt class="hdlist1">
529 </dt>
530 <dd>
532 Create a new branch named &lt;new_branch&gt; and start it at
533 &lt;start_point&gt;; see <a href="git-branch.html">git-branch(1)</a> for details.
534 </p>
535 </dd>
536 <dt class="hdlist1">
538 </dt>
539 <dd>
541 Creates the branch &lt;new_branch&gt; and start it at &lt;start_point&gt;;
542 if it already exists, then reset it to &lt;start_point&gt;. This is
543 equivalent to running "git branch" with "-f"; see
544 <a href="git-branch.html">git-branch(1)</a> for details.
545 </p>
546 </dd>
547 <dt class="hdlist1">
549 </dt>
550 <dt class="hdlist1">
551 --track
552 </dt>
553 <dd>
555 When creating a new branch, set up "upstream" configuration. See
556 "--track" in <a href="git-branch.html">git-branch(1)</a> for details.
557 </p>
558 <div class="paragraph"><p>If no <em>-b</em> option is given, the name of the new branch will be
559 derived from the remote-tracking branch. If "remotes/" or "refs/remotes/"
560 is prefixed it is stripped away, and then the part up to the
561 next slash (which would be the nickname of the remote) is removed.
562 This would tell us to use "hack" as the local branch when branching
563 off of "origin/hack" (or "remotes/origin/hack", or even
564 "refs/remotes/origin/hack"). If the given name has no slash, or the above
565 guessing results in an empty name, the guessing is aborted. You can
566 explicitly give a name with <em>-b</em> in such a case.</p></div>
567 </dd>
568 <dt class="hdlist1">
569 --no-track
570 </dt>
571 <dd>
573 Do not set up "upstream" configuration, even if the
574 branch.autosetupmerge configuration variable is true.
575 </p>
576 </dd>
577 <dt class="hdlist1">
579 </dt>
580 <dd>
582 Create the new branch&#8217;s reflog; see <a href="git-branch.html">git-branch(1)</a> for
583 details.
584 </p>
585 </dd>
586 <dt class="hdlist1">
587 --detach
588 </dt>
589 <dd>
591 Rather than checking out a branch to work on it, check out a
592 commit for inspection and discardable experiments.
593 This is the default behavior of "git checkout &lt;commit&gt;" when
594 &lt;commit&gt; is not a branch name. See the "DETACHED HEAD" section
595 below for details.
596 </p>
597 </dd>
598 <dt class="hdlist1">
599 --orphan
600 </dt>
601 <dd>
603 Create a new <em>orphan</em> branch, named &lt;new_branch&gt;, started from
604 &lt;start_point&gt; and switch to it. The first commit made on this
605 new branch will have no parents and it will be the root of a new
606 history totally disconnected from all the other branches and
607 commits.
608 </p>
609 <div class="paragraph"><p>The index and the working tree are adjusted as if you had previously run
610 "git checkout &lt;start_point&gt;". This allows you to start a new history
611 that records a set of paths similar to &lt;start_point&gt; by easily running
612 "git commit -a" to make the root commit.</p></div>
613 <div class="paragraph"><p>This can be useful when you want to publish the tree from a commit
614 without exposing its full history. You might want to do this to publish
615 an open source branch of a project whose current tree is "clean", but
616 whose full history contains proprietary or otherwise encumbered bits of
617 code.</p></div>
618 <div class="paragraph"><p>If you want to start a disconnected history that records a set of paths
619 that is totally different from the one of &lt;start_point&gt;, then you should
620 clear the index and the working tree right after creating the orphan
621 branch by running "git rm -rf ." from the top level of the working tree.
622 Afterwards you will be ready to prepare your new files, repopulating the
623 working tree, by copying them from elsewhere, extracting a tarball, etc.</p></div>
624 </dd>
625 <dt class="hdlist1">
627 </dt>
628 <dt class="hdlist1">
629 --merge
630 </dt>
631 <dd>
633 When switching branches,
634 if you have local modifications to one or more files that
635 are different between the current branch and the branch to
636 which you are switching, the command refuses to switch
637 branches in order to preserve your modifications in context.
638 However, with this option, a three-way merge between the current
639 branch, your working tree contents, and the new branch
640 is done, and you will be on the new branch.
641 </p>
642 <div class="paragraph"><p>When a merge conflict happens, the index entries for conflicting
643 paths are left unmerged, and you need to resolve the conflicts
644 and mark the resolved paths with <tt>git add</tt> (or <tt>git rm</tt> if the merge
645 should result in deletion of the path).</p></div>
646 <div class="paragraph"><p>When checking out paths from the index, this option lets you recreate
647 the conflicted merge in the specified paths.</p></div>
648 </dd>
649 <dt class="hdlist1">
650 --conflict=&lt;style&gt;
651 </dt>
652 <dd>
654 The same as --merge option above, but changes the way the
655 conflicting hunks are presented, overriding the
656 merge.conflictstyle configuration variable. Possible values are
657 "merge" (default) and "diff3" (in addition to what is shown by
658 "merge" style, shows the original contents).
659 </p>
660 </dd>
661 <dt class="hdlist1">
663 </dt>
664 <dt class="hdlist1">
665 --patch
666 </dt>
667 <dd>
669 Interactively select hunks in the difference between the
670 &lt;tree-ish&gt; (or the index, if unspecified) and the working
671 tree. The chosen hunks are then applied in reverse to the
672 working tree (and if a &lt;tree-ish&gt; was specified, the index).
673 </p>
674 <div class="paragraph"><p>This means that you can use <tt>git checkout -p</tt> to selectively discard
675 edits from your current working tree.</p></div>
676 </dd>
677 <dt class="hdlist1">
678 &lt;branch&gt;
679 </dt>
680 <dd>
682 Branch to checkout; if it refers to a branch (i.e., a name that,
683 when prepended with "refs/heads/", is a valid ref), then that
684 branch is checked out. Otherwise, if it refers to a valid
685 commit, your HEAD becomes "detached" and you are no longer on
686 any branch (see below for details).
687 </p>
688 <div class="paragraph"><p>As a special case, the <tt>"@{-N}"</tt> syntax for the N-th last branch
689 checks out the branch (instead of detaching). You may also specify
690 <tt>-</tt> which is synonymous with <tt>"@{-1}"</tt>.</p></div>
691 <div class="paragraph"><p>As a further special case, you may use <tt>"A...B"</tt> as a shortcut for the
692 merge base of <tt>A</tt> and <tt>B</tt> if there is exactly one merge base. You can
693 leave out at most one of <tt>A</tt> and <tt>B</tt>, in which case it defaults to <tt>HEAD</tt>.</p></div>
694 </dd>
695 <dt class="hdlist1">
696 &lt;new_branch&gt;
697 </dt>
698 <dd>
700 Name for the new branch.
701 </p>
702 </dd>
703 <dt class="hdlist1">
704 &lt;start_point&gt;
705 </dt>
706 <dd>
708 The name of a commit at which to start the new branch; see
709 <a href="git-branch.html">git-branch(1)</a> for details. Defaults to HEAD.
710 </p>
711 </dd>
712 <dt class="hdlist1">
713 &lt;tree-ish&gt;
714 </dt>
715 <dd>
717 Tree to checkout from (when paths are given). If not specified,
718 the index will be used.
719 </p>
720 </dd>
721 </dl></div>
722 </div>
723 <h2 id="_detached_head">DETACHED HEAD</h2>
724 <div class="sectionbody">
725 <div class="paragraph"><p>HEAD normally refers to a named branch (e.g. <em>master</em>). Meanwhile, each
726 branch refers to a specific commit. Let&#8217;s look at a repo with three
727 commits, one of them tagged, and with branch <em>master</em> checked out:</p></div>
728 <div class="listingblock">
729 <div class="content">
730 <pre><tt> HEAD (refers to branch 'master')
733 a---b---c branch 'master' (refers to commit 'c')
736 tag 'v2.0' (refers to commit 'b')</tt></pre>
737 </div></div>
738 <div class="paragraph"><p>When a commit is created in this state, the branch is updated to refer to
739 the new commit. Specifically, <em>git commit</em> creates a new commit <em>d</em>, whose
740 parent is commit <em>c</em>, and then updates branch <em>master</em> to refer to new
741 commit <em>d</em>. HEAD still refers to branch <em>master</em> and so indirectly now refers
742 to commit <em>d</em>:</p></div>
743 <div class="listingblock">
744 <div class="content">
745 <pre><tt>$ edit; git add; git commit
747 HEAD (refers to branch 'master')
750 a---b---c---d branch 'master' (refers to commit 'd')
753 tag 'v2.0' (refers to commit 'b')</tt></pre>
754 </div></div>
755 <div class="paragraph"><p>It is sometimes useful to be able to checkout a commit that is not at
756 the tip of any named branch, or even to create a new commit that is not
757 referenced by a named branch. Let&#8217;s look at what happens when we
758 checkout commit <em>b</em> (here we show two ways this may be done):</p></div>
759 <div class="listingblock">
760 <div class="content">
761 <pre><tt>$ git checkout v2.0 # or
762 $ git checkout master^^
764 HEAD (refers to commit 'b')
767 a---b---c---d branch 'master' (refers to commit 'd')
770 tag 'v2.0' (refers to commit 'b')</tt></pre>
771 </div></div>
772 <div class="paragraph"><p>Notice that regardless of which checkout command we use, HEAD now refers
773 directly to commit <em>b</em>. This is known as being in detached HEAD state.
774 It means simply that HEAD refers to a specific commit, as opposed to
775 referring to a named branch. Let&#8217;s see what happens when we create a commit:</p></div>
776 <div class="listingblock">
777 <div class="content">
778 <pre><tt>$ edit; git add; git commit
780 HEAD (refers to commit 'e')
785 a---b---c---d branch 'master' (refers to commit 'd')
788 tag 'v2.0' (refers to commit 'b')</tt></pre>
789 </div></div>
790 <div class="paragraph"><p>There is now a new commit <em>e</em>, but it is referenced only by HEAD. We can
791 of course add yet another commit in this state:</p></div>
792 <div class="listingblock">
793 <div class="content">
794 <pre><tt>$ edit; git add; git commit
796 HEAD (refers to commit 'f')
799 e---f
801 a---b---c---d branch 'master' (refers to commit 'd')
804 tag 'v2.0' (refers to commit 'b')</tt></pre>
805 </div></div>
806 <div class="paragraph"><p>In fact, we can perform all the normal git operations. But, let&#8217;s look
807 at what happens when we then checkout master:</p></div>
808 <div class="listingblock">
809 <div class="content">
810 <pre><tt>$ git checkout master
812 HEAD (refers to branch 'master')
813 e---f |
815 a---b---c---d branch 'master' (refers to commit 'd')
818 tag 'v2.0' (refers to commit 'b')</tt></pre>
819 </div></div>
820 <div class="paragraph"><p>It is important to realize that at this point nothing refers to commit
821 <em>f</em>. Eventually commit <em>f</em> (and by extension commit <em>e</em>) will be deleted
822 by the routine git garbage collection process, unless we create a reference
823 before that happens. If we have not yet moved away from commit <em>f</em>,
824 any of these will create a reference to it:</p></div>
825 <div class="listingblock">
826 <div class="content">
827 <pre><tt>$ git checkout -b foo <b>&lt;1&gt;</b>
828 $ git branch foo <b>&lt;2&gt;</b>
829 $ git tag foo <b>&lt;3&gt;</b></tt></pre>
830 </div></div>
831 <div class="colist arabic"><ol>
832 <li>
834 creates a new branch <em>foo</em>, which refers to commit <em>f</em>, and then
835 updates HEAD to refer to branch <em>foo</em>. In other words, we&#8217;ll no longer
836 be in detached HEAD state after this command.
837 </p>
838 </li>
839 <li>
841 similarly creates a new branch <em>foo</em>, which refers to commit <em>f</em>,
842 but leaves HEAD detached.
843 </p>
844 </li>
845 <li>
847 creates a new tag <em>foo</em>, which refers to commit <em>f</em>,
848 leaving HEAD detached.
849 </p>
850 </li>
851 </ol></div>
852 <div class="paragraph"><p>If we have moved away from commit <em>f</em>, then we must first recover its object
853 name (typically by using git reflog), and then we can create a reference to
854 it. For example, to see the last two commits to which HEAD referred, we
855 can use either of these commands:</p></div>
856 <div class="listingblock">
857 <div class="content">
858 <pre><tt>$ git reflog -2 HEAD # or
859 $ git log -g -2 HEAD</tt></pre>
860 </div></div>
861 </div>
862 <h2 id="_examples">EXAMPLES</h2>
863 <div class="sectionbody">
864 <div class="olist arabic"><ol class="arabic">
865 <li>
867 The following sequence checks out the <tt>master</tt> branch, reverts
868 the <tt>Makefile</tt> to two revisions back, deletes hello.c by
869 mistake, and gets it back from the index.
870 </p>
871 <div class="listingblock">
872 <div class="content">
873 <pre><tt>$ git checkout master <b>&lt;1&gt;</b>
874 $ git checkout master~2 Makefile <b>&lt;2&gt;</b>
875 $ rm -f hello.c
876 $ git checkout hello.c <b>&lt;3&gt;</b></tt></pre>
877 </div></div>
878 <div class="colist arabic"><ol>
879 <li>
881 switch branch
882 </p>
883 </li>
884 <li>
886 take a file out of another commit
887 </p>
888 </li>
889 <li>
891 restore hello.c from the index
892 </p>
893 <div class="paragraph"><p>If you have an unfortunate branch that is named <tt>hello.c</tt>, this
894 step would be confused as an instruction to switch to that branch.
895 You should instead write:</p></div>
896 <div class="listingblock">
897 <div class="content">
898 <pre><tt>$ git checkout -- hello.c</tt></pre>
899 </div></div>
900 </li>
901 </ol></div>
902 </li>
903 <li>
905 After working in the wrong branch, switching to the correct
906 branch would be done using:
907 </p>
908 <div class="listingblock">
909 <div class="content">
910 <pre><tt>$ git checkout mytopic</tt></pre>
911 </div></div>
912 <div class="paragraph"><p>However, your "wrong" branch and correct "mytopic" branch may
913 differ in files that you have modified locally, in which case
914 the above checkout would fail like this:</p></div>
915 <div class="listingblock">
916 <div class="content">
917 <pre><tt>$ git checkout mytopic
918 error: You have local changes to 'frotz'; not switching branches.</tt></pre>
919 </div></div>
920 <div class="paragraph"><p>You can give the <tt>-m</tt> flag to the command, which would try a
921 three-way merge:</p></div>
922 <div class="listingblock">
923 <div class="content">
924 <pre><tt>$ git checkout -m mytopic
925 Auto-merging frotz</tt></pre>
926 </div></div>
927 <div class="paragraph"><p>After this three-way merge, the local modifications are <em>not</em>
928 registered in your index file, so <tt>git diff</tt> would show you what
929 changes you made since the tip of the new branch.</p></div>
930 </li>
931 <li>
933 When a merge conflict happens during switching branches with
934 the <tt>-m</tt> option, you would see something like this:
935 </p>
936 <div class="listingblock">
937 <div class="content">
938 <pre><tt>$ git checkout -m mytopic
939 Auto-merging frotz
940 ERROR: Merge conflict in frotz
941 fatal: merge program failed</tt></pre>
942 </div></div>
943 <div class="paragraph"><p>At this point, <tt>git diff</tt> shows the changes cleanly merged as in
944 the previous example, as well as the changes in the conflicted
945 files. Edit and resolve the conflict and mark it resolved with
946 <tt>git add</tt> as usual:</p></div>
947 <div class="listingblock">
948 <div class="content">
949 <pre><tt>$ edit frotz
950 $ git add frotz</tt></pre>
951 </div></div>
952 </li>
953 </ol></div>
954 </div>
955 <h2 id="_git">GIT</h2>
956 <div class="sectionbody">
957 <div class="paragraph"><p>Part of the <a href="git.html">git(1)</a> suite</p></div>
958 </div>
959 <div id="footer">
960 <div id="footer-text">
961 Last updated 2011-03-15 23:30:13 UTC
962 </div>
963 </div>
964 </body>
965 </html>