Autogenerated HTML docs for v1.7.7-325-g05f6ed
[git/jnareb-git.git] / git-checkout.html
blobc8e300fc3bb76e57c5ee66b1a102944137a2948c
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.5.2" />
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.0em;
122 margin-bottom: 1.5em;
124 div.admonitionblock {
125 margin-top: 2.0em;
126 margin-bottom: 2.0em;
127 margin-right: 10%;
128 color: #606060;
131 div.content { /* Block element content. */
132 padding: 0;
135 /* Block element titles. */
136 div.title, caption.title {
137 color: #527bbd;
138 font-family: sans-serif;
139 font-weight: bold;
140 text-align: left;
141 margin-top: 1.0em;
142 margin-bottom: 0.5em;
144 div.title + * {
145 margin-top: 0;
148 td div.title:first-child {
149 margin-top: 0.0em;
151 div.content div.title:first-child {
152 margin-top: 0.0em;
154 div.content + div.title {
155 margin-top: 0.0em;
158 div.sidebarblock > div.content {
159 background: #ffffee;
160 border: 1px solid silver;
161 padding: 0.5em;
164 div.listingblock > div.content {
165 border: 1px solid silver;
166 background: #f4f4f4;
167 padding: 0.5em;
170 div.quoteblock, div.verseblock {
171 padding-left: 1.0em;
172 margin-left: 1.0em;
173 margin-right: 10%;
174 border-left: 5px solid #dddddd;
175 color: #777777;
178 div.quoteblock > div.attribution {
179 padding-top: 0.5em;
180 text-align: right;
183 div.verseblock > div.content {
184 white-space: pre;
186 div.verseblock > div.attribution {
187 padding-top: 0.75em;
188 text-align: left;
190 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
191 div.verseblock + div.attribution {
192 text-align: left;
195 div.admonitionblock .icon {
196 vertical-align: top;
197 font-size: 1.1em;
198 font-weight: bold;
199 text-decoration: underline;
200 color: #527bbd;
201 padding-right: 0.5em;
203 div.admonitionblock td.content {
204 padding-left: 0.5em;
205 border-left: 3px solid #dddddd;
208 div.exampleblock > div.content {
209 border-left: 3px solid #dddddd;
210 padding-left: 0.5em;
213 div.imageblock div.content { padding-left: 0; }
214 span.image img { border-style: none; }
215 a.image:visited { color: white; }
217 dl {
218 margin-top: 0.8em;
219 margin-bottom: 0.8em;
221 dt {
222 margin-top: 0.5em;
223 margin-bottom: 0;
224 font-style: normal;
225 color: navy;
227 dd > *:first-child {
228 margin-top: 0.1em;
231 ul, ol {
232 list-style-position: outside;
234 ol.arabic {
235 list-style-type: decimal;
237 ol.loweralpha {
238 list-style-type: lower-alpha;
240 ol.upperalpha {
241 list-style-type: upper-alpha;
243 ol.lowerroman {
244 list-style-type: lower-roman;
246 ol.upperroman {
247 list-style-type: upper-roman;
250 div.compact ul, div.compact ol,
251 div.compact p, div.compact p,
252 div.compact div, div.compact div {
253 margin-top: 0.1em;
254 margin-bottom: 0.1em;
257 div.tableblock > table {
258 border: 3px solid #527bbd;
260 thead, p.table.header {
261 font-family: sans-serif;
262 font-weight: bold;
264 tfoot {
265 font-weight: bold;
267 td > div.verse {
268 white-space: pre;
270 p.table {
271 margin-top: 0;
273 /* Because the table frame attribute is overriden by CSS in most browsers. */
274 div.tableblock > table[frame="void"] {
275 border-style: none;
277 div.tableblock > table[frame="hsides"] {
278 border-left-style: none;
279 border-right-style: none;
281 div.tableblock > table[frame="vsides"] {
282 border-top-style: none;
283 border-bottom-style: none;
287 div.hdlist {
288 margin-top: 0.8em;
289 margin-bottom: 0.8em;
291 div.hdlist tr {
292 padding-bottom: 15px;
294 dt.hdlist1.strong, td.hdlist1.strong {
295 font-weight: bold;
297 td.hdlist1 {
298 vertical-align: top;
299 font-style: normal;
300 padding-right: 0.8em;
301 color: navy;
303 td.hdlist2 {
304 vertical-align: top;
306 div.hdlist.compact tr {
307 margin: 0;
308 padding-bottom: 0;
311 .comment {
312 background: yellow;
315 .footnote, .footnoteref {
316 font-size: 0.8em;
319 span.footnote, span.footnoteref {
320 vertical-align: super;
323 #footnotes {
324 margin: 20px 0 20px 0;
325 padding: 7px 0 0 0;
328 #footnotes div.footnote {
329 margin: 0 0 5px 0;
332 #footnotes hr {
333 border: none;
334 border-top: 1px solid silver;
335 height: 1px;
336 text-align: left;
337 margin-left: 0;
338 width: 20%;
339 min-width: 100px;
343 @media print {
344 div#footer-badges { display: none; }
347 div#toc {
348 margin-bottom: 2.5em;
351 div#toctitle {
352 color: #527bbd;
353 font-family: sans-serif;
354 font-size: 1.1em;
355 font-weight: bold;
356 margin-top: 1.0em;
357 margin-bottom: 0.1em;
360 div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
361 margin-top: 0;
362 margin-bottom: 0;
364 div.toclevel2 {
365 margin-left: 2em;
366 font-size: 0.9em;
368 div.toclevel3 {
369 margin-left: 4em;
370 font-size: 0.9em;
372 div.toclevel4 {
373 margin-left: 6em;
374 font-size: 0.9em;
376 /* Overrides for manpage documents */
377 h1 {
378 padding-top: 0.5em;
379 padding-bottom: 0.5em;
380 border-top: 2px solid silver;
381 border-bottom: 2px solid silver;
383 h2 {
384 border-style: none;
386 div.sectionbody {
387 margin-left: 5%;
390 @media print {
391 div#toc { display: none; }
394 /* Workarounds for IE6's broken and incomplete CSS2. */
396 div.sidebar-content {
397 background: #ffffee;
398 border: 1px solid silver;
399 padding: 0.5em;
401 div.sidebar-title, div.image-title {
402 color: #527bbd;
403 font-family: sans-serif;
404 font-weight: bold;
405 margin-top: 0.0em;
406 margin-bottom: 0.5em;
409 div.listingblock div.content {
410 border: 1px solid silver;
411 background: #f4f4f4;
412 padding: 0.5em;
415 div.quoteblock-attribution {
416 padding-top: 0.5em;
417 text-align: right;
420 div.verseblock-content {
421 white-space: pre;
423 div.verseblock-attribution {
424 padding-top: 0.75em;
425 text-align: left;
428 div.exampleblock-content {
429 border-left: 3px solid #dddddd;
430 padding-left: 0.5em;
433 /* IE6 sets dynamically generated links as visited. */
434 div#toc a:visited { color: blue; }
435 </style>
436 <script type="text/javascript">
437 /*<![CDATA[*/
438 window.onload = function(){asciidoc.footnotes();}
439 var asciidoc = { // Namespace.
441 /////////////////////////////////////////////////////////////////////
442 // Table Of Contents generator
443 /////////////////////////////////////////////////////////////////////
445 /* Author: Mihai Bazon, September 2002
446 * http://students.infoiasi.ro/~mishoo
448 * Table Of Content generator
449 * Version: 0.4
451 * Feel free to use this script under the terms of the GNU General Public
452 * License, as long as you do not remove or alter this notice.
455 /* modified by Troy D. Hanson, September 2006. License: GPL */
456 /* modified by Stuart Rackham, 2006, 2009. License: GPL */
458 // toclevels = 1..4.
459 toc: function (toclevels) {
461 function getText(el) {
462 var text = "";
463 for (var i = el.firstChild; i != null; i = i.nextSibling) {
464 if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
465 text += i.data;
466 else if (i.firstChild != null)
467 text += getText(i);
469 return text;
472 function TocEntry(el, text, toclevel) {
473 this.element = el;
474 this.text = text;
475 this.toclevel = toclevel;
478 function tocEntries(el, toclevels) {
479 var result = new Array;
480 var re = new RegExp('[hH]([2-'+(toclevels+1)+'])');
481 // Function that scans the DOM tree for header elements (the DOM2
482 // nodeIterator API would be a better technique but not supported by all
483 // browsers).
484 var iterate = function (el) {
485 for (var i = el.firstChild; i != null; i = i.nextSibling) {
486 if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
487 var mo = re.exec(i.tagName);
488 if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
489 result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
491 iterate(i);
495 iterate(el);
496 return result;
499 var toc = document.getElementById("toc");
500 var entries = tocEntries(document.getElementById("content"), toclevels);
501 for (var i = 0; i < entries.length; ++i) {
502 var entry = entries[i];
503 if (entry.element.id == "")
504 entry.element.id = "_toc_" + i;
505 var a = document.createElement("a");
506 a.href = "#" + entry.element.id;
507 a.appendChild(document.createTextNode(entry.text));
508 var div = document.createElement("div");
509 div.appendChild(a);
510 div.className = "toclevel" + entry.toclevel;
511 toc.appendChild(div);
513 if (entries.length == 0)
514 toc.parentNode.removeChild(toc);
518 /////////////////////////////////////////////////////////////////////
519 // Footnotes generator
520 /////////////////////////////////////////////////////////////////////
522 /* Based on footnote generation code from:
523 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
526 footnotes: function () {
527 var cont = document.getElementById("content");
528 var noteholder = document.getElementById("footnotes");
529 var spans = cont.getElementsByTagName("span");
530 var refs = {};
531 var n = 0;
532 for (i=0; i<spans.length; i++) {
533 if (spans[i].className == "footnote") {
534 n++;
535 // Use [\s\S] in place of . so multi-line matches work.
536 // Because JavaScript has no s (dotall) regex flag.
537 note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
538 noteholder.innerHTML +=
539 "<div class='footnote' id='_footnote_" + n + "'>" +
540 "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
541 n + "</a>. " + note + "</div>";
542 spans[i].innerHTML =
543 "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
544 "' title='View footnote' class='footnote'>" + n + "</a>]";
545 var id =spans[i].getAttribute("id");
546 if (id != null) refs["#"+id] = n;
549 if (n == 0)
550 noteholder.parentNode.removeChild(noteholder);
551 else {
552 // Process footnoterefs.
553 for (i=0; i<spans.length; i++) {
554 if (spans[i].className == "footnoteref") {
555 var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
556 href = href.match(/#.*/)[0]; // Because IE return full URL.
557 n = refs[href];
558 spans[i].innerHTML =
559 "[<a href='#_footnote_" + n +
560 "' title='View footnote' class='footnote'>" + n + "</a>]";
567 /*]]>*/
568 </script>
569 </head>
570 <body>
571 <div id="header">
572 <h1>
573 git-checkout(1) Manual Page
574 </h1>
575 <h2>NAME</h2>
576 <div class="sectionbody">
577 <p>git-checkout -
578 Checkout a branch or paths to the working tree
579 </p>
580 </div>
581 </div>
582 <div id="content">
583 <h2 id="_synopsis">SYNOPSIS</h2>
584 <div class="sectionbody">
585 <div class="verseblock">
586 <div class="verseblock-content"><em>git checkout</em> [-q] [-f] [-m] [&lt;branch&gt;]
587 <em>git checkout</em> [-q] [-f] [-m] [--detach] [&lt;commit&gt;]
588 <em>git checkout</em> [-q] [-f] [-m] [[-b|-B|--orphan] &lt;new_branch&gt;] [&lt;start_point&gt;]
589 <em>git checkout</em> [-f|--ours|--theirs|-m|--conflict=&lt;style&gt;] [&lt;tree-ish&gt;] [--] &lt;paths&gt;&#8230;
590 <em>git checkout</em> [-p|--patch] [&lt;tree-ish&gt;] [--] [&lt;paths&gt;&#8230;]</div>
591 <div class="verseblock-attribution">
592 </div></div>
593 </div>
594 <h2 id="_description">DESCRIPTION</h2>
595 <div class="sectionbody">
596 <div class="paragraph"><p>Updates files in the working tree to match the version in the index
597 or the specified tree. If no paths are given, <em>git checkout</em> will
598 also update <tt>HEAD</tt> to set the specified branch as the current
599 branch.</p></div>
600 <div class="dlist"><dl>
601 <dt class="hdlist1">
602 <em>git checkout</em> [&lt;branch&gt;]
603 </dt>
604 <dt class="hdlist1">
605 <em>git checkout</em> -b|-B &lt;new_branch&gt; [&lt;start point&gt;]
606 </dt>
607 <dt class="hdlist1">
608 <em>git checkout</em> [--detach] [&lt;commit&gt;]
609 </dt>
610 <dd>
612 This form switches branches by updating the index, working
613 tree, and HEAD to reflect the specified branch or commit.
614 </p>
615 <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>
616 were called and then checked out; in this case you can
617 use the <tt>--track</tt> or <tt>--no-track</tt> options, which will be passed to
618 <em>git branch</em>. As a convenience, <tt>--track</tt> without <tt>-b</tt> implies branch
619 creation; see the description of <tt>--track</tt> below.</p></div>
620 <div class="paragraph"><p>If <tt>-B</tt> is given, &lt;new_branch&gt; is created if it doesn&#8217;t exist; otherwise, it
621 is reset. This is the transactional equivalent of</p></div>
622 <div class="listingblock">
623 <div class="content">
624 <pre><tt>$ git branch -f &lt;branch&gt; [&lt;start point&gt;]
625 $ git checkout &lt;branch&gt;</tt></pre>
626 </div></div>
627 <div class="paragraph"><p>that is to say, the branch is not reset/created unless "git checkout" is
628 successful.</p></div>
629 </dd>
630 <dt class="hdlist1">
631 <em>git checkout</em> [-p|--patch] [&lt;tree-ish&gt;] [--] &lt;pathspec&gt;&#8230;
632 </dt>
633 <dd>
635 When &lt;paths&gt; or <tt>--patch</tt> are given, <em>git checkout</em> does <strong>not</strong>
636 switch branches. It updates the named paths in the working tree
637 from the index file or from a named &lt;tree-ish&gt; (most often a
638 commit). In this case, the <tt>-b</tt> and <tt>--track</tt> options are
639 meaningless and giving either of them results in an error. The
640 &lt;tree-ish&gt; argument can be used to specify a specific tree-ish
641 (i.e. commit, tag or tree) to update the index for the given
642 paths before updating the working tree.
643 </p>
644 <div class="paragraph"><p>The index may contain unmerged entries because of a previous failed merge.
645 By default, if you try to check out such an entry from the index, the
646 checkout operation will fail and nothing will be checked out.
647 Using <tt>-f</tt> will ignore these unmerged entries. The contents from a
648 specific side of the merge can be checked out of the index by
649 using <tt>--ours</tt> or <tt>--theirs</tt>. With <tt>-m</tt>, changes made to the working tree
650 file can be discarded to re-create the original conflicted merge result.</p></div>
651 </dd>
652 </dl></div>
653 </div>
654 <h2 id="_options">OPTIONS</h2>
655 <div class="sectionbody">
656 <div class="dlist"><dl>
657 <dt class="hdlist1">
659 </dt>
660 <dt class="hdlist1">
661 --quiet
662 </dt>
663 <dd>
665 Quiet, suppress feedback messages.
666 </p>
667 </dd>
668 <dt class="hdlist1">
670 </dt>
671 <dt class="hdlist1">
672 --force
673 </dt>
674 <dd>
676 When switching branches, proceed even if the index or the
677 working tree differs from HEAD. This is used to throw away
678 local changes.
679 </p>
680 <div class="paragraph"><p>When checking out paths from the index, do not fail upon unmerged
681 entries; instead, unmerged entries are ignored.</p></div>
682 </dd>
683 <dt class="hdlist1">
684 --ours
685 </dt>
686 <dt class="hdlist1">
687 --theirs
688 </dt>
689 <dd>
691 When checking out paths from the index, check out stage #2
692 (<em>ours</em>) or #3 (<em>theirs</em>) for unmerged paths.
693 </p>
694 </dd>
695 <dt class="hdlist1">
697 </dt>
698 <dd>
700 Create a new branch named &lt;new_branch&gt; and start it at
701 &lt;start_point&gt;; see <a href="git-branch.html">git-branch(1)</a> for details.
702 </p>
703 </dd>
704 <dt class="hdlist1">
706 </dt>
707 <dd>
709 Creates the branch &lt;new_branch&gt; and start it at &lt;start_point&gt;;
710 if it already exists, then reset it to &lt;start_point&gt;. This is
711 equivalent to running "git branch" with "-f"; see
712 <a href="git-branch.html">git-branch(1)</a> for details.
713 </p>
714 </dd>
715 <dt class="hdlist1">
717 </dt>
718 <dt class="hdlist1">
719 --track
720 </dt>
721 <dd>
723 When creating a new branch, set up "upstream" configuration. See
724 "--track" in <a href="git-branch.html">git-branch(1)</a> for details.
725 </p>
726 <div class="paragraph"><p>If no <em>-b</em> option is given, the name of the new branch will be
727 derived from the remote-tracking branch. If "remotes/" or "refs/remotes/"
728 is prefixed it is stripped away, and then the part up to the
729 next slash (which would be the nickname of the remote) is removed.
730 This would tell us to use "hack" as the local branch when branching
731 off of "origin/hack" (or "remotes/origin/hack", or even
732 "refs/remotes/origin/hack"). If the given name has no slash, or the above
733 guessing results in an empty name, the guessing is aborted. You can
734 explicitly give a name with <em>-b</em> in such a case.</p></div>
735 </dd>
736 <dt class="hdlist1">
737 --no-track
738 </dt>
739 <dd>
741 Do not set up "upstream" configuration, even if the
742 branch.autosetupmerge configuration variable is true.
743 </p>
744 </dd>
745 <dt class="hdlist1">
747 </dt>
748 <dd>
750 Create the new branch&#8217;s reflog; see <a href="git-branch.html">git-branch(1)</a> for
751 details.
752 </p>
753 </dd>
754 <dt class="hdlist1">
755 --detach
756 </dt>
757 <dd>
759 Rather than checking out a branch to work on it, check out a
760 commit for inspection and discardable experiments.
761 This is the default behavior of "git checkout &lt;commit&gt;" when
762 &lt;commit&gt; is not a branch name. See the "DETACHED HEAD" section
763 below for details.
764 </p>
765 </dd>
766 <dt class="hdlist1">
767 --orphan
768 </dt>
769 <dd>
771 Create a new <em>orphan</em> branch, named &lt;new_branch&gt;, started from
772 &lt;start_point&gt; and switch to it. The first commit made on this
773 new branch will have no parents and it will be the root of a new
774 history totally disconnected from all the other branches and
775 commits.
776 </p>
777 <div class="paragraph"><p>The index and the working tree are adjusted as if you had previously run
778 "git checkout &lt;start_point&gt;". This allows you to start a new history
779 that records a set of paths similar to &lt;start_point&gt; by easily running
780 "git commit -a" to make the root commit.</p></div>
781 <div class="paragraph"><p>This can be useful when you want to publish the tree from a commit
782 without exposing its full history. You might want to do this to publish
783 an open source branch of a project whose current tree is "clean", but
784 whose full history contains proprietary or otherwise encumbered bits of
785 code.</p></div>
786 <div class="paragraph"><p>If you want to start a disconnected history that records a set of paths
787 that is totally different from the one of &lt;start_point&gt;, then you should
788 clear the index and the working tree right after creating the orphan
789 branch by running "git rm -rf ." from the top level of the working tree.
790 Afterwards you will be ready to prepare your new files, repopulating the
791 working tree, by copying them from elsewhere, extracting a tarball, etc.</p></div>
792 </dd>
793 <dt class="hdlist1">
795 </dt>
796 <dt class="hdlist1">
797 --merge
798 </dt>
799 <dd>
801 When switching branches,
802 if you have local modifications to one or more files that
803 are different between the current branch and the branch to
804 which you are switching, the command refuses to switch
805 branches in order to preserve your modifications in context.
806 However, with this option, a three-way merge between the current
807 branch, your working tree contents, and the new branch
808 is done, and you will be on the new branch.
809 </p>
810 <div class="paragraph"><p>When a merge conflict happens, the index entries for conflicting
811 paths are left unmerged, and you need to resolve the conflicts
812 and mark the resolved paths with <tt>git add</tt> (or <tt>git rm</tt> if the merge
813 should result in deletion of the path).</p></div>
814 <div class="paragraph"><p>When checking out paths from the index, this option lets you recreate
815 the conflicted merge in the specified paths.</p></div>
816 </dd>
817 <dt class="hdlist1">
818 --conflict=&lt;style&gt;
819 </dt>
820 <dd>
822 The same as --merge option above, but changes the way the
823 conflicting hunks are presented, overriding the
824 merge.conflictstyle configuration variable. Possible values are
825 "merge" (default) and "diff3" (in addition to what is shown by
826 "merge" style, shows the original contents).
827 </p>
828 </dd>
829 <dt class="hdlist1">
831 </dt>
832 <dt class="hdlist1">
833 --patch
834 </dt>
835 <dd>
837 Interactively select hunks in the difference between the
838 &lt;tree-ish&gt; (or the index, if unspecified) and the working
839 tree. The chosen hunks are then applied in reverse to the
840 working tree (and if a &lt;tree-ish&gt; was specified, the index).
841 </p>
842 <div class="paragraph"><p>This means that you can use <tt>git checkout -p</tt> to selectively discard
843 edits from your current working tree. See the &#8220;Interactive Mode&#8221;
844 section of <a href="git-add.html">git-add(1)</a> to learn how to operate the <tt>--patch</tt> mode.</p></div>
845 </dd>
846 <dt class="hdlist1">
847 &lt;branch&gt;
848 </dt>
849 <dd>
851 Branch to checkout; if it refers to a branch (i.e., a name that,
852 when prepended with "refs/heads/", is a valid ref), then that
853 branch is checked out. Otherwise, if it refers to a valid
854 commit, your HEAD becomes "detached" and you are no longer on
855 any branch (see below for details).
856 </p>
857 <div class="paragraph"><p>As a special case, the <tt>"@{-N}"</tt> syntax for the N-th last branch
858 checks out the branch (instead of detaching). You may also specify
859 <tt>-</tt> which is synonymous with <tt>"@{-1}"</tt>.</p></div>
860 <div class="paragraph"><p>As a further special case, you may use <tt>"A...B"</tt> as a shortcut for the
861 merge base of <tt>A</tt> and <tt>B</tt> if there is exactly one merge base. You can
862 leave out at most one of <tt>A</tt> and <tt>B</tt>, in which case it defaults to <tt>HEAD</tt>.</p></div>
863 </dd>
864 <dt class="hdlist1">
865 &lt;new_branch&gt;
866 </dt>
867 <dd>
869 Name for the new branch.
870 </p>
871 </dd>
872 <dt class="hdlist1">
873 &lt;start_point&gt;
874 </dt>
875 <dd>
877 The name of a commit at which to start the new branch; see
878 <a href="git-branch.html">git-branch(1)</a> for details. Defaults to HEAD.
879 </p>
880 </dd>
881 <dt class="hdlist1">
882 &lt;tree-ish&gt;
883 </dt>
884 <dd>
886 Tree to checkout from (when paths are given). If not specified,
887 the index will be used.
888 </p>
889 </dd>
890 </dl></div>
891 </div>
892 <h2 id="_detached_head">DETACHED HEAD</h2>
893 <div class="sectionbody">
894 <div class="paragraph"><p>HEAD normally refers to a named branch (e.g. <em>master</em>). Meanwhile, each
895 branch refers to a specific commit. Let&#8217;s look at a repo with three
896 commits, one of them tagged, and with branch <em>master</em> checked out:</p></div>
897 <div class="listingblock">
898 <div class="content">
899 <pre><tt> HEAD (refers to branch 'master')
902 a---b---c branch 'master' (refers to commit 'c')
905 tag 'v2.0' (refers to commit 'b')</tt></pre>
906 </div></div>
907 <div class="paragraph"><p>When a commit is created in this state, the branch is updated to refer to
908 the new commit. Specifically, <em>git commit</em> creates a new commit <em>d</em>, whose
909 parent is commit <em>c</em>, and then updates branch <em>master</em> to refer to new
910 commit <em>d</em>. HEAD still refers to branch <em>master</em> and so indirectly now refers
911 to commit <em>d</em>:</p></div>
912 <div class="listingblock">
913 <div class="content">
914 <pre><tt>$ edit; git add; git commit
916 HEAD (refers to branch 'master')
919 a---b---c---d branch 'master' (refers to commit 'd')
922 tag 'v2.0' (refers to commit 'b')</tt></pre>
923 </div></div>
924 <div class="paragraph"><p>It is sometimes useful to be able to checkout a commit that is not at
925 the tip of any named branch, or even to create a new commit that is not
926 referenced by a named branch. Let&#8217;s look at what happens when we
927 checkout commit <em>b</em> (here we show two ways this may be done):</p></div>
928 <div class="listingblock">
929 <div class="content">
930 <pre><tt>$ git checkout v2.0 # or
931 $ git checkout master^^
933 HEAD (refers to commit 'b')
936 a---b---c---d branch 'master' (refers to commit 'd')
939 tag 'v2.0' (refers to commit 'b')</tt></pre>
940 </div></div>
941 <div class="paragraph"><p>Notice that regardless of which checkout command we use, HEAD now refers
942 directly to commit <em>b</em>. This is known as being in detached HEAD state.
943 It means simply that HEAD refers to a specific commit, as opposed to
944 referring to a named branch. Let&#8217;s see what happens when we create a commit:</p></div>
945 <div class="listingblock">
946 <div class="content">
947 <pre><tt>$ edit; git add; git commit
949 HEAD (refers to commit 'e')
954 a---b---c---d branch 'master' (refers to commit 'd')
957 tag 'v2.0' (refers to commit 'b')</tt></pre>
958 </div></div>
959 <div class="paragraph"><p>There is now a new commit <em>e</em>, but it is referenced only by HEAD. We can
960 of course add yet another commit in this state:</p></div>
961 <div class="listingblock">
962 <div class="content">
963 <pre><tt>$ edit; git add; git commit
965 HEAD (refers to commit 'f')
968 e---f
970 a---b---c---d branch 'master' (refers to commit 'd')
973 tag 'v2.0' (refers to commit 'b')</tt></pre>
974 </div></div>
975 <div class="paragraph"><p>In fact, we can perform all the normal git operations. But, let&#8217;s look
976 at what happens when we then checkout master:</p></div>
977 <div class="listingblock">
978 <div class="content">
979 <pre><tt>$ git checkout master
981 HEAD (refers to branch 'master')
982 e---f |
984 a---b---c---d branch 'master' (refers to commit 'd')
987 tag 'v2.0' (refers to commit 'b')</tt></pre>
988 </div></div>
989 <div class="paragraph"><p>It is important to realize that at this point nothing refers to commit
990 <em>f</em>. Eventually commit <em>f</em> (and by extension commit <em>e</em>) will be deleted
991 by the routine git garbage collection process, unless we create a reference
992 before that happens. If we have not yet moved away from commit <em>f</em>,
993 any of these will create a reference to it:</p></div>
994 <div class="listingblock">
995 <div class="content">
996 <pre><tt>$ git checkout -b foo <b>&lt;1&gt;</b>
997 $ git branch foo <b>&lt;2&gt;</b>
998 $ git tag foo <b>&lt;3&gt;</b></tt></pre>
999 </div></div>
1000 <div class="colist arabic"><ol>
1001 <li>
1003 creates a new branch <em>foo</em>, which refers to commit <em>f</em>, and then
1004 updates HEAD to refer to branch <em>foo</em>. In other words, we&#8217;ll no longer
1005 be in detached HEAD state after this command.
1006 </p>
1007 </li>
1008 <li>
1010 similarly creates a new branch <em>foo</em>, which refers to commit <em>f</em>,
1011 but leaves HEAD detached.
1012 </p>
1013 </li>
1014 <li>
1016 creates a new tag <em>foo</em>, which refers to commit <em>f</em>,
1017 leaving HEAD detached.
1018 </p>
1019 </li>
1020 </ol></div>
1021 <div class="paragraph"><p>If we have moved away from commit <em>f</em>, then we must first recover its object
1022 name (typically by using git reflog), and then we can create a reference to
1023 it. For example, to see the last two commits to which HEAD referred, we
1024 can use either of these commands:</p></div>
1025 <div class="listingblock">
1026 <div class="content">
1027 <pre><tt>$ git reflog -2 HEAD # or
1028 $ git log -g -2 HEAD</tt></pre>
1029 </div></div>
1030 </div>
1031 <h2 id="_examples">EXAMPLES</h2>
1032 <div class="sectionbody">
1033 <div class="olist arabic"><ol class="arabic">
1034 <li>
1036 The following sequence checks out the <tt>master</tt> branch, reverts
1037 the <tt>Makefile</tt> to two revisions back, deletes hello.c by
1038 mistake, and gets it back from the index.
1039 </p>
1040 <div class="listingblock">
1041 <div class="content">
1042 <pre><tt>$ git checkout master <b>&lt;1&gt;</b>
1043 $ git checkout master~2 Makefile <b>&lt;2&gt;</b>
1044 $ rm -f hello.c
1045 $ git checkout hello.c <b>&lt;3&gt;</b></tt></pre>
1046 </div></div>
1047 <div class="colist arabic"><ol>
1048 <li>
1050 switch branch
1051 </p>
1052 </li>
1053 <li>
1055 take a file out of another commit
1056 </p>
1057 </li>
1058 <li>
1060 restore hello.c from the index
1061 </p>
1062 <div class="paragraph"><p>If you have an unfortunate branch that is named <tt>hello.c</tt>, this
1063 step would be confused as an instruction to switch to that branch.
1064 You should instead write:</p></div>
1065 <div class="listingblock">
1066 <div class="content">
1067 <pre><tt>$ git checkout -- hello.c</tt></pre>
1068 </div></div>
1069 </li>
1070 </ol></div>
1071 </li>
1072 <li>
1074 After working in the wrong branch, switching to the correct
1075 branch would be done using:
1076 </p>
1077 <div class="listingblock">
1078 <div class="content">
1079 <pre><tt>$ git checkout mytopic</tt></pre>
1080 </div></div>
1081 <div class="paragraph"><p>However, your "wrong" branch and correct "mytopic" branch may
1082 differ in files that you have modified locally, in which case
1083 the above checkout would fail like this:</p></div>
1084 <div class="listingblock">
1085 <div class="content">
1086 <pre><tt>$ git checkout mytopic
1087 error: You have local changes to 'frotz'; not switching branches.</tt></pre>
1088 </div></div>
1089 <div class="paragraph"><p>You can give the <tt>-m</tt> flag to the command, which would try a
1090 three-way merge:</p></div>
1091 <div class="listingblock">
1092 <div class="content">
1093 <pre><tt>$ git checkout -m mytopic
1094 Auto-merging frotz</tt></pre>
1095 </div></div>
1096 <div class="paragraph"><p>After this three-way merge, the local modifications are <em>not</em>
1097 registered in your index file, so <tt>git diff</tt> would show you what
1098 changes you made since the tip of the new branch.</p></div>
1099 </li>
1100 <li>
1102 When a merge conflict happens during switching branches with
1103 the <tt>-m</tt> option, you would see something like this:
1104 </p>
1105 <div class="listingblock">
1106 <div class="content">
1107 <pre><tt>$ git checkout -m mytopic
1108 Auto-merging frotz
1109 ERROR: Merge conflict in frotz
1110 fatal: merge program failed</tt></pre>
1111 </div></div>
1112 <div class="paragraph"><p>At this point, <tt>git diff</tt> shows the changes cleanly merged as in
1113 the previous example, as well as the changes in the conflicted
1114 files. Edit and resolve the conflict and mark it resolved with
1115 <tt>git add</tt> as usual:</p></div>
1116 <div class="listingblock">
1117 <div class="content">
1118 <pre><tt>$ edit frotz
1119 $ git add frotz</tt></pre>
1120 </div></div>
1121 </li>
1122 </ol></div>
1123 </div>
1124 <h2 id="_git">GIT</h2>
1125 <div class="sectionbody">
1126 <div class="paragraph"><p>Part of the <a href="git.html">git(1)</a> suite</p></div>
1127 </div>
1128 </div>
1129 <div id="footnotes"><hr /></div>
1130 <div id="footer">
1131 <div id="footer-text">
1132 Last updated 2011-09-21 23:01:14 PDT
1133 </div>
1134 </div>
1135 </body>
1136 </html>