Autogenerated HTML docs for v1.7.7-237-g34c44
[git/jnareb-git.git] / gitcore-tutorial.html
blobec665cad56c184232596d78a2e1a16b2ab1866e9
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>gitcore-tutorial(7)</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 gitcore-tutorial(7) Manual Page
574 </h1>
575 <h2>NAME</h2>
576 <div class="sectionbody">
577 <p>gitcore-tutorial -
578 A git core tutorial for developers
579 </p>
580 </div>
581 </div>
582 <div id="content">
583 <h2 id="_synopsis">SYNOPSIS</h2>
584 <div class="sectionbody">
585 <div class="paragraph"><p>git *</p></div>
586 </div>
587 <h2 id="_description">DESCRIPTION</h2>
588 <div class="sectionbody">
589 <div class="paragraph"><p>This tutorial explains how to use the "core" git commands to set up and
590 work with a git repository.</p></div>
591 <div class="paragraph"><p>If you just need to use git as a revision control system you may prefer
592 to start with "A Tutorial Introduction to GIT" (<a href="gittutorial.html">gittutorial(7)</a>) or
593 <a href="user-manual.html">the GIT User Manual</a>.</p></div>
594 <div class="paragraph"><p>However, an understanding of these low-level tools can be helpful if
595 you want to understand git&#8217;s internals.</p></div>
596 <div class="paragraph"><p>The core git is often called "plumbing", with the prettier user
597 interfaces on top of it called "porcelain". You may not want to use the
598 plumbing directly very often, but it can be good to know what the
599 plumbing does for when the porcelain isn&#8217;t flushing.</p></div>
600 <div class="paragraph"><p>Back when this document was originally written, many porcelain
601 commands were shell scripts. For simplicity, it still uses them as
602 examples to illustrate how plumbing is fit together to form the
603 porcelain commands. The source tree includes some of these scripts in
604 contrib/examples/ for reference. Although these are not implemented as
605 shell scripts anymore, the description of what the plumbing layer
606 commands do is still valid.</p></div>
607 <div class="admonitionblock">
608 <table><tr>
609 <td class="icon">
610 <div class="title">Note</div>
611 </td>
612 <td class="content">Deeper technical details are often marked as Notes, which you can
613 skip on your first reading.</td>
614 </tr></table>
615 </div>
616 </div>
617 <h2 id="_creating_a_git_repository">Creating a git repository</h2>
618 <div class="sectionbody">
619 <div class="paragraph"><p>Creating a new git repository couldn&#8217;t be easier: all git repositories start
620 out empty, and the only thing you need to do is find yourself a
621 subdirectory that you want to use as a working tree - either an empty
622 one for a totally new project, or an existing working tree that you want
623 to import into git.</p></div>
624 <div class="paragraph"><p>For our first example, we&#8217;re going to start a totally new repository from
625 scratch, with no pre-existing files, and we&#8217;ll call it <em>git-tutorial</em>.
626 To start up, create a subdirectory for it, change into that
627 subdirectory, and initialize the git infrastructure with <em>git init</em>:</p></div>
628 <div class="listingblock">
629 <div class="content">
630 <pre><tt>$ mkdir git-tutorial
631 $ cd git-tutorial
632 $ git init</tt></pre>
633 </div></div>
634 <div class="paragraph"><p>to which git will reply</p></div>
635 <div class="listingblock">
636 <div class="content">
637 <pre><tt>Initialized empty Git repository in .git/</tt></pre>
638 </div></div>
639 <div class="paragraph"><p>which is just git&#8217;s way of saying that you haven&#8217;t been doing anything
640 strange, and that it will have created a local <tt>.git</tt> directory setup for
641 your new project. You will now have a <tt>.git</tt> directory, and you can
642 inspect that with <em>ls</em>. For your new empty project, it should show you
643 three entries, among other things:</p></div>
644 <div class="ulist"><ul>
645 <li>
647 a file called <tt>HEAD</tt>, that has <tt>ref: refs/heads/master</tt> in it.
648 This is similar to a symbolic link and points at
649 <tt>refs/heads/master</tt> relative to the <tt>HEAD</tt> file.
650 </p>
651 <div class="paragraph"><p>Don&#8217;t worry about the fact that the file that the <tt>HEAD</tt> link points to
652 doesn&#8217;t even exist yet&#8201;&#8212;&#8201;you haven&#8217;t created the commit that will
653 start your <tt>HEAD</tt> development branch yet.</p></div>
654 </li>
655 <li>
657 a subdirectory called <tt>objects</tt>, which will contain all the
658 objects of your project. You should never have any real reason to
659 look at the objects directly, but you might want to know that these
660 objects are what contains all the real <em>data</em> in your repository.
661 </p>
662 </li>
663 <li>
665 a subdirectory called <tt>refs</tt>, which contains references to objects.
666 </p>
667 </li>
668 </ul></div>
669 <div class="paragraph"><p>In particular, the <tt>refs</tt> subdirectory will contain two other
670 subdirectories, named <tt>heads</tt> and <tt>tags</tt> respectively. They do
671 exactly what their names imply: they contain references to any number
672 of different <em>heads</em> of development (aka <em>branches</em>), and to any
673 <em>tags</em> that you have created to name specific versions in your
674 repository.</p></div>
675 <div class="paragraph"><p>One note: the special <tt>master</tt> head is the default branch, which is
676 why the <tt>.git/HEAD</tt> file was created points to it even if it
677 doesn&#8217;t yet exist. Basically, the <tt>HEAD</tt> link is supposed to always
678 point to the branch you are working on right now, and you always
679 start out expecting to work on the <tt>master</tt> branch.</p></div>
680 <div class="paragraph"><p>However, this is only a convention, and you can name your branches
681 anything you want, and don&#8217;t have to ever even <em>have</em> a <tt>master</tt>
682 branch. A number of the git tools will assume that <tt>.git/HEAD</tt> is
683 valid, though.</p></div>
684 <div class="admonitionblock">
685 <table><tr>
686 <td class="icon">
687 <div class="title">Note</div>
688 </td>
689 <td class="content">An <em>object</em> is identified by its 160-bit SHA1 hash, aka <em>object name</em>,
690 and a reference to an object is always the 40-byte hex
691 representation of that SHA1 name. The files in the <tt>refs</tt>
692 subdirectory are expected to contain these hex references
693 (usually with a final <tt>\n</tt> at the end), and you should thus
694 expect to see a number of 41-byte files containing these
695 references in these <tt>refs</tt> subdirectories when you actually start
696 populating your tree.</td>
697 </tr></table>
698 </div>
699 <div class="admonitionblock">
700 <table><tr>
701 <td class="icon">
702 <div class="title">Note</div>
703 </td>
704 <td class="content">An advanced user may want to take a look at <a href="gitrepository-layout.html">gitrepository-layout(5)</a>
705 after finishing this tutorial.</td>
706 </tr></table>
707 </div>
708 <div class="paragraph"><p>You have now created your first git repository. Of course, since it&#8217;s
709 empty, that&#8217;s not very useful, so let&#8217;s start populating it with data.</p></div>
710 </div>
711 <h2 id="_populating_a_git_repository">Populating a git repository</h2>
712 <div class="sectionbody">
713 <div class="paragraph"><p>We&#8217;ll keep this simple and stupid, so we&#8217;ll start off with populating a
714 few trivial files just to get a feel for it.</p></div>
715 <div class="paragraph"><p>Start off with just creating any random files that you want to maintain
716 in your git repository. We&#8217;ll start off with a few bad examples, just to
717 get a feel for how this works:</p></div>
718 <div class="listingblock">
719 <div class="content">
720 <pre><tt>$ echo "Hello World" &gt;hello
721 $ echo "Silly example" &gt;example</tt></pre>
722 </div></div>
723 <div class="paragraph"><p>you have now created two files in your working tree (aka <em>working directory</em>),
724 but to actually check in your hard work, you will have to go through two steps:</p></div>
725 <div class="ulist"><ul>
726 <li>
728 fill in the <em>index</em> file (aka <em>cache</em>) with the information about your
729 working tree state.
730 </p>
731 </li>
732 <li>
734 commit that index file as an object.
735 </p>
736 </li>
737 </ul></div>
738 <div class="paragraph"><p>The first step is trivial: when you want to tell git about any changes
739 to your working tree, you use the <em>git update-index</em> program. That
740 program normally just takes a list of filenames you want to update, but
741 to avoid trivial mistakes, it refuses to add new entries to the index
742 (or remove existing ones) unless you explicitly tell it that you&#8217;re
743 adding a new entry with the <tt>--add</tt> flag (or removing an entry with the
744 <tt>--remove</tt>) flag.</p></div>
745 <div class="paragraph"><p>So to populate the index with the two files you just created, you can do</p></div>
746 <div class="listingblock">
747 <div class="content">
748 <pre><tt>$ git update-index --add hello example</tt></pre>
749 </div></div>
750 <div class="paragraph"><p>and you have now told git to track those two files.</p></div>
751 <div class="paragraph"><p>In fact, as you did that, if you now look into your object directory,
752 you&#8217;ll notice that git will have added two new objects to the object
753 database. If you did exactly the steps above, you should now be able to do</p></div>
754 <div class="listingblock">
755 <div class="content">
756 <pre><tt>$ ls .git/objects/??/*</tt></pre>
757 </div></div>
758 <div class="paragraph"><p>and see two files:</p></div>
759 <div class="listingblock">
760 <div class="content">
761 <pre><tt>.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238
762 .git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962</tt></pre>
763 </div></div>
764 <div class="paragraph"><p>which correspond with the objects with names of <tt>557db&#8230;</tt> and
765 <tt>f24c7&#8230;</tt> respectively.</p></div>
766 <div class="paragraph"><p>If you want to, you can use <em>git cat-file</em> to look at those objects, but
767 you&#8217;ll have to use the object name, not the filename of the object:</p></div>
768 <div class="listingblock">
769 <div class="content">
770 <pre><tt>$ git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238</tt></pre>
771 </div></div>
772 <div class="paragraph"><p>where the <tt>-t</tt> tells <em>git cat-file</em> to tell you what the "type" of the
773 object is. git will tell you that you have a "blob" object (i.e., just a
774 regular file), and you can see the contents with</p></div>
775 <div class="listingblock">
776 <div class="content">
777 <pre><tt>$ git cat-file blob 557db03</tt></pre>
778 </div></div>
779 <div class="paragraph"><p>which will print out "Hello World". The object <tt>557db03</tt> is nothing
780 more than the contents of your file <tt>hello</tt>.</p></div>
781 <div class="admonitionblock">
782 <table><tr>
783 <td class="icon">
784 <div class="title">Note</div>
785 </td>
786 <td class="content">Don&#8217;t confuse that object with the file <tt>hello</tt> itself. The
787 object is literally just those specific <strong>contents</strong> of the file, and
788 however much you later change the contents in file <tt>hello</tt>, the object
789 we just looked at will never change. Objects are immutable.</td>
790 </tr></table>
791 </div>
792 <div class="admonitionblock">
793 <table><tr>
794 <td class="icon">
795 <div class="title">Note</div>
796 </td>
797 <td class="content">The second example demonstrates that you can
798 abbreviate the object name to only the first several
799 hexadecimal digits in most places.</td>
800 </tr></table>
801 </div>
802 <div class="paragraph"><p>Anyway, as we mentioned previously, you normally never actually take a
803 look at the objects themselves, and typing long 40-character hex
804 names is not something you&#8217;d normally want to do. The above digression
805 was just to show that <em>git update-index</em> did something magical, and
806 actually saved away the contents of your files into the git object
807 database.</p></div>
808 <div class="paragraph"><p>Updating the index did something else too: it created a <tt>.git/index</tt>
809 file. This is the index that describes your current working tree, and
810 something you should be very aware of. Again, you normally never worry
811 about the index file itself, but you should be aware of the fact that
812 you have not actually really "checked in" your files into git so far,
813 you&#8217;ve only <strong>told</strong> git about them.</p></div>
814 <div class="paragraph"><p>However, since git knows about them, you can now start using some of the
815 most basic git commands to manipulate the files or look at their status.</p></div>
816 <div class="paragraph"><p>In particular, let&#8217;s not even check in the two files into git yet, we&#8217;ll
817 start off by adding another line to <tt>hello</tt> first:</p></div>
818 <div class="listingblock">
819 <div class="content">
820 <pre><tt>$ echo "It's a new day for git" &gt;&gt;hello</tt></pre>
821 </div></div>
822 <div class="paragraph"><p>and you can now, since you told git about the previous state of <tt>hello</tt>, ask
823 git what has changed in the tree compared to your old index, using the
824 <em>git diff-files</em> command:</p></div>
825 <div class="listingblock">
826 <div class="content">
827 <pre><tt>$ git diff-files</tt></pre>
828 </div></div>
829 <div class="paragraph"><p>Oops. That wasn&#8217;t very readable. It just spit out its own internal
830 version of a <em>diff</em>, but that internal version really just tells you
831 that it has noticed that "hello" has been modified, and that the old object
832 contents it had have been replaced with something else.</p></div>
833 <div class="paragraph"><p>To make it readable, we can tell <em>git diff-files</em> to output the
834 differences as a patch, using the <tt>-p</tt> flag:</p></div>
835 <div class="listingblock">
836 <div class="content">
837 <pre><tt>$ git diff-files -p
838 diff --git a/hello b/hello
839 index 557db03..263414f 100644
840 --- a/hello
841 +++ b/hello
842 @@ -1 +1,2 @@
843 Hello World
844 +It's a new day for git</tt></pre>
845 </div></div>
846 <div class="paragraph"><p>i.e. the diff of the change we caused by adding another line to <tt>hello</tt>.</p></div>
847 <div class="paragraph"><p>In other words, <em>git diff-files</em> always shows us the difference between
848 what is recorded in the index, and what is currently in the working
849 tree. That&#8217;s very useful.</p></div>
850 <div class="paragraph"><p>A common shorthand for <tt>git diff-files -p</tt> is to just write <tt>git
851 diff</tt>, which will do the same thing.</p></div>
852 <div class="listingblock">
853 <div class="content">
854 <pre><tt>$ git diff
855 diff --git a/hello b/hello
856 index 557db03..263414f 100644
857 --- a/hello
858 +++ b/hello
859 @@ -1 +1,2 @@
860 Hello World
861 +It's a new day for git</tt></pre>
862 </div></div>
863 </div>
864 <h2 id="_committing_git_state">Committing git state</h2>
865 <div class="sectionbody">
866 <div class="paragraph"><p>Now, we want to go to the next stage in git, which is to take the files
867 that git knows about in the index, and commit them as a real tree. We do
868 that in two phases: creating a <em>tree</em> object, and committing that <em>tree</em>
869 object as a <em>commit</em> object together with an explanation of what the
870 tree was all about, along with information of how we came to that state.</p></div>
871 <div class="paragraph"><p>Creating a tree object is trivial, and is done with <em>git write-tree</em>.
872 There are no options or other input: <tt>git write-tree</tt> will take the
873 current index state, and write an object that describes that whole
874 index. In other words, we&#8217;re now tying together all the different
875 filenames with their contents (and their permissions), and we&#8217;re
876 creating the equivalent of a git "directory" object:</p></div>
877 <div class="listingblock">
878 <div class="content">
879 <pre><tt>$ git write-tree</tt></pre>
880 </div></div>
881 <div class="paragraph"><p>and this will just output the name of the resulting tree, in this case
882 (if you have done exactly as I&#8217;ve described) it should be</p></div>
883 <div class="listingblock">
884 <div class="content">
885 <pre><tt>8988da15d077d4829fc51d8544c097def6644dbb</tt></pre>
886 </div></div>
887 <div class="paragraph"><p>which is another incomprehensible object name. Again, if you want to,
888 you can use <tt>git cat-file -t 8988d&#8230;</tt> to see that this time the object
889 is not a "blob" object, but a "tree" object (you can also use
890 <tt>git cat-file</tt> to actually output the raw object contents, but you&#8217;ll see
891 mainly a binary mess, so that&#8217;s less interesting).</p></div>
892 <div class="paragraph"><p>However&#8201;&#8212;&#8201;normally you&#8217;d never use <em>git write-tree</em> on its own, because
893 normally you always commit a tree into a commit object using the
894 <em>git commit-tree</em> command. In fact, it&#8217;s easier to not actually use
895 <em>git write-tree</em> on its own at all, but to just pass its result in as an
896 argument to <em>git commit-tree</em>.</p></div>
897 <div class="paragraph"><p><em>git commit-tree</em> normally takes several arguments&#8201;&#8212;&#8201;it wants to know
898 what the <em>parent</em> of a commit was, but since this is the first commit
899 ever in this new repository, and it has no parents, we only need to pass in
900 the object name of the tree. However, <em>git commit-tree</em> also wants to get a
901 commit message on its standard input, and it will write out the resulting
902 object name for the commit to its standard output.</p></div>
903 <div class="paragraph"><p>And this is where we create the <tt>.git/refs/heads/master</tt> file
904 which is pointed at by <tt>HEAD</tt>. This file is supposed to contain
905 the reference to the top-of-tree of the master branch, and since
906 that&#8217;s exactly what <em>git commit-tree</em> spits out, we can do this
907 all with a sequence of simple shell commands:</p></div>
908 <div class="listingblock">
909 <div class="content">
910 <pre><tt>$ tree=$(git write-tree)
911 $ commit=$(echo 'Initial commit' | git commit-tree $tree)
912 $ git update-ref HEAD $commit</tt></pre>
913 </div></div>
914 <div class="paragraph"><p>In this case this creates a totally new commit that is not related to
915 anything else. Normally you do this only <strong>once</strong> for a project ever, and
916 all later commits will be parented on top of an earlier commit.</p></div>
917 <div class="paragraph"><p>Again, normally you&#8217;d never actually do this by hand. There is a
918 helpful script called <tt>git commit</tt> that will do all of this for you. So
919 you could have just written <tt>git commit</tt>
920 instead, and it would have done the above magic scripting for you.</p></div>
921 </div>
922 <h2 id="_making_a_change">Making a change</h2>
923 <div class="sectionbody">
924 <div class="paragraph"><p>Remember how we did the <em>git update-index</em> on file <tt>hello</tt> and then we
925 changed <tt>hello</tt> afterward, and could compare the new state of <tt>hello</tt> with the
926 state we saved in the index file?</p></div>
927 <div class="paragraph"><p>Further, remember how I said that <em>git write-tree</em> writes the contents
928 of the <strong>index</strong> file to the tree, and thus what we just committed was in
929 fact the <strong>original</strong> contents of the file <tt>hello</tt>, not the new ones. We did
930 that on purpose, to show the difference between the index state, and the
931 state in the working tree, and how they don&#8217;t have to match, even
932 when we commit things.</p></div>
933 <div class="paragraph"><p>As before, if we do <tt>git diff-files -p</tt> in our git-tutorial project,
934 we&#8217;ll still see the same difference we saw last time: the index file
935 hasn&#8217;t changed by the act of committing anything. However, now that we
936 have committed something, we can also learn to use a new command:
937 <em>git diff-index</em>.</p></div>
938 <div class="paragraph"><p>Unlike <em>git diff-files</em>, which showed the difference between the index
939 file and the working tree, <em>git diff-index</em> shows the differences
940 between a committed <strong>tree</strong> and either the index file or the working
941 tree. In other words, <em>git diff-index</em> wants a tree to be diffed
942 against, and before we did the commit, we couldn&#8217;t do that, because we
943 didn&#8217;t have anything to diff against.</p></div>
944 <div class="paragraph"><p>But now we can do</p></div>
945 <div class="listingblock">
946 <div class="content">
947 <pre><tt>$ git diff-index -p HEAD</tt></pre>
948 </div></div>
949 <div class="paragraph"><p>(where <tt>-p</tt> has the same meaning as it did in <em>git diff-files</em>), and it
950 will show us the same difference, but for a totally different reason.
951 Now we&#8217;re comparing the working tree not against the index file,
952 but against the tree we just wrote. It just so happens that those two
953 are obviously the same, so we get the same result.</p></div>
954 <div class="paragraph"><p>Again, because this is a common operation, you can also just shorthand
955 it with</p></div>
956 <div class="listingblock">
957 <div class="content">
958 <pre><tt>$ git diff HEAD</tt></pre>
959 </div></div>
960 <div class="paragraph"><p>which ends up doing the above for you.</p></div>
961 <div class="paragraph"><p>In other words, <em>git diff-index</em> normally compares a tree against the
962 working tree, but when given the <tt>--cached</tt> flag, it is told to
963 instead compare against just the index cache contents, and ignore the
964 current working tree state entirely. Since we just wrote the index
965 file to HEAD, doing <tt>git diff-index --cached -p HEAD</tt> should thus return
966 an empty set of differences, and that&#8217;s exactly what it does.</p></div>
967 <div class="admonitionblock">
968 <table><tr>
969 <td class="icon">
970 <div class="title">Note</div>
971 </td>
972 <td class="content">
973 <div class="paragraph"><p><em>git diff-index</em> really always uses the index for its
974 comparisons, and saying that it compares a tree against the working
975 tree is thus not strictly accurate. In particular, the list of
976 files to compare (the "meta-data") <strong>always</strong> comes from the index file,
977 regardless of whether the <tt>--cached</tt> flag is used or not. The <tt>--cached</tt>
978 flag really only determines whether the file <strong>contents</strong> to be compared
979 come from the working tree or not.</p></div>
980 <div class="paragraph"><p>This is not hard to understand, as soon as you realize that git simply
981 never knows (or cares) about files that it is not told about
982 explicitly. git will never go <strong>looking</strong> for files to compare, it
983 expects you to tell it what the files are, and that&#8217;s what the index
984 is there for.</p></div>
985 </td>
986 </tr></table>
987 </div>
988 <div class="paragraph"><p>However, our next step is to commit the <strong>change</strong> we did, and again, to
989 understand what&#8217;s going on, keep in mind the difference between "working
990 tree contents", "index file" and "committed tree". We have changes
991 in the working tree that we want to commit, and we always have to
992 work through the index file, so the first thing we need to do is to
993 update the index cache:</p></div>
994 <div class="listingblock">
995 <div class="content">
996 <pre><tt>$ git update-index hello</tt></pre>
997 </div></div>
998 <div class="paragraph"><p>(note how we didn&#8217;t need the <tt>--add</tt> flag this time, since git knew
999 about the file already).</p></div>
1000 <div class="paragraph"><p>Note what happens to the different <em>git diff-&#42;</em> versions here.
1001 After we&#8217;ve updated <tt>hello</tt> in the index, <tt>git diff-files -p</tt> now shows no
1002 differences, but <tt>git diff-index -p HEAD</tt> still <strong>does</strong> show that the
1003 current state is different from the state we committed. In fact, now
1004 <em>git diff-index</em> shows the same difference whether we use the <tt>--cached</tt>
1005 flag or not, since now the index is coherent with the working tree.</p></div>
1006 <div class="paragraph"><p>Now, since we&#8217;ve updated <tt>hello</tt> in the index, we can commit the new
1007 version. We could do it by writing the tree by hand again, and
1008 committing the tree (this time we&#8217;d have to use the <tt>-p HEAD</tt> flag to
1009 tell commit that the HEAD was the <strong>parent</strong> of the new commit, and that
1010 this wasn&#8217;t an initial commit any more), but you&#8217;ve done that once
1011 already, so let&#8217;s just use the helpful script this time:</p></div>
1012 <div class="listingblock">
1013 <div class="content">
1014 <pre><tt>$ git commit</tt></pre>
1015 </div></div>
1016 <div class="paragraph"><p>which starts an editor for you to write the commit message and tells you
1017 a bit about what you have done.</p></div>
1018 <div class="paragraph"><p>Write whatever message you want, and all the lines that start with <em>#</em>
1019 will be pruned out, and the rest will be used as the commit message for
1020 the change. If you decide you don&#8217;t want to commit anything after all at
1021 this point (you can continue to edit things and update the index), you
1022 can just leave an empty message. Otherwise <tt>git commit</tt> will commit
1023 the change for you.</p></div>
1024 <div class="paragraph"><p>You&#8217;ve now made your first real git commit. And if you&#8217;re interested in
1025 looking at what <tt>git commit</tt> really does, feel free to investigate:
1026 it&#8217;s a few very simple shell scripts to generate the helpful (?) commit
1027 message headers, and a few one-liners that actually do the
1028 commit itself (<em>git commit</em>).</p></div>
1029 </div>
1030 <h2 id="_inspecting_changes">Inspecting Changes</h2>
1031 <div class="sectionbody">
1032 <div class="paragraph"><p>While creating changes is useful, it&#8217;s even more useful if you can tell
1033 later what changed. The most useful command for this is another of the
1034 <em>diff</em> family, namely <em>git diff-tree</em>.</p></div>
1035 <div class="paragraph"><p><em>git diff-tree</em> can be given two arbitrary trees, and it will tell you the
1036 differences between them. Perhaps even more commonly, though, you can
1037 give it just a single commit object, and it will figure out the parent
1038 of that commit itself, and show the difference directly. Thus, to get
1039 the same diff that we&#8217;ve already seen several times, we can now do</p></div>
1040 <div class="listingblock">
1041 <div class="content">
1042 <pre><tt>$ git diff-tree -p HEAD</tt></pre>
1043 </div></div>
1044 <div class="paragraph"><p>(again, <tt>-p</tt> means to show the difference as a human-readable patch),
1045 and it will show what the last commit (in <tt>HEAD</tt>) actually changed.</p></div>
1046 <div class="admonitionblock">
1047 <table><tr>
1048 <td class="icon">
1049 <div class="title">Note</div>
1050 </td>
1051 <td class="content">
1052 <div class="paragraph"><p>Here is an ASCII art by Jon Loeliger that illustrates how
1053 various <em>diff-&#42;</em> commands compare things.</p></div>
1054 <div class="literalblock">
1055 <div class="content">
1056 <pre><tt> diff-tree
1057 +----+
1061 +-----------+
1062 | Object DB |
1063 | Backing |
1064 | Store |
1065 +-----------+
1068 | | diff-index --cached
1070 diff-index | V
1071 | +-----------+
1072 | | Index |
1073 | | "cache" |
1074 | +-----------+
1077 | | diff-files
1080 +-----------+
1081 | Working |
1082 | Directory |
1083 +-----------+</tt></pre>
1084 </div></div>
1085 </td>
1086 </tr></table>
1087 </div>
1088 <div class="paragraph"><p>More interestingly, you can also give <em>git diff-tree</em> the <tt>--pretty</tt> flag,
1089 which tells it to also show the commit message and author and date of the
1090 commit, and you can tell it to show a whole series of diffs.
1091 Alternatively, you can tell it to be "silent", and not show the diffs at
1092 all, but just show the actual commit message.</p></div>
1093 <div class="paragraph"><p>In fact, together with the <em>git rev-list</em> program (which generates a
1094 list of revisions), <em>git diff-tree</em> ends up being a veritable fount of
1095 changes. A trivial (but very useful) script called <em>git whatchanged</em> is
1096 included with git which does exactly this, and shows a log of recent
1097 activities.</p></div>
1098 <div class="paragraph"><p>To see the whole history of our pitiful little git-tutorial project, you
1099 can do</p></div>
1100 <div class="listingblock">
1101 <div class="content">
1102 <pre><tt>$ git log</tt></pre>
1103 </div></div>
1104 <div class="paragraph"><p>which shows just the log messages, or if we want to see the log together
1105 with the associated patches use the more complex (and much more
1106 powerful)</p></div>
1107 <div class="listingblock">
1108 <div class="content">
1109 <pre><tt>$ git whatchanged -p</tt></pre>
1110 </div></div>
1111 <div class="paragraph"><p>and you will see exactly what has changed in the repository over its
1112 short history.</p></div>
1113 <div class="admonitionblock">
1114 <table><tr>
1115 <td class="icon">
1116 <div class="title">Note</div>
1117 </td>
1118 <td class="content">When using the above two commands, the initial commit will be shown.
1119 If this is a problem because it is huge, you can hide it by setting
1120 the log.showroot configuration variable to false. Having this, you
1121 can still show it for each command just adding the <tt>--root</tt> option,
1122 which is a flag for <em>git diff-tree</em> accepted by both commands.</td>
1123 </tr></table>
1124 </div>
1125 <div class="paragraph"><p>With that, you should now be having some inkling of what git does, and
1126 can explore on your own.</p></div>
1127 <div class="admonitionblock">
1128 <table><tr>
1129 <td class="icon">
1130 <div class="title">Note</div>
1131 </td>
1132 <td class="content">Most likely, you are not directly using the core
1133 git Plumbing commands, but using Porcelain such as <em>git add</em>, &#8216;git-rm&#8217;
1134 and &#8216;git-commit&#8217;.</td>
1135 </tr></table>
1136 </div>
1137 </div>
1138 <h2 id="_tagging_a_version">Tagging a version</h2>
1139 <div class="sectionbody">
1140 <div class="paragraph"><p>In git, there are two kinds of tags, a "light" one, and an "annotated tag".</p></div>
1141 <div class="paragraph"><p>A "light" tag is technically nothing more than a branch, except we put
1142 it in the <tt>.git/refs/tags/</tt> subdirectory instead of calling it a <tt>head</tt>.
1143 So the simplest form of tag involves nothing more than</p></div>
1144 <div class="listingblock">
1145 <div class="content">
1146 <pre><tt>$ git tag my-first-tag</tt></pre>
1147 </div></div>
1148 <div class="paragraph"><p>which just writes the current <tt>HEAD</tt> into the <tt>.git/refs/tags/my-first-tag</tt>
1149 file, after which point you can then use this symbolic name for that
1150 particular state. You can, for example, do</p></div>
1151 <div class="listingblock">
1152 <div class="content">
1153 <pre><tt>$ git diff my-first-tag</tt></pre>
1154 </div></div>
1155 <div class="paragraph"><p>to diff your current state against that tag which at this point will
1156 obviously be an empty diff, but if you continue to develop and commit
1157 stuff, you can use your tag as an "anchor-point" to see what has changed
1158 since you tagged it.</p></div>
1159 <div class="paragraph"><p>An "annotated tag" is actually a real git object, and contains not only a
1160 pointer to the state you want to tag, but also a small tag name and
1161 message, along with optionally a PGP signature that says that yes,
1162 you really did
1163 that tag. You create these annotated tags with either the <tt>-a</tt> or
1164 <tt>-s</tt> flag to <em>git tag</em>:</p></div>
1165 <div class="listingblock">
1166 <div class="content">
1167 <pre><tt>$ git tag -s &lt;tagname&gt;</tt></pre>
1168 </div></div>
1169 <div class="paragraph"><p>which will sign the current <tt>HEAD</tt> (but you can also give it another
1170 argument that specifies the thing to tag, e.g., you could have tagged the
1171 current <tt>mybranch</tt> point by using <tt>git tag &lt;tagname&gt; mybranch</tt>).</p></div>
1172 <div class="paragraph"><p>You normally only do signed tags for major releases or things
1173 like that, while the light-weight tags are useful for any marking you
1174 want to do&#8201;&#8212;&#8201;any time you decide that you want to remember a certain
1175 point, just create a private tag for it, and you have a nice symbolic
1176 name for the state at that point.</p></div>
1177 </div>
1178 <h2 id="_copying_repositories">Copying repositories</h2>
1179 <div class="sectionbody">
1180 <div class="paragraph"><p>git repositories are normally totally self-sufficient and relocatable.
1181 Unlike CVS, for example, there is no separate notion of
1182 "repository" and "working tree". A git repository normally <strong>is</strong> the
1183 working tree, with the local git information hidden in the <tt>.git</tt>
1184 subdirectory. There is nothing else. What you see is what you got.</p></div>
1185 <div class="admonitionblock">
1186 <table><tr>
1187 <td class="icon">
1188 <div class="title">Note</div>
1189 </td>
1190 <td class="content">You can tell git to split the git internal information from
1191 the directory that it tracks, but we&#8217;ll ignore that for now: it&#8217;s not
1192 how normal projects work, and it&#8217;s really only meant for special uses.
1193 So the mental model of "the git information is always tied directly to
1194 the working tree that it describes" may not be technically 100%
1195 accurate, but it&#8217;s a good model for all normal use.</td>
1196 </tr></table>
1197 </div>
1198 <div class="paragraph"><p>This has two implications:</p></div>
1199 <div class="ulist"><ul>
1200 <li>
1202 if you grow bored with the tutorial repository you created (or you&#8217;ve
1203 made a mistake and want to start all over), you can just do simple
1204 </p>
1205 <div class="listingblock">
1206 <div class="content">
1207 <pre><tt>$ rm -rf git-tutorial</tt></pre>
1208 </div></div>
1209 <div class="paragraph"><p>and it will be gone. There&#8217;s no external repository, and there&#8217;s no
1210 history outside the project you created.</p></div>
1211 </li>
1212 <li>
1214 if you want to move or duplicate a git repository, you can do so. There
1215 is <em>git clone</em> command, but if all you want to do is just to
1216 create a copy of your repository (with all the full history that
1217 went along with it), you can do so with a regular
1218 <tt>cp -a git-tutorial new-git-tutorial</tt>.
1219 </p>
1220 <div class="paragraph"><p>Note that when you&#8217;ve moved or copied a git repository, your git index
1221 file (which caches various information, notably some of the "stat"
1222 information for the files involved) will likely need to be refreshed.
1223 So after you do a <tt>cp -a</tt> to create a new copy, you&#8217;ll want to do</p></div>
1224 <div class="listingblock">
1225 <div class="content">
1226 <pre><tt>$ git update-index --refresh</tt></pre>
1227 </div></div>
1228 <div class="paragraph"><p>in the new repository to make sure that the index file is up-to-date.</p></div>
1229 </li>
1230 </ul></div>
1231 <div class="paragraph"><p>Note that the second point is true even across machines. You can
1232 duplicate a remote git repository with <strong>any</strong> regular copy mechanism, be it
1233 <em>scp</em>, <em>rsync</em> or <em>wget</em>.</p></div>
1234 <div class="paragraph"><p>When copying a remote repository, you&#8217;ll want to at a minimum update the
1235 index cache when you do this, and especially with other peoples'
1236 repositories you often want to make sure that the index cache is in some
1237 known state (you don&#8217;t know <strong>what</strong> they&#8217;ve done and not yet checked in),
1238 so usually you&#8217;ll precede the <em>git update-index</em> with a</p></div>
1239 <div class="listingblock">
1240 <div class="content">
1241 <pre><tt>$ git read-tree --reset HEAD
1242 $ git update-index --refresh</tt></pre>
1243 </div></div>
1244 <div class="paragraph"><p>which will force a total index re-build from the tree pointed to by <tt>HEAD</tt>.
1245 It resets the index contents to <tt>HEAD</tt>, and then the <em>git update-index</em>
1246 makes sure to match up all index entries with the checked-out files.
1247 If the original repository had uncommitted changes in its
1248 working tree, <tt>git update-index --refresh</tt> notices them and
1249 tells you they need to be updated.</p></div>
1250 <div class="paragraph"><p>The above can also be written as simply</p></div>
1251 <div class="listingblock">
1252 <div class="content">
1253 <pre><tt>$ git reset</tt></pre>
1254 </div></div>
1255 <div class="paragraph"><p>and in fact a lot of the common git command combinations can be scripted
1256 with the <tt>git xyz</tt> interfaces. You can learn things by just looking
1257 at what the various git scripts do. For example, <tt>git reset</tt> used to be
1258 the above two lines implemented in <em>git reset</em>, but some things like
1259 <em>git status</em> and <em>git commit</em> are slightly more complex scripts around
1260 the basic git commands.</p></div>
1261 <div class="paragraph"><p>Many (most?) public remote repositories will not contain any of
1262 the checked out files or even an index file, and will <strong>only</strong> contain the
1263 actual core git files. Such a repository usually doesn&#8217;t even have the
1264 <tt>.git</tt> subdirectory, but has all the git files directly in the
1265 repository.</p></div>
1266 <div class="paragraph"><p>To create your own local live copy of such a "raw" git repository, you&#8217;d
1267 first create your own subdirectory for the project, and then copy the
1268 raw repository contents into the <tt>.git</tt> directory. For example, to
1269 create your own copy of the git repository, you&#8217;d do the following</p></div>
1270 <div class="listingblock">
1271 <div class="content">
1272 <pre><tt>$ mkdir my-git
1273 $ cd my-git
1274 $ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git</tt></pre>
1275 </div></div>
1276 <div class="paragraph"><p>followed by</p></div>
1277 <div class="listingblock">
1278 <div class="content">
1279 <pre><tt>$ git read-tree HEAD</tt></pre>
1280 </div></div>
1281 <div class="paragraph"><p>to populate the index. However, now you have populated the index, and
1282 you have all the git internal files, but you will notice that you don&#8217;t
1283 actually have any of the working tree files to work on. To get
1284 those, you&#8217;d check them out with</p></div>
1285 <div class="listingblock">
1286 <div class="content">
1287 <pre><tt>$ git checkout-index -u -a</tt></pre>
1288 </div></div>
1289 <div class="paragraph"><p>where the <tt>-u</tt> flag means that you want the checkout to keep the index
1290 up-to-date (so that you don&#8217;t have to refresh it afterward), and the
1291 <tt>-a</tt> flag means "check out all files" (if you have a stale copy or an
1292 older version of a checked out tree you may also need to add the <tt>-f</tt>
1293 flag first, to tell <em>git checkout-index</em> to <strong>force</strong> overwriting of any old
1294 files).</p></div>
1295 <div class="paragraph"><p>Again, this can all be simplified with</p></div>
1296 <div class="listingblock">
1297 <div class="content">
1298 <pre><tt>$ git clone rsync://rsync.kernel.org/pub/scm/git/git.git/ my-git
1299 $ cd my-git
1300 $ git checkout</tt></pre>
1301 </div></div>
1302 <div class="paragraph"><p>which will end up doing all of the above for you.</p></div>
1303 <div class="paragraph"><p>You have now successfully copied somebody else&#8217;s (mine) remote
1304 repository, and checked it out.</p></div>
1305 </div>
1306 <h2 id="_creating_a_new_branch">Creating a new branch</h2>
1307 <div class="sectionbody">
1308 <div class="paragraph"><p>Branches in git are really nothing more than pointers into the git
1309 object database from within the <tt>.git/refs/</tt> subdirectory, and as we
1310 already discussed, the <tt>HEAD</tt> branch is nothing but a symlink to one of
1311 these object pointers.</p></div>
1312 <div class="paragraph"><p>You can at any time create a new branch by just picking an arbitrary
1313 point in the project history, and just writing the SHA1 name of that
1314 object into a file under <tt>.git/refs/heads/</tt>. You can use any filename you
1315 want (and indeed, subdirectories), but the convention is that the
1316 "normal" branch is called <tt>master</tt>. That&#8217;s just a convention, though,
1317 and nothing enforces it.</p></div>
1318 <div class="paragraph"><p>To show that as an example, let&#8217;s go back to the git-tutorial repository we
1319 used earlier, and create a branch in it. You do that by simply just
1320 saying that you want to check out a new branch:</p></div>
1321 <div class="listingblock">
1322 <div class="content">
1323 <pre><tt>$ git checkout -b mybranch</tt></pre>
1324 </div></div>
1325 <div class="paragraph"><p>will create a new branch based at the current <tt>HEAD</tt> position, and switch
1326 to it.</p></div>
1327 <div class="admonitionblock">
1328 <table><tr>
1329 <td class="icon">
1330 <div class="title">Note</div>
1331 </td>
1332 <td class="content">
1333 <div class="paragraph"><p>If you make the decision to start your new branch at some
1334 other point in the history than the current <tt>HEAD</tt>, you can do so by
1335 just telling <em>git checkout</em> what the base of the checkout would be.
1336 In other words, if you have an earlier tag or branch, you&#8217;d just do</p></div>
1337 <div class="listingblock">
1338 <div class="content">
1339 <pre><tt>$ git checkout -b mybranch earlier-commit</tt></pre>
1340 </div></div>
1341 <div class="paragraph"><p>and it would create the new branch <tt>mybranch</tt> at the earlier commit,
1342 and check out the state at that time.</p></div>
1343 </td>
1344 </tr></table>
1345 </div>
1346 <div class="paragraph"><p>You can always just jump back to your original <tt>master</tt> branch by doing</p></div>
1347 <div class="listingblock">
1348 <div class="content">
1349 <pre><tt>$ git checkout master</tt></pre>
1350 </div></div>
1351 <div class="paragraph"><p>(or any other branch-name, for that matter) and if you forget which
1352 branch you happen to be on, a simple</p></div>
1353 <div class="listingblock">
1354 <div class="content">
1355 <pre><tt>$ cat .git/HEAD</tt></pre>
1356 </div></div>
1357 <div class="paragraph"><p>will tell you where it&#8217;s pointing. To get the list of branches
1358 you have, you can say</p></div>
1359 <div class="listingblock">
1360 <div class="content">
1361 <pre><tt>$ git branch</tt></pre>
1362 </div></div>
1363 <div class="paragraph"><p>which used to be nothing more than a simple script around <tt>ls .git/refs/heads</tt>.
1364 There will be an asterisk in front of the branch you are currently on.</p></div>
1365 <div class="paragraph"><p>Sometimes you may wish to create a new branch <em>without</em> actually
1366 checking it out and switching to it. If so, just use the command</p></div>
1367 <div class="listingblock">
1368 <div class="content">
1369 <pre><tt>$ git branch &lt;branchname&gt; [startingpoint]</tt></pre>
1370 </div></div>
1371 <div class="paragraph"><p>which will simply <em>create</em> the branch, but will not do anything further.
1372 You can then later&#8201;&#8212;&#8201;once you decide that you want to actually develop
1373 on that branch&#8201;&#8212;&#8201;switch to that branch with a regular <em>git checkout</em>
1374 with the branchname as the argument.</p></div>
1375 </div>
1376 <h2 id="_merging_two_branches">Merging two branches</h2>
1377 <div class="sectionbody">
1378 <div class="paragraph"><p>One of the ideas of having a branch is that you do some (possibly
1379 experimental) work in it, and eventually merge it back to the main
1380 branch. So assuming you created the above <tt>mybranch</tt> that started out
1381 being the same as the original <tt>master</tt> branch, let&#8217;s make sure we&#8217;re in
1382 that branch, and do some work there.</p></div>
1383 <div class="listingblock">
1384 <div class="content">
1385 <pre><tt>$ git checkout mybranch
1386 $ echo "Work, work, work" &gt;&gt;hello
1387 $ git commit -m "Some work." -i hello</tt></pre>
1388 </div></div>
1389 <div class="paragraph"><p>Here, we just added another line to <tt>hello</tt>, and we used a shorthand for
1390 doing both <tt>git update-index hello</tt> and <tt>git commit</tt> by just giving the
1391 filename directly to <tt>git commit</tt>, with an <tt>-i</tt> flag (it tells
1392 git to <em>include</em> that file in addition to what you have done to
1393 the index file so far when making the commit). The <tt>-m</tt> flag is to give the
1394 commit log message from the command line.</p></div>
1395 <div class="paragraph"><p>Now, to make it a bit more interesting, let&#8217;s assume that somebody else
1396 does some work in the original branch, and simulate that by going back
1397 to the master branch, and editing the same file differently there:</p></div>
1398 <div class="listingblock">
1399 <div class="content">
1400 <pre><tt>$ git checkout master</tt></pre>
1401 </div></div>
1402 <div class="paragraph"><p>Here, take a moment to look at the contents of <tt>hello</tt>, and notice how they
1403 don&#8217;t contain the work we just did in <tt>mybranch</tt>&#8201;&#8212;&#8201;because that work
1404 hasn&#8217;t happened in the <tt>master</tt> branch at all. Then do</p></div>
1405 <div class="listingblock">
1406 <div class="content">
1407 <pre><tt>$ echo "Play, play, play" &gt;&gt;hello
1408 $ echo "Lots of fun" &gt;&gt;example
1409 $ git commit -m "Some fun." -i hello example</tt></pre>
1410 </div></div>
1411 <div class="paragraph"><p>since the master branch is obviously in a much better mood.</p></div>
1412 <div class="paragraph"><p>Now, you&#8217;ve got two branches, and you decide that you want to merge the
1413 work done. Before we do that, let&#8217;s introduce a cool graphical tool that
1414 helps you view what&#8217;s going on:</p></div>
1415 <div class="listingblock">
1416 <div class="content">
1417 <pre><tt>$ gitk --all</tt></pre>
1418 </div></div>
1419 <div class="paragraph"><p>will show you graphically both of your branches (that&#8217;s what the <tt>--all</tt>
1420 means: normally it will just show you your current <tt>HEAD</tt>) and their
1421 histories. You can also see exactly how they came to be from a common
1422 source.</p></div>
1423 <div class="paragraph"><p>Anyway, let&#8217;s exit <em>gitk</em> (<tt>^Q</tt> or the File menu), and decide that we want
1424 to merge the work we did on the <tt>mybranch</tt> branch into the <tt>master</tt>
1425 branch (which is currently our <tt>HEAD</tt> too). To do that, there&#8217;s a nice
1426 script called <em>git merge</em>, which wants to know which branches you want
1427 to resolve and what the merge is all about:</p></div>
1428 <div class="listingblock">
1429 <div class="content">
1430 <pre><tt>$ git merge -m "Merge work in mybranch" mybranch</tt></pre>
1431 </div></div>
1432 <div class="paragraph"><p>where the first argument is going to be used as the commit message if
1433 the merge can be resolved automatically.</p></div>
1434 <div class="paragraph"><p>Now, in this case we&#8217;ve intentionally created a situation where the
1435 merge will need to be fixed up by hand, though, so git will do as much
1436 of it as it can automatically (which in this case is just merge the <tt>example</tt>
1437 file, which had no differences in the <tt>mybranch</tt> branch), and say:</p></div>
1438 <div class="listingblock">
1439 <div class="content">
1440 <pre><tt> Auto-merging hello
1441 CONFLICT (content): Merge conflict in hello
1442 Automatic merge failed; fix conflicts and then commit the result.</tt></pre>
1443 </div></div>
1444 <div class="paragraph"><p>It tells you that it did an "Automatic merge", which
1445 failed due to conflicts in <tt>hello</tt>.</p></div>
1446 <div class="paragraph"><p>Not to worry. It left the (trivial) conflict in <tt>hello</tt> in the same form you
1447 should already be well used to if you&#8217;ve ever used CVS, so let&#8217;s just
1448 open <tt>hello</tt> in our editor (whatever that may be), and fix it up somehow.
1449 I&#8217;d suggest just making it so that <tt>hello</tt> contains all four lines:</p></div>
1450 <div class="listingblock">
1451 <div class="content">
1452 <pre><tt>Hello World
1453 It's a new day for git
1454 Play, play, play
1455 Work, work, work</tt></pre>
1456 </div></div>
1457 <div class="paragraph"><p>and once you&#8217;re happy with your manual merge, just do a</p></div>
1458 <div class="listingblock">
1459 <div class="content">
1460 <pre><tt>$ git commit -i hello</tt></pre>
1461 </div></div>
1462 <div class="paragraph"><p>which will very loudly warn you that you&#8217;re now committing a merge
1463 (which is correct, so never mind), and you can write a small merge
1464 message about your adventures in <em>git merge</em>-land.</p></div>
1465 <div class="paragraph"><p>After you&#8217;re done, start up <tt>gitk --all</tt> to see graphically what the
1466 history looks like. Notice that <tt>mybranch</tt> still exists, and you can
1467 switch to it, and continue to work with it if you want to. The
1468 <tt>mybranch</tt> branch will not contain the merge, but next time you merge it
1469 from the <tt>master</tt> branch, git will know how you merged it, so you&#8217;ll not
1470 have to do <em>that</em> merge again.</p></div>
1471 <div class="paragraph"><p>Another useful tool, especially if you do not always work in X-Window
1472 environment, is <tt>git show-branch</tt>.</p></div>
1473 <div class="listingblock">
1474 <div class="content">
1475 <pre><tt>$ git show-branch --topo-order --more=1 master mybranch
1476 * [master] Merge work in mybranch
1477 ! [mybranch] Some work.
1479 - [master] Merge work in mybranch
1480 *+ [mybranch] Some work.
1481 * [master^] Some fun.</tt></pre>
1482 </div></div>
1483 <div class="paragraph"><p>The first two lines indicate that it is showing the two branches
1484 and the first line of the commit log message from their
1485 top-of-the-tree commits, you are currently on <tt>master</tt> branch
1486 (notice the asterisk <tt>&#42;</tt> character), and the first column for
1487 the later output lines is used to show commits contained in the
1488 <tt>master</tt> branch, and the second column for the <tt>mybranch</tt>
1489 branch. Three commits are shown along with their log messages.
1490 All of them have non blank characters in the first column (<tt>&#42;</tt>
1491 shows an ordinary commit on the current branch, <tt>-</tt> is a merge commit), which
1492 means they are now part of the <tt>master</tt> branch. Only the "Some
1493 work" commit has the plus <tt>+</tt> character in the second column,
1494 because <tt>mybranch</tt> has not been merged to incorporate these
1495 commits from the master branch. The string inside brackets
1496 before the commit log message is a short name you can use to
1497 name the commit. In the above example, <em>master</em> and <em>mybranch</em>
1498 are branch heads. <em>master^</em> is the first parent of <em>master</em>
1499 branch head. Please see <a href="gitrevisions.html">gitrevisions(7)</a> if you want to
1500 see more complex cases.</p></div>
1501 <div class="admonitionblock">
1502 <table><tr>
1503 <td class="icon">
1504 <div class="title">Note</div>
1505 </td>
1506 <td class="content">Without the <em>--more=1</em> option, <em>git show-branch</em> would not output the
1507 <em>[master^]</em> commit, as <em>[mybranch]</em> commit is a common ancestor of
1508 both <em>master</em> and <em>mybranch</em> tips. Please see <a href="git-show-branch.html">git-show-branch(1)</a>
1509 for details.</td>
1510 </tr></table>
1511 </div>
1512 <div class="admonitionblock">
1513 <table><tr>
1514 <td class="icon">
1515 <div class="title">Note</div>
1516 </td>
1517 <td class="content">If there were more commits on the <em>master</em> branch after the merge, the
1518 merge commit itself would not be shown by <em>git show-branch</em> by
1519 default. You would need to provide <em>--sparse</em> option to make the
1520 merge commit visible in this case.</td>
1521 </tr></table>
1522 </div>
1523 <div class="paragraph"><p>Now, let&#8217;s pretend you are the one who did all the work in
1524 <tt>mybranch</tt>, and the fruit of your hard work has finally been merged
1525 to the <tt>master</tt> branch. Let&#8217;s go back to <tt>mybranch</tt>, and run
1526 <em>git merge</em> to get the "upstream changes" back to your branch.</p></div>
1527 <div class="listingblock">
1528 <div class="content">
1529 <pre><tt>$ git checkout mybranch
1530 $ git merge -m "Merge upstream changes." master</tt></pre>
1531 </div></div>
1532 <div class="paragraph"><p>This outputs something like this (the actual commit object names
1533 would be different)</p></div>
1534 <div class="listingblock">
1535 <div class="content">
1536 <pre><tt>Updating from ae3a2da... to a80b4aa....
1537 Fast-forward (no commit created; -m option ignored)
1538 example | 1 +
1539 hello | 1 +
1540 2 files changed, 2 insertions(+), 0 deletions(-)</tt></pre>
1541 </div></div>
1542 <div class="paragraph"><p>Because your branch did not contain anything more than what had
1543 already been merged into the <tt>master</tt> branch, the merge operation did
1544 not actually do a merge. Instead, it just updated the top of
1545 the tree of your branch to that of the <tt>master</tt> branch. This is
1546 often called <em>fast-forward</em> merge.</p></div>
1547 <div class="paragraph"><p>You can run <tt>gitk --all</tt> again to see how the commit ancestry
1548 looks like, or run <em>show-branch</em>, which tells you this.</p></div>
1549 <div class="listingblock">
1550 <div class="content">
1551 <pre><tt>$ git show-branch master mybranch
1552 ! [master] Merge work in mybranch
1553 * [mybranch] Merge work in mybranch
1555 -- [master] Merge work in mybranch</tt></pre>
1556 </div></div>
1557 </div>
1558 <h2 id="_merging_external_work">Merging external work</h2>
1559 <div class="sectionbody">
1560 <div class="paragraph"><p>It&#8217;s usually much more common that you merge with somebody else than
1561 merging with your own branches, so it&#8217;s worth pointing out that git
1562 makes that very easy too, and in fact, it&#8217;s not that different from
1563 doing a <em>git merge</em>. In fact, a remote merge ends up being nothing
1564 more than "fetch the work from a remote repository into a temporary tag"
1565 followed by a <em>git merge</em>.</p></div>
1566 <div class="paragraph"><p>Fetching from a remote repository is done by, unsurprisingly,
1567 <em>git fetch</em>:</p></div>
1568 <div class="listingblock">
1569 <div class="content">
1570 <pre><tt>$ git fetch &lt;remote-repository&gt;</tt></pre>
1571 </div></div>
1572 <div class="paragraph"><p>One of the following transports can be used to name the
1573 repository to download from:</p></div>
1574 <div class="dlist"><dl>
1575 <dt class="hdlist1">
1576 Rsync
1577 </dt>
1578 <dd>
1580 <tt>rsync://remote.machine/path/to/repo.git/</tt>
1581 </p>
1582 <div class="paragraph"><p>Rsync transport is usable for both uploading and downloading,
1583 but is completely unaware of what git does, and can produce
1584 unexpected results when you download from the public repository
1585 while the repository owner is uploading into it via <tt>rsync</tt>
1586 transport. Most notably, it could update the files under
1587 <tt>refs/</tt> which holds the object name of the topmost commits
1588 before uploading the files in <tt>objects/</tt>&#8201;&#8212;&#8201;the downloader would
1589 obtain head commit object name while that object itself is still
1590 not available in the repository. For this reason, it is
1591 considered deprecated.</p></div>
1592 </dd>
1593 <dt class="hdlist1">
1595 </dt>
1596 <dd>
1598 <tt>remote.machine:/path/to/repo.git/</tt> or
1599 </p>
1600 <div class="paragraph"><p><tt>ssh://remote.machine/path/to/repo.git/</tt></p></div>
1601 <div class="paragraph"><p>This transport can be used for both uploading and downloading,
1602 and requires you to have a log-in privilege over <tt>ssh</tt> to the
1603 remote machine. It finds out the set of objects the other side
1604 lacks by exchanging the head commits both ends have and
1605 transfers (close to) minimum set of objects. It is by far the
1606 most efficient way to exchange git objects between repositories.</p></div>
1607 </dd>
1608 <dt class="hdlist1">
1609 Local directory
1610 </dt>
1611 <dd>
1613 <tt>/path/to/repo.git/</tt>
1614 </p>
1615 <div class="paragraph"><p>This transport is the same as SSH transport but uses <em>sh</em> to run
1616 both ends on the local machine instead of running other end on
1617 the remote machine via <em>ssh</em>.</p></div>
1618 </dd>
1619 <dt class="hdlist1">
1620 git Native
1621 </dt>
1622 <dd>
1624 <tt>git://remote.machine/path/to/repo.git/</tt>
1625 </p>
1626 <div class="paragraph"><p>This transport was designed for anonymous downloading. Like SSH
1627 transport, it finds out the set of objects the downstream side
1628 lacks and transfers (close to) minimum set of objects.</p></div>
1629 </dd>
1630 <dt class="hdlist1">
1631 HTTP(S)
1632 </dt>
1633 <dd>
1635 <tt>http://remote.machine/path/to/repo.git/</tt>
1636 </p>
1637 <div class="paragraph"><p>Downloader from http and https URL
1638 first obtains the topmost commit object name from the remote site
1639 by looking at the specified refname under <tt>repo.git/refs/</tt> directory,
1640 and then tries to obtain the
1641 commit object by downloading from <tt>repo.git/objects/xx/xxx&#8230;</tt>
1642 using the object name of that commit object. Then it reads the
1643 commit object to find out its parent commits and the associate
1644 tree object; it repeats this process until it gets all the
1645 necessary objects. Because of this behavior, they are
1646 sometimes also called <em>commit walkers</em>.</p></div>
1647 <div class="paragraph"><p>The <em>commit walkers</em> are sometimes also called <em>dumb
1648 transports</em>, because they do not require any git aware smart
1649 server like git Native transport does. Any stock HTTP server
1650 that does not even support directory index would suffice. But
1651 you must prepare your repository with <em>git update-server-info</em>
1652 to help dumb transport downloaders.</p></div>
1653 </dd>
1654 </dl></div>
1655 <div class="paragraph"><p>Once you fetch from the remote repository, you <tt>merge</tt> that
1656 with your current branch.</p></div>
1657 <div class="paragraph"><p>However&#8201;&#8212;&#8201;it&#8217;s such a common thing to <tt>fetch</tt> and then
1658 immediately <tt>merge</tt>, that it&#8217;s called <tt>git pull</tt>, and you can
1659 simply do</p></div>
1660 <div class="listingblock">
1661 <div class="content">
1662 <pre><tt>$ git pull &lt;remote-repository&gt;</tt></pre>
1663 </div></div>
1664 <div class="paragraph"><p>and optionally give a branch-name for the remote end as a second
1665 argument.</p></div>
1666 <div class="admonitionblock">
1667 <table><tr>
1668 <td class="icon">
1669 <div class="title">Note</div>
1670 </td>
1671 <td class="content">You could do without using any branches at all, by
1672 keeping as many local repositories as you would like to have
1673 branches, and merging between them with <em>git pull</em>, just like
1674 you merge between branches. The advantage of this approach is
1675 that it lets you keep a set of files for each <tt>branch</tt> checked
1676 out and you may find it easier to switch back and forth if you
1677 juggle multiple lines of development simultaneously. Of
1678 course, you will pay the price of more disk usage to hold
1679 multiple working trees, but disk space is cheap these days.</td>
1680 </tr></table>
1681 </div>
1682 <div class="paragraph"><p>It is likely that you will be pulling from the same remote
1683 repository from time to time. As a short hand, you can store
1684 the remote repository URL in the local repository&#8217;s config file
1685 like this:</p></div>
1686 <div class="listingblock">
1687 <div class="content">
1688 <pre><tt>$ git config remote.linus.url http://www.kernel.org/pub/scm/git/git.git/</tt></pre>
1689 </div></div>
1690 <div class="paragraph"><p>and use the "linus" keyword with <em>git pull</em> instead of the full URL.</p></div>
1691 <div class="paragraph"><p>Examples.</p></div>
1692 <div class="olist arabic"><ol class="arabic">
1693 <li>
1695 <tt>git pull linus</tt>
1696 </p>
1697 </li>
1698 <li>
1700 <tt>git pull linus tag v0.99.1</tt>
1701 </p>
1702 </li>
1703 </ol></div>
1704 <div class="paragraph"><p>the above are equivalent to:</p></div>
1705 <div class="olist arabic"><ol class="arabic">
1706 <li>
1708 <tt>git pull <a href="http://www.kernel.org/pub/scm/git/git.git/">http://www.kernel.org/pub/scm/git/git.git/</a> HEAD</tt>
1709 </p>
1710 </li>
1711 <li>
1713 <tt>git pull <a href="http://www.kernel.org/pub/scm/git/git.git/">http://www.kernel.org/pub/scm/git/git.git/</a> tag v0.99.1</tt>
1714 </p>
1715 </li>
1716 </ol></div>
1717 </div>
1718 <h2 id="_how_does_the_merge_work">How does the merge work?</h2>
1719 <div class="sectionbody">
1720 <div class="paragraph"><p>We said this tutorial shows what plumbing does to help you cope
1721 with the porcelain that isn&#8217;t flushing, but we so far did not
1722 talk about how the merge really works. If you are following
1723 this tutorial the first time, I&#8217;d suggest to skip to "Publishing
1724 your work" section and come back here later.</p></div>
1725 <div class="paragraph"><p>OK, still with me? To give us an example to look at, let&#8217;s go
1726 back to the earlier repository with "hello" and "example" file,
1727 and bring ourselves back to the pre-merge state:</p></div>
1728 <div class="listingblock">
1729 <div class="content">
1730 <pre><tt>$ git show-branch --more=2 master mybranch
1731 ! [master] Merge work in mybranch
1732 * [mybranch] Merge work in mybranch
1734 -- [master] Merge work in mybranch
1735 +* [master^2] Some work.
1736 +* [master^] Some fun.</tt></pre>
1737 </div></div>
1738 <div class="paragraph"><p>Remember, before running <em>git merge</em>, our <tt>master</tt> head was at
1739 "Some fun." commit, while our <tt>mybranch</tt> head was at "Some
1740 work." commit.</p></div>
1741 <div class="listingblock">
1742 <div class="content">
1743 <pre><tt>$ git checkout mybranch
1744 $ git reset --hard master^2
1745 $ git checkout master
1746 $ git reset --hard master^</tt></pre>
1747 </div></div>
1748 <div class="paragraph"><p>After rewinding, the commit structure should look like this:</p></div>
1749 <div class="listingblock">
1750 <div class="content">
1751 <pre><tt>$ git show-branch
1752 * [master] Some fun.
1753 ! [mybranch] Some work.
1755 * [master] Some fun.
1756 + [mybranch] Some work.
1757 *+ [master^] Initial commit</tt></pre>
1758 </div></div>
1759 <div class="paragraph"><p>Now we are ready to experiment with the merge by hand.</p></div>
1760 <div class="paragraph"><p><tt>git merge</tt> command, when merging two branches, uses 3-way merge
1761 algorithm. First, it finds the common ancestor between them.
1762 The command it uses is <em>git merge-base</em>:</p></div>
1763 <div class="listingblock">
1764 <div class="content">
1765 <pre><tt>$ mb=$(git merge-base HEAD mybranch)</tt></pre>
1766 </div></div>
1767 <div class="paragraph"><p>The command writes the commit object name of the common ancestor
1768 to the standard output, so we captured its output to a variable,
1769 because we will be using it in the next step. By the way, the common
1770 ancestor commit is the "Initial commit" commit in this case. You can
1771 tell it by:</p></div>
1772 <div class="listingblock">
1773 <div class="content">
1774 <pre><tt>$ git name-rev --name-only --tags $mb
1775 my-first-tag</tt></pre>
1776 </div></div>
1777 <div class="paragraph"><p>After finding out a common ancestor commit, the second step is
1778 this:</p></div>
1779 <div class="listingblock">
1780 <div class="content">
1781 <pre><tt>$ git read-tree -m -u $mb HEAD mybranch</tt></pre>
1782 </div></div>
1783 <div class="paragraph"><p>This is the same <em>git read-tree</em> command we have already seen,
1784 but it takes three trees, unlike previous examples. This reads
1785 the contents of each tree into different <em>stage</em> in the index
1786 file (the first tree goes to stage 1, the second to stage 2,
1787 etc.). After reading three trees into three stages, the paths
1788 that are the same in all three stages are <em>collapsed</em> into stage
1789 0. Also paths that are the same in two of three stages are
1790 collapsed into stage 0, taking the SHA1 from either stage 2 or
1791 stage 3, whichever is different from stage 1 (i.e. only one side
1792 changed from the common ancestor).</p></div>
1793 <div class="paragraph"><p>After <em>collapsing</em> operation, paths that are different in three
1794 trees are left in non-zero stages. At this point, you can
1795 inspect the index file with this command:</p></div>
1796 <div class="listingblock">
1797 <div class="content">
1798 <pre><tt>$ git ls-files --stage
1799 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1800 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1801 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1802 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello</tt></pre>
1803 </div></div>
1804 <div class="paragraph"><p>In our example of only two files, we did not have unchanged
1805 files so only <em>example</em> resulted in collapsing. But in real-life
1806 large projects, when only a small number of files change in one commit,
1807 this <em>collapsing</em> tends to trivially merge most of the paths
1808 fairly quickly, leaving only a handful of real changes in non-zero
1809 stages.</p></div>
1810 <div class="paragraph"><p>To look at only non-zero stages, use <tt>--unmerged</tt> flag:</p></div>
1811 <div class="listingblock">
1812 <div class="content">
1813 <pre><tt>$ git ls-files --unmerged
1814 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1815 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1816 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello</tt></pre>
1817 </div></div>
1818 <div class="paragraph"><p>The next step of merging is to merge these three versions of the
1819 file, using 3-way merge. This is done by giving
1820 <em>git merge-one-file</em> command as one of the arguments to
1821 <em>git merge-index</em> command:</p></div>
1822 <div class="listingblock">
1823 <div class="content">
1824 <pre><tt>$ git merge-index git-merge-one-file hello
1825 Auto-merging hello
1826 ERROR: Merge conflict in hello
1827 fatal: merge program failed</tt></pre>
1828 </div></div>
1829 <div class="paragraph"><p><em>git merge-one-file</em> script is called with parameters to
1830 describe those three versions, and is responsible to leave the
1831 merge results in the working tree.
1832 It is a fairly straightforward shell script, and
1833 eventually calls <em>merge</em> program from RCS suite to perform a
1834 file-level 3-way merge. In this case, <em>merge</em> detects
1835 conflicts, and the merge result with conflict marks is left in
1836 the working tree.. This can be seen if you run <tt>ls-files
1837 --stage</tt> again at this point:</p></div>
1838 <div class="listingblock">
1839 <div class="content">
1840 <pre><tt>$ git ls-files --stage
1841 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1842 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1843 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1844 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello</tt></pre>
1845 </div></div>
1846 <div class="paragraph"><p>This is the state of the index file and the working file after
1847 <em>git merge</em> returns control back to you, leaving the conflicting
1848 merge for you to resolve. Notice that the path <tt>hello</tt> is still
1849 unmerged, and what you see with <em>git diff</em> at this point is
1850 differences since stage 2 (i.e. your version).</p></div>
1851 </div>
1852 <h2 id="_publishing_your_work">Publishing your work</h2>
1853 <div class="sectionbody">
1854 <div class="paragraph"><p>So, we can use somebody else&#8217;s work from a remote repository, but
1855 how can <strong>you</strong> prepare a repository to let other people pull from
1856 it?</p></div>
1857 <div class="paragraph"><p>You do your real work in your working tree that has your
1858 primary repository hanging under it as its <tt>.git</tt> subdirectory.
1859 You <strong>could</strong> make that repository accessible remotely and ask
1860 people to pull from it, but in practice that is not the way
1861 things are usually done. A recommended way is to have a public
1862 repository, make it reachable by other people, and when the
1863 changes you made in your primary working tree are in good shape,
1864 update the public repository from it. This is often called
1865 <em>pushing</em>.</p></div>
1866 <div class="admonitionblock">
1867 <table><tr>
1868 <td class="icon">
1869 <div class="title">Note</div>
1870 </td>
1871 <td class="content">This public repository could further be mirrored, and that is
1872 how git repositories at <tt>kernel.org</tt> are managed.</td>
1873 </tr></table>
1874 </div>
1875 <div class="paragraph"><p>Publishing the changes from your local (private) repository to
1876 your remote (public) repository requires a write privilege on
1877 the remote machine. You need to have an SSH account there to
1878 run a single command, <em>git-receive-pack</em>.</p></div>
1879 <div class="paragraph"><p>First, you need to create an empty repository on the remote
1880 machine that will house your public repository. This empty
1881 repository will be populated and be kept up-to-date by pushing
1882 into it later. Obviously, this repository creation needs to be
1883 done only once.</p></div>
1884 <div class="admonitionblock">
1885 <table><tr>
1886 <td class="icon">
1887 <div class="title">Note</div>
1888 </td>
1889 <td class="content"><em>git push</em> uses a pair of commands,
1890 <em>git send-pack</em> on your local machine, and <em>git-receive-pack</em>
1891 on the remote machine. The communication between the two over
1892 the network internally uses an SSH connection.</td>
1893 </tr></table>
1894 </div>
1895 <div class="paragraph"><p>Your private repository&#8217;s git directory is usually <tt>.git</tt>, but
1896 your public repository is often named after the project name,
1897 i.e. <tt>&lt;project&gt;.git</tt>. Let&#8217;s create such a public repository for
1898 project <tt>my-git</tt>. After logging into the remote machine, create
1899 an empty directory:</p></div>
1900 <div class="listingblock">
1901 <div class="content">
1902 <pre><tt>$ mkdir my-git.git</tt></pre>
1903 </div></div>
1904 <div class="paragraph"><p>Then, make that directory into a git repository by running
1905 <em>git init</em>, but this time, since its name is not the usual
1906 <tt>.git</tt>, we do things slightly differently:</p></div>
1907 <div class="listingblock">
1908 <div class="content">
1909 <pre><tt>$ GIT_DIR=my-git.git git init</tt></pre>
1910 </div></div>
1911 <div class="paragraph"><p>Make sure this directory is available for others you want your
1912 changes to be pulled via the transport of your choice. Also
1913 you need to make sure that you have the <em>git-receive-pack</em>
1914 program on the <tt>$PATH</tt>.</p></div>
1915 <div class="admonitionblock">
1916 <table><tr>
1917 <td class="icon">
1918 <div class="title">Note</div>
1919 </td>
1920 <td class="content">Many installations of sshd do not invoke your shell as the login
1921 shell when you directly run programs; what this means is that if
1922 your login shell is <em>bash</em>, only <tt>.bashrc</tt> is read and not
1923 <tt>.bash_profile</tt>. As a workaround, make sure <tt>.bashrc</tt> sets up
1924 <tt>$PATH</tt> so that you can run <em>git-receive-pack</em> program.</td>
1925 </tr></table>
1926 </div>
1927 <div class="admonitionblock">
1928 <table><tr>
1929 <td class="icon">
1930 <div class="title">Note</div>
1931 </td>
1932 <td class="content">If you plan to publish this repository to be accessed over http,
1933 you should do <tt>mv my-git.git/hooks/post-update.sample
1934 my-git.git/hooks/post-update</tt> at this point.
1935 This makes sure that every time you push into this
1936 repository, <tt>git update-server-info</tt> is run.</td>
1937 </tr></table>
1938 </div>
1939 <div class="paragraph"><p>Your "public repository" is now ready to accept your changes.
1940 Come back to the machine you have your private repository. From
1941 there, run this command:</p></div>
1942 <div class="listingblock">
1943 <div class="content">
1944 <pre><tt>$ git push &lt;public-host&gt;:/path/to/my-git.git master</tt></pre>
1945 </div></div>
1946 <div class="paragraph"><p>This synchronizes your public repository to match the named
1947 branch head (i.e. <tt>master</tt> in this case) and objects reachable
1948 from them in your current repository.</p></div>
1949 <div class="paragraph"><p>As a real example, this is how I update my public git
1950 repository. Kernel.org mirror network takes care of the
1951 propagation to other publicly visible machines:</p></div>
1952 <div class="listingblock">
1953 <div class="content">
1954 <pre><tt>$ git push master.kernel.org:/pub/scm/git/git.git/</tt></pre>
1955 </div></div>
1956 </div>
1957 <h2 id="_packing_your_repository">Packing your repository</h2>
1958 <div class="sectionbody">
1959 <div class="paragraph"><p>Earlier, we saw that one file under <tt>.git/objects/??/</tt> directory
1960 is stored for each git object you create. This representation
1961 is efficient to create atomically and safely, but
1962 not so convenient to transport over the network. Since git objects are
1963 immutable once they are created, there is a way to optimize the
1964 storage by "packing them together". The command</p></div>
1965 <div class="listingblock">
1966 <div class="content">
1967 <pre><tt>$ git repack</tt></pre>
1968 </div></div>
1969 <div class="paragraph"><p>will do it for you. If you followed the tutorial examples, you
1970 would have accumulated about 17 objects in <tt>.git/objects/??/</tt>
1971 directories by now. <em>git repack</em> tells you how many objects it
1972 packed, and stores the packed file in <tt>.git/objects/pack</tt>
1973 directory.</p></div>
1974 <div class="admonitionblock">
1975 <table><tr>
1976 <td class="icon">
1977 <div class="title">Note</div>
1978 </td>
1979 <td class="content">You will see two files, <tt>pack-&#42;.pack</tt> and <tt>pack-&#42;.idx</tt>,
1980 in <tt>.git/objects/pack</tt> directory. They are closely related to
1981 each other, and if you ever copy them by hand to a different
1982 repository for whatever reason, you should make sure you copy
1983 them together. The former holds all the data from the objects
1984 in the pack, and the latter holds the index for random
1985 access.</td>
1986 </tr></table>
1987 </div>
1988 <div class="paragraph"><p>If you are paranoid, running <em>git verify-pack</em> command would
1989 detect if you have a corrupt pack, but do not worry too much.
1990 Our programs are always perfect ;-).</p></div>
1991 <div class="paragraph"><p>Once you have packed objects, you do not need to leave the
1992 unpacked objects that are contained in the pack file anymore.</p></div>
1993 <div class="listingblock">
1994 <div class="content">
1995 <pre><tt>$ git prune-packed</tt></pre>
1996 </div></div>
1997 <div class="paragraph"><p>would remove them for you.</p></div>
1998 <div class="paragraph"><p>You can try running <tt>find .git/objects -type f</tt> before and after
1999 you run <tt>git prune-packed</tt> if you are curious. Also <tt>git
2000 count-objects</tt> would tell you how many unpacked objects are in
2001 your repository and how much space they are consuming.</p></div>
2002 <div class="admonitionblock">
2003 <table><tr>
2004 <td class="icon">
2005 <div class="title">Note</div>
2006 </td>
2007 <td class="content"><tt>git pull</tt> is slightly cumbersome for HTTP transport, as a
2008 packed repository may contain relatively few objects in a
2009 relatively large pack. If you expect many HTTP pulls from your
2010 public repository you might want to repack &amp; prune often, or
2011 never.</td>
2012 </tr></table>
2013 </div>
2014 <div class="paragraph"><p>If you run <tt>git repack</tt> again at this point, it will say
2015 "Nothing new to pack.". Once you continue your development and
2016 accumulate the changes, running <tt>git repack</tt> again will create a
2017 new pack, that contains objects created since you packed your
2018 repository the last time. We recommend that you pack your project
2019 soon after the initial import (unless you are starting your
2020 project from scratch), and then run <tt>git repack</tt> every once in a
2021 while, depending on how active your project is.</p></div>
2022 <div class="paragraph"><p>When a repository is synchronized via <tt>git push</tt> and <tt>git pull</tt>
2023 objects packed in the source repository are usually stored
2024 unpacked in the destination, unless rsync transport is used.
2025 While this allows you to use different packing strategies on
2026 both ends, it also means you may need to repack both
2027 repositories every once in a while.</p></div>
2028 </div>
2029 <h2 id="_working_with_others">Working with Others</h2>
2030 <div class="sectionbody">
2031 <div class="paragraph"><p>Although git is a truly distributed system, it is often
2032 convenient to organize your project with an informal hierarchy
2033 of developers. Linux kernel development is run this way. There
2034 is a nice illustration (page 17, "Merges to Mainline") in
2035 <a href="http://www.xenotime.net/linux/mentor/linux-mentoring-2006.pdf">Randy Dunlap&#8217;s presentation</a>.</p></div>
2036 <div class="paragraph"><p>It should be stressed that this hierarchy is purely <strong>informal</strong>.
2037 There is nothing fundamental in git that enforces the "chain of
2038 patch flow" this hierarchy implies. You do not have to pull
2039 from only one remote repository.</p></div>
2040 <div class="paragraph"><p>A recommended workflow for a "project lead" goes like this:</p></div>
2041 <div class="olist arabic"><ol class="arabic">
2042 <li>
2044 Prepare your primary repository on your local machine. Your
2045 work is done there.
2046 </p>
2047 </li>
2048 <li>
2050 Prepare a public repository accessible to others.
2051 </p>
2052 <div class="paragraph"><p>If other people are pulling from your repository over dumb
2053 transport protocols (HTTP), you need to keep this repository
2054 <em>dumb transport friendly</em>. After <tt>git init</tt>,
2055 <tt>$GIT_DIR/hooks/post-update.sample</tt> copied from the standard templates
2056 would contain a call to <em>git update-server-info</em>
2057 but you need to manually enable the hook with
2058 <tt>mv post-update.sample post-update</tt>. This makes sure
2059 <em>git update-server-info</em> keeps the necessary files up-to-date.</p></div>
2060 </li>
2061 <li>
2063 Push into the public repository from your primary
2064 repository.
2065 </p>
2066 </li>
2067 <li>
2069 <em>git repack</em> the public repository. This establishes a big
2070 pack that contains the initial set of objects as the
2071 baseline, and possibly <em>git prune</em> if the transport
2072 used for pulling from your repository supports packed
2073 repositories.
2074 </p>
2075 </li>
2076 <li>
2078 Keep working in your primary repository. Your changes
2079 include modifications of your own, patches you receive via
2080 e-mails, and merges resulting from pulling the "public"
2081 repositories of your "subsystem maintainers".
2082 </p>
2083 <div class="paragraph"><p>You can repack this private repository whenever you feel like.</p></div>
2084 </li>
2085 <li>
2087 Push your changes to the public repository, and announce it
2088 to the public.
2089 </p>
2090 </li>
2091 <li>
2093 Every once in a while, <em>git repack</em> the public repository.
2094 Go back to step 5. and continue working.
2095 </p>
2096 </li>
2097 </ol></div>
2098 <div class="paragraph"><p>A recommended work cycle for a "subsystem maintainer" who works
2099 on that project and has an own "public repository" goes like this:</p></div>
2100 <div class="olist arabic"><ol class="arabic">
2101 <li>
2103 Prepare your work repository, by <em>git clone</em> the public
2104 repository of the "project lead". The URL used for the
2105 initial cloning is stored in the remote.origin.url
2106 configuration variable.
2107 </p>
2108 </li>
2109 <li>
2111 Prepare a public repository accessible to others, just like
2112 the "project lead" person does.
2113 </p>
2114 </li>
2115 <li>
2117 Copy over the packed files from "project lead" public
2118 repository to your public repository, unless the "project
2119 lead" repository lives on the same machine as yours. In the
2120 latter case, you can use <tt>objects/info/alternates</tt> file to
2121 point at the repository you are borrowing from.
2122 </p>
2123 </li>
2124 <li>
2126 Push into the public repository from your primary
2127 repository. Run <em>git repack</em>, and possibly <em>git prune</em> if the
2128 transport used for pulling from your repository supports
2129 packed repositories.
2130 </p>
2131 </li>
2132 <li>
2134 Keep working in your primary repository. Your changes
2135 include modifications of your own, patches you receive via
2136 e-mails, and merges resulting from pulling the "public"
2137 repositories of your "project lead" and possibly your
2138 "sub-subsystem maintainers".
2139 </p>
2140 <div class="paragraph"><p>You can repack this private repository whenever you feel
2141 like.</p></div>
2142 </li>
2143 <li>
2145 Push your changes to your public repository, and ask your
2146 "project lead" and possibly your "sub-subsystem
2147 maintainers" to pull from it.
2148 </p>
2149 </li>
2150 <li>
2152 Every once in a while, <em>git repack</em> the public repository.
2153 Go back to step 5. and continue working.
2154 </p>
2155 </li>
2156 </ol></div>
2157 <div class="paragraph"><p>A recommended work cycle for an "individual developer" who does
2158 not have a "public" repository is somewhat different. It goes
2159 like this:</p></div>
2160 <div class="olist arabic"><ol class="arabic">
2161 <li>
2163 Prepare your work repository, by <em>git clone</em> the public
2164 repository of the "project lead" (or a "subsystem
2165 maintainer", if you work on a subsystem). The URL used for
2166 the initial cloning is stored in the remote.origin.url
2167 configuration variable.
2168 </p>
2169 </li>
2170 <li>
2172 Do your work in your repository on <em>master</em> branch.
2173 </p>
2174 </li>
2175 <li>
2177 Run <tt>git fetch origin</tt> from the public repository of your
2178 upstream every once in a while. This does only the first
2179 half of <tt>git pull</tt> but does not merge. The head of the
2180 public repository is stored in <tt>.git/refs/remotes/origin/master</tt>.
2181 </p>
2182 </li>
2183 <li>
2185 Use <tt>git cherry origin</tt> to see which ones of your patches
2186 were accepted, and/or use <tt>git rebase origin</tt> to port your
2187 unmerged changes forward to the updated upstream.
2188 </p>
2189 </li>
2190 <li>
2192 Use <tt>git format-patch origin</tt> to prepare patches for e-mail
2193 submission to your upstream and send it out. Go back to
2194 step 2. and continue.
2195 </p>
2196 </li>
2197 </ol></div>
2198 </div>
2199 <h2 id="_working_with_others_shared_repository_style">Working with Others, Shared Repository Style</h2>
2200 <div class="sectionbody">
2201 <div class="paragraph"><p>If you are coming from CVS background, the style of cooperation
2202 suggested in the previous section may be new to you. You do not
2203 have to worry. git supports "shared public repository" style of
2204 cooperation you are probably more familiar with as well.</p></div>
2205 <div class="paragraph"><p>See <a href="gitcvs-migration.html">gitcvs-migration(7)</a> for the details.</p></div>
2206 </div>
2207 <h2 id="_bundling_your_work_together">Bundling your work together</h2>
2208 <div class="sectionbody">
2209 <div class="paragraph"><p>It is likely that you will be working on more than one thing at
2210 a time. It is easy to manage those more-or-less independent tasks
2211 using branches with git.</p></div>
2212 <div class="paragraph"><p>We have already seen how branches work previously,
2213 with "fun and work" example using two branches. The idea is the
2214 same if there are more than two branches. Let&#8217;s say you started
2215 out from "master" head, and have some new code in the "master"
2216 branch, and two independent fixes in the "commit-fix" and
2217 "diff-fix" branches:</p></div>
2218 <div class="listingblock">
2219 <div class="content">
2220 <pre><tt>$ git show-branch
2221 ! [commit-fix] Fix commit message normalization.
2222 ! [diff-fix] Fix rename detection.
2223 * [master] Release candidate #1
2225 + [diff-fix] Fix rename detection.
2226 + [diff-fix~1] Better common substring algorithm.
2227 + [commit-fix] Fix commit message normalization.
2228 * [master] Release candidate #1
2229 ++* [diff-fix~2] Pretty-print messages.</tt></pre>
2230 </div></div>
2231 <div class="paragraph"><p>Both fixes are tested well, and at this point, you want to merge
2232 in both of them. You could merge in <em>diff-fix</em> first and then
2233 <em>commit-fix</em> next, like this:</p></div>
2234 <div class="listingblock">
2235 <div class="content">
2236 <pre><tt>$ git merge -m "Merge fix in diff-fix" diff-fix
2237 $ git merge -m "Merge fix in commit-fix" commit-fix</tt></pre>
2238 </div></div>
2239 <div class="paragraph"><p>Which would result in:</p></div>
2240 <div class="listingblock">
2241 <div class="content">
2242 <pre><tt>$ git show-branch
2243 ! [commit-fix] Fix commit message normalization.
2244 ! [diff-fix] Fix rename detection.
2245 * [master] Merge fix in commit-fix
2247 - [master] Merge fix in commit-fix
2248 + * [commit-fix] Fix commit message normalization.
2249 - [master~1] Merge fix in diff-fix
2250 +* [diff-fix] Fix rename detection.
2251 +* [diff-fix~1] Better common substring algorithm.
2252 * [master~2] Release candidate #1
2253 ++* [master~3] Pretty-print messages.</tt></pre>
2254 </div></div>
2255 <div class="paragraph"><p>However, there is no particular reason to merge in one branch
2256 first and the other next, when what you have are a set of truly
2257 independent changes (if the order mattered, then they are not
2258 independent by definition). You could instead merge those two
2259 branches into the current branch at once. First let&#8217;s undo what
2260 we just did and start over. We would want to get the master
2261 branch before these two merges by resetting it to <em>master~2</em>:</p></div>
2262 <div class="listingblock">
2263 <div class="content">
2264 <pre><tt>$ git reset --hard master~2</tt></pre>
2265 </div></div>
2266 <div class="paragraph"><p>You can make sure <tt>git show-branch</tt> matches the state before
2267 those two <em>git merge</em> you just did. Then, instead of running
2268 two <em>git merge</em> commands in a row, you would merge these two
2269 branch heads (this is known as <em>making an Octopus</em>):</p></div>
2270 <div class="listingblock">
2271 <div class="content">
2272 <pre><tt>$ git merge commit-fix diff-fix
2273 $ git show-branch
2274 ! [commit-fix] Fix commit message normalization.
2275 ! [diff-fix] Fix rename detection.
2276 * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
2278 - [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
2279 + * [commit-fix] Fix commit message normalization.
2280 +* [diff-fix] Fix rename detection.
2281 +* [diff-fix~1] Better common substring algorithm.
2282 * [master~1] Release candidate #1
2283 ++* [master~2] Pretty-print messages.</tt></pre>
2284 </div></div>
2285 <div class="paragraph"><p>Note that you should not do Octopus because you can. An octopus
2286 is a valid thing to do and often makes it easier to view the
2287 commit history if you are merging more than two independent
2288 changes at the same time. However, if you have merge conflicts
2289 with any of the branches you are merging in and need to hand
2290 resolve, that is an indication that the development happened in
2291 those branches were not independent after all, and you should
2292 merge two at a time, documenting how you resolved the conflicts,
2293 and the reason why you preferred changes made in one side over
2294 the other. Otherwise it would make the project history harder
2295 to follow, not easier.</p></div>
2296 </div>
2297 <h2 id="_see_also">SEE ALSO</h2>
2298 <div class="sectionbody">
2299 <div class="paragraph"><p><a href="gittutorial.html">gittutorial(7)</a>,
2300 <a href="gittutorial-2.html">gittutorial-2(7)</a>,
2301 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>,
2302 <a href="git-help.html">git-help(1)</a>,
2303 <a href="everyday.html">Everyday git</a>,
2304 <a href="user-manual.html">The Git User&#8217;s Manual</a></p></div>
2305 </div>
2306 <h2 id="_git">GIT</h2>
2307 <div class="sectionbody">
2308 <div class="paragraph"><p>Part of the <a href="git.html">git(1)</a> suite.</p></div>
2309 </div>
2310 </div>
2311 <div id="footnotes"><hr /></div>
2312 <div id="footer">
2313 <div id="footer-text">
2314 Last updated 2011-09-21 23:01:14 PDT
2315 </div>
2316 </div>
2317 </body>
2318 </html>