Autogenerated HTML docs for v2.44.0-248-g4f9b7
[git-htmldocs.git] / gitcore-tutorial.html
blob0fdcae9011f256a1be644c79736d8e93af5e8489
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
5 <head>
6 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
7 <meta name="generator" content="AsciiDoc 10.2.0" />
8 <title>gitcore-tutorial(7)</title>
9 <style type="text/css">
10 /* Shared CSS for AsciiDoc xhtml11 and html5 backends */
12 /* Default font. */
13 body {
14 font-family: Georgia,serif;
17 /* Title font. */
18 h1, h2, h3, h4, h5, h6,
19 div.title, caption.title,
20 thead, p.table.header,
21 #toctitle,
22 #author, #revnumber, #revdate, #revremark,
23 #footer {
24 font-family: Arial,Helvetica,sans-serif;
27 body {
28 margin: 1em 5% 1em 5%;
31 a {
32 color: blue;
33 text-decoration: underline;
35 a:visited {
36 color: fuchsia;
39 em {
40 font-style: italic;
41 color: navy;
44 strong {
45 font-weight: bold;
46 color: #083194;
49 h1, h2, h3, h4, h5, h6 {
50 color: #527bbd;
51 margin-top: 1.2em;
52 margin-bottom: 0.5em;
53 line-height: 1.3;
56 h1, h2, h3 {
57 border-bottom: 2px solid silver;
59 h2 {
60 padding-top: 0.5em;
62 h3 {
63 float: left;
65 h3 + * {
66 clear: left;
68 h5 {
69 font-size: 1.0em;
72 div.sectionbody {
73 margin-left: 0;
76 hr {
77 border: 1px solid silver;
80 p {
81 margin-top: 0.5em;
82 margin-bottom: 0.5em;
85 ul, ol, li > p {
86 margin-top: 0;
88 ul > li { color: #aaa; }
89 ul > li > * { color: black; }
91 .monospaced, code, pre {
92 font-family: "Courier New", Courier, monospace;
93 font-size: inherit;
94 color: navy;
95 padding: 0;
96 margin: 0;
98 pre {
99 white-space: pre-wrap;
102 #author {
103 color: #527bbd;
104 font-weight: bold;
105 font-size: 1.1em;
107 #email {
109 #revnumber, #revdate, #revremark {
112 #footer {
113 font-size: small;
114 border-top: 2px solid silver;
115 padding-top: 0.5em;
116 margin-top: 4.0em;
118 #footer-text {
119 float: left;
120 padding-bottom: 0.5em;
122 #footer-badges {
123 float: right;
124 padding-bottom: 0.5em;
127 #preamble {
128 margin-top: 1.5em;
129 margin-bottom: 1.5em;
131 div.imageblock, div.exampleblock, div.verseblock,
132 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
133 div.admonitionblock {
134 margin-top: 1.0em;
135 margin-bottom: 1.5em;
137 div.admonitionblock {
138 margin-top: 2.0em;
139 margin-bottom: 2.0em;
140 margin-right: 10%;
141 color: #606060;
144 div.content { /* Block element content. */
145 padding: 0;
148 /* Block element titles. */
149 div.title, caption.title {
150 color: #527bbd;
151 font-weight: bold;
152 text-align: left;
153 margin-top: 1.0em;
154 margin-bottom: 0.5em;
156 div.title + * {
157 margin-top: 0;
160 td div.title:first-child {
161 margin-top: 0.0em;
163 div.content div.title:first-child {
164 margin-top: 0.0em;
166 div.content + div.title {
167 margin-top: 0.0em;
170 div.sidebarblock > div.content {
171 background: #ffffee;
172 border: 1px solid #dddddd;
173 border-left: 4px solid #f0f0f0;
174 padding: 0.5em;
177 div.listingblock > div.content {
178 border: 1px solid #dddddd;
179 border-left: 5px solid #f0f0f0;
180 background: #f8f8f8;
181 padding: 0.5em;
184 div.quoteblock, div.verseblock {
185 padding-left: 1.0em;
186 margin-left: 1.0em;
187 margin-right: 10%;
188 border-left: 5px solid #f0f0f0;
189 color: #888;
192 div.quoteblock > div.attribution {
193 padding-top: 0.5em;
194 text-align: right;
197 div.verseblock > pre.content {
198 font-family: inherit;
199 font-size: inherit;
201 div.verseblock > div.attribution {
202 padding-top: 0.75em;
203 text-align: left;
205 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
206 div.verseblock + div.attribution {
207 text-align: left;
210 div.admonitionblock .icon {
211 vertical-align: top;
212 font-size: 1.1em;
213 font-weight: bold;
214 text-decoration: underline;
215 color: #527bbd;
216 padding-right: 0.5em;
218 div.admonitionblock td.content {
219 padding-left: 0.5em;
220 border-left: 3px solid #dddddd;
223 div.exampleblock > div.content {
224 border-left: 3px solid #dddddd;
225 padding-left: 0.5em;
228 div.imageblock div.content { padding-left: 0; }
229 span.image img { border-style: none; vertical-align: text-bottom; }
230 a.image:visited { color: white; }
232 dl {
233 margin-top: 0.8em;
234 margin-bottom: 0.8em;
236 dt {
237 margin-top: 0.5em;
238 margin-bottom: 0;
239 font-style: normal;
240 color: navy;
242 dd > *:first-child {
243 margin-top: 0.1em;
246 ul, ol {
247 list-style-position: outside;
249 ol.arabic {
250 list-style-type: decimal;
252 ol.loweralpha {
253 list-style-type: lower-alpha;
255 ol.upperalpha {
256 list-style-type: upper-alpha;
258 ol.lowerroman {
259 list-style-type: lower-roman;
261 ol.upperroman {
262 list-style-type: upper-roman;
265 div.compact ul, div.compact ol,
266 div.compact p, div.compact p,
267 div.compact div, div.compact div {
268 margin-top: 0.1em;
269 margin-bottom: 0.1em;
272 tfoot {
273 font-weight: bold;
275 td > div.verse {
276 white-space: pre;
279 div.hdlist {
280 margin-top: 0.8em;
281 margin-bottom: 0.8em;
283 div.hdlist tr {
284 padding-bottom: 15px;
286 dt.hdlist1.strong, td.hdlist1.strong {
287 font-weight: bold;
289 td.hdlist1 {
290 vertical-align: top;
291 font-style: normal;
292 padding-right: 0.8em;
293 color: navy;
295 td.hdlist2 {
296 vertical-align: top;
298 div.hdlist.compact tr {
299 margin: 0;
300 padding-bottom: 0;
303 .comment {
304 background: yellow;
307 .footnote, .footnoteref {
308 font-size: 0.8em;
311 span.footnote, span.footnoteref {
312 vertical-align: super;
315 #footnotes {
316 margin: 20px 0 20px 0;
317 padding: 7px 0 0 0;
320 #footnotes div.footnote {
321 margin: 0 0 5px 0;
324 #footnotes hr {
325 border: none;
326 border-top: 1px solid silver;
327 height: 1px;
328 text-align: left;
329 margin-left: 0;
330 width: 20%;
331 min-width: 100px;
334 div.colist td {
335 padding-right: 0.5em;
336 padding-bottom: 0.3em;
337 vertical-align: top;
339 div.colist td img {
340 margin-top: 0.3em;
343 @media print {
344 #footer-badges { display: none; }
347 #toc {
348 margin-bottom: 2.5em;
351 #toctitle {
352 color: #527bbd;
353 font-size: 1.1em;
354 font-weight: bold;
355 margin-top: 1.0em;
356 margin-bottom: 0.1em;
359 div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
360 margin-top: 0;
361 margin-bottom: 0;
363 div.toclevel2 {
364 margin-left: 2em;
365 font-size: 0.9em;
367 div.toclevel3 {
368 margin-left: 4em;
369 font-size: 0.9em;
371 div.toclevel4 {
372 margin-left: 6em;
373 font-size: 0.9em;
376 span.aqua { color: aqua; }
377 span.black { color: black; }
378 span.blue { color: blue; }
379 span.fuchsia { color: fuchsia; }
380 span.gray { color: gray; }
381 span.green { color: green; }
382 span.lime { color: lime; }
383 span.maroon { color: maroon; }
384 span.navy { color: navy; }
385 span.olive { color: olive; }
386 span.purple { color: purple; }
387 span.red { color: red; }
388 span.silver { color: silver; }
389 span.teal { color: teal; }
390 span.white { color: white; }
391 span.yellow { color: yellow; }
393 span.aqua-background { background: aqua; }
394 span.black-background { background: black; }
395 span.blue-background { background: blue; }
396 span.fuchsia-background { background: fuchsia; }
397 span.gray-background { background: gray; }
398 span.green-background { background: green; }
399 span.lime-background { background: lime; }
400 span.maroon-background { background: maroon; }
401 span.navy-background { background: navy; }
402 span.olive-background { background: olive; }
403 span.purple-background { background: purple; }
404 span.red-background { background: red; }
405 span.silver-background { background: silver; }
406 span.teal-background { background: teal; }
407 span.white-background { background: white; }
408 span.yellow-background { background: yellow; }
410 span.big { font-size: 2em; }
411 span.small { font-size: 0.6em; }
413 span.underline { text-decoration: underline; }
414 span.overline { text-decoration: overline; }
415 span.line-through { text-decoration: line-through; }
417 div.unbreakable { page-break-inside: avoid; }
421 * xhtml11 specific
423 * */
425 div.tableblock {
426 margin-top: 1.0em;
427 margin-bottom: 1.5em;
429 div.tableblock > table {
430 border: 3px solid #527bbd;
432 thead, p.table.header {
433 font-weight: bold;
434 color: #527bbd;
436 p.table {
437 margin-top: 0;
439 /* Because the table frame attribute is overridden by CSS in most browsers. */
440 div.tableblock > table[frame="void"] {
441 border-style: none;
443 div.tableblock > table[frame="hsides"] {
444 border-left-style: none;
445 border-right-style: none;
447 div.tableblock > table[frame="vsides"] {
448 border-top-style: none;
449 border-bottom-style: none;
454 * html5 specific
456 * */
458 table.tableblock {
459 margin-top: 1.0em;
460 margin-bottom: 1.5em;
462 thead, p.tableblock.header {
463 font-weight: bold;
464 color: #527bbd;
466 p.tableblock {
467 margin-top: 0;
469 table.tableblock {
470 border-width: 3px;
471 border-spacing: 0px;
472 border-style: solid;
473 border-color: #527bbd;
474 border-collapse: collapse;
476 th.tableblock, td.tableblock {
477 border-width: 1px;
478 padding: 4px;
479 border-style: solid;
480 border-color: #527bbd;
483 table.tableblock.frame-topbot {
484 border-left-style: hidden;
485 border-right-style: hidden;
487 table.tableblock.frame-sides {
488 border-top-style: hidden;
489 border-bottom-style: hidden;
491 table.tableblock.frame-none {
492 border-style: hidden;
495 th.tableblock.halign-left, td.tableblock.halign-left {
496 text-align: left;
498 th.tableblock.halign-center, td.tableblock.halign-center {
499 text-align: center;
501 th.tableblock.halign-right, td.tableblock.halign-right {
502 text-align: right;
505 th.tableblock.valign-top, td.tableblock.valign-top {
506 vertical-align: top;
508 th.tableblock.valign-middle, td.tableblock.valign-middle {
509 vertical-align: middle;
511 th.tableblock.valign-bottom, td.tableblock.valign-bottom {
512 vertical-align: bottom;
517 * manpage specific
519 * */
521 body.manpage h1 {
522 padding-top: 0.5em;
523 padding-bottom: 0.5em;
524 border-top: 2px solid silver;
525 border-bottom: 2px solid silver;
527 body.manpage h2 {
528 border-style: none;
530 body.manpage div.sectionbody {
531 margin-left: 3em;
534 @media print {
535 body.manpage div#toc { display: none; }
539 </style>
540 <script type="text/javascript">
541 /*<![CDATA[*/
542 var asciidoc = { // Namespace.
544 /////////////////////////////////////////////////////////////////////
545 // Table Of Contents generator
546 /////////////////////////////////////////////////////////////////////
548 /* Author: Mihai Bazon, September 2002
549 * http://students.infoiasi.ro/~mishoo
551 * Table Of Content generator
552 * Version: 0.4
554 * Feel free to use this script under the terms of the GNU General Public
555 * License, as long as you do not remove or alter this notice.
558 /* modified by Troy D. Hanson, September 2006. License: GPL */
559 /* modified by Stuart Rackham, 2006, 2009. License: GPL */
561 // toclevels = 1..4.
562 toc: function (toclevels) {
564 function getText(el) {
565 var text = "";
566 for (var i = el.firstChild; i != null; i = i.nextSibling) {
567 if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
568 text += i.data;
569 else if (i.firstChild != null)
570 text += getText(i);
572 return text;
575 function TocEntry(el, text, toclevel) {
576 this.element = el;
577 this.text = text;
578 this.toclevel = toclevel;
581 function tocEntries(el, toclevels) {
582 var result = new Array;
583 var re = new RegExp('[hH]([1-'+(toclevels+1)+'])');
584 // Function that scans the DOM tree for header elements (the DOM2
585 // nodeIterator API would be a better technique but not supported by all
586 // browsers).
587 var iterate = function (el) {
588 for (var i = el.firstChild; i != null; i = i.nextSibling) {
589 if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
590 var mo = re.exec(i.tagName);
591 if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
592 result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
594 iterate(i);
598 iterate(el);
599 return result;
602 var toc = document.getElementById("toc");
603 if (!toc) {
604 return;
607 // Delete existing TOC entries in case we're reloading the TOC.
608 var tocEntriesToRemove = [];
609 var i;
610 for (i = 0; i < toc.childNodes.length; i++) {
611 var entry = toc.childNodes[i];
612 if (entry.nodeName.toLowerCase() == 'div'
613 && entry.getAttribute("class")
614 && entry.getAttribute("class").match(/^toclevel/))
615 tocEntriesToRemove.push(entry);
617 for (i = 0; i < tocEntriesToRemove.length; i++) {
618 toc.removeChild(tocEntriesToRemove[i]);
621 // Rebuild TOC entries.
622 var entries = tocEntries(document.getElementById("content"), toclevels);
623 for (var i = 0; i < entries.length; ++i) {
624 var entry = entries[i];
625 if (entry.element.id == "")
626 entry.element.id = "_toc_" + i;
627 var a = document.createElement("a");
628 a.href = "#" + entry.element.id;
629 a.appendChild(document.createTextNode(entry.text));
630 var div = document.createElement("div");
631 div.appendChild(a);
632 div.className = "toclevel" + entry.toclevel;
633 toc.appendChild(div);
635 if (entries.length == 0)
636 toc.parentNode.removeChild(toc);
640 /////////////////////////////////////////////////////////////////////
641 // Footnotes generator
642 /////////////////////////////////////////////////////////////////////
644 /* Based on footnote generation code from:
645 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
648 footnotes: function () {
649 // Delete existing footnote entries in case we're reloading the footnodes.
650 var i;
651 var noteholder = document.getElementById("footnotes");
652 if (!noteholder) {
653 return;
655 var entriesToRemove = [];
656 for (i = 0; i < noteholder.childNodes.length; i++) {
657 var entry = noteholder.childNodes[i];
658 if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote")
659 entriesToRemove.push(entry);
661 for (i = 0; i < entriesToRemove.length; i++) {
662 noteholder.removeChild(entriesToRemove[i]);
665 // Rebuild footnote entries.
666 var cont = document.getElementById("content");
667 var spans = cont.getElementsByTagName("span");
668 var refs = {};
669 var n = 0;
670 for (i=0; i<spans.length; i++) {
671 if (spans[i].className == "footnote") {
672 n++;
673 var note = spans[i].getAttribute("data-note");
674 if (!note) {
675 // Use [\s\S] in place of . so multi-line matches work.
676 // Because JavaScript has no s (dotall) regex flag.
677 note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
678 spans[i].innerHTML =
679 "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
680 "' title='View footnote' class='footnote'>" + n + "</a>]";
681 spans[i].setAttribute("data-note", note);
683 noteholder.innerHTML +=
684 "<div class='footnote' id='_footnote_" + n + "'>" +
685 "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
686 n + "</a>. " + note + "</div>";
687 var id =spans[i].getAttribute("id");
688 if (id != null) refs["#"+id] = n;
691 if (n == 0)
692 noteholder.parentNode.removeChild(noteholder);
693 else {
694 // Process footnoterefs.
695 for (i=0; i<spans.length; i++) {
696 if (spans[i].className == "footnoteref") {
697 var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
698 href = href.match(/#.*/)[0]; // Because IE return full URL.
699 n = refs[href];
700 spans[i].innerHTML =
701 "[<a href='#_footnote_" + n +
702 "' title='View footnote' class='footnote'>" + n + "</a>]";
708 install: function(toclevels) {
709 var timerId;
711 function reinstall() {
712 asciidoc.footnotes();
713 if (toclevels) {
714 asciidoc.toc(toclevels);
718 function reinstallAndRemoveTimer() {
719 clearInterval(timerId);
720 reinstall();
723 timerId = setInterval(reinstall, 500);
724 if (document.addEventListener)
725 document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false);
726 else
727 window.onload = reinstallAndRemoveTimer;
731 asciidoc.install();
732 /*]]>*/
733 </script>
734 </head>
735 <body class="manpage">
736 <div id="header">
737 <h1>
738 gitcore-tutorial(7) Manual Page
739 </h1>
740 <h2>NAME</h2>
741 <div class="sectionbody">
742 <p>gitcore-tutorial -
743 A Git core tutorial for developers
744 </p>
745 </div>
746 </div>
747 <div id="content">
748 <div class="sect1">
749 <h2 id="_synopsis">SYNOPSIS</h2>
750 <div class="sectionbody">
751 <div class="paragraph"><p>git *</p></div>
752 </div>
753 </div>
754 <div class="sect1">
755 <h2 id="_description">DESCRIPTION</h2>
756 <div class="sectionbody">
757 <div class="paragraph"><p>This tutorial explains how to use the "core" Git commands to set up and
758 work with a Git repository.</p></div>
759 <div class="paragraph"><p>If you just need to use Git as a revision control system you may prefer
760 to start with "A Tutorial Introduction to Git" (<a href="gittutorial.html">gittutorial(7)</a>) or
761 <a href="user-manual.html">the Git User Manual</a>.</p></div>
762 <div class="paragraph"><p>However, an understanding of these low-level tools can be helpful if
763 you want to understand Git&#8217;s internals.</p></div>
764 <div class="paragraph"><p>The core Git is often called "plumbing", with the prettier user
765 interfaces on top of it called "porcelain". You may not want to use the
766 plumbing directly very often, but it can be good to know what the
767 plumbing does when the porcelain isn&#8217;t flushing.</p></div>
768 <div class="paragraph"><p>Back when this document was originally written, many porcelain
769 commands were shell scripts. For simplicity, it still uses them as
770 examples to illustrate how plumbing is fit together to form the
771 porcelain commands. The source tree includes some of these scripts in
772 contrib/examples/ for reference. Although these are not implemented as
773 shell scripts anymore, the description of what the plumbing layer
774 commands do is still valid.</p></div>
775 <div class="admonitionblock">
776 <table><tr>
777 <td class="icon">
778 <div class="title">Note</div>
779 </td>
780 <td class="content">Deeper technical details are often marked as Notes, which you can
781 skip on your first reading.</td>
782 </tr></table>
783 </div>
784 </div>
785 </div>
786 <div class="sect1">
787 <h2 id="_creating_a_git_repository">Creating a Git repository</h2>
788 <div class="sectionbody">
789 <div class="paragraph"><p>Creating a new Git repository couldn&#8217;t be easier: all Git repositories start
790 out empty, and the only thing you need to do is find yourself a
791 subdirectory that you want to use as a working tree - either an empty
792 one for a totally new project, or an existing working tree that you want
793 to import into Git.</p></div>
794 <div class="paragraph"><p>For our first example, we&#8217;re going to start a totally new repository from
795 scratch, with no pre-existing files, and we&#8217;ll call it <em>git-tutorial</em>.
796 To start up, create a subdirectory for it, change into that
797 subdirectory, and initialize the Git infrastructure with <em>git init</em>:</p></div>
798 <div class="listingblock">
799 <div class="content">
800 <pre><code>$ mkdir git-tutorial
801 $ cd git-tutorial
802 $ git init</code></pre>
803 </div></div>
804 <div class="paragraph"><p>to which Git will reply</p></div>
805 <div class="listingblock">
806 <div class="content">
807 <pre><code>Initialized empty Git repository in .git/</code></pre>
808 </div></div>
809 <div class="paragraph"><p>which is just Git&#8217;s way of saying that you haven&#8217;t been doing anything
810 strange, and that it will have created a local <code>.git</code> directory setup for
811 your new project. You will now have a <code>.git</code> directory, and you can
812 inspect that with <em>ls</em>. For your new empty project, it should show you
813 three entries, among other things:</p></div>
814 <div class="ulist"><ul>
815 <li>
817 a file called <code>HEAD</code>, that has <code>ref: refs/heads/master</code> in it.
818 This is similar to a symbolic link and points at
819 <code>refs/heads/master</code> relative to the <code>HEAD</code> file.
820 </p>
821 <div class="paragraph"><p>Don&#8217;t worry about the fact that the file that the <code>HEAD</code> link points to
822 doesn&#8217;t even exist yet&#8201;&#8212;&#8201;you haven&#8217;t created the commit that will
823 start your <code>HEAD</code> development branch yet.</p></div>
824 </li>
825 <li>
827 a subdirectory called <code>objects</code>, which will contain all the
828 objects of your project. You should never have any real reason to
829 look at the objects directly, but you might want to know that these
830 objects are what contains all the real <em>data</em> in your repository.
831 </p>
832 </li>
833 <li>
835 a subdirectory called <code>refs</code>, which contains references to objects.
836 </p>
837 </li>
838 </ul></div>
839 <div class="paragraph"><p>In particular, the <code>refs</code> subdirectory will contain two other
840 subdirectories, named <code>heads</code> and <code>tags</code> respectively. They do
841 exactly what their names imply: they contain references to any number
842 of different <em>heads</em> of development (aka <em>branches</em>), and to any
843 <em>tags</em> that you have created to name specific versions in your
844 repository.</p></div>
845 <div class="paragraph"><p>One note: the special <code>master</code> head is the default branch, which is
846 why the <code>.git/HEAD</code> file was created points to it even if it
847 doesn&#8217;t yet exist. Basically, the <code>HEAD</code> link is supposed to always
848 point to the branch you are working on right now, and you always
849 start out expecting to work on the <code>master</code> branch.</p></div>
850 <div class="paragraph"><p>However, this is only a convention, and you can name your branches
851 anything you want, and don&#8217;t have to ever even <em>have</em> a <code>master</code>
852 branch. A number of the Git tools will assume that <code>.git/HEAD</code> is
853 valid, though.</p></div>
854 <div class="admonitionblock">
855 <table><tr>
856 <td class="icon">
857 <div class="title">Note</div>
858 </td>
859 <td class="content">An <em>object</em> is identified by its 160-bit SHA-1 hash, aka <em>object name</em>,
860 and a reference to an object is always the 40-byte hex
861 representation of that SHA-1 name. The files in the <code>refs</code>
862 subdirectory are expected to contain these hex references
863 (usually with a final <code>\n</code> at the end), and you should thus
864 expect to see a number of 41-byte files containing these
865 references in these <code>refs</code> subdirectories when you actually start
866 populating your tree.</td>
867 </tr></table>
868 </div>
869 <div class="admonitionblock">
870 <table><tr>
871 <td class="icon">
872 <div class="title">Note</div>
873 </td>
874 <td class="content">An advanced user may want to take a look at <a href="gitrepository-layout.html">gitrepository-layout(5)</a>
875 after finishing this tutorial.</td>
876 </tr></table>
877 </div>
878 <div class="paragraph"><p>You have now created your first Git repository. Of course, since it&#8217;s
879 empty, that&#8217;s not very useful, so let&#8217;s start populating it with data.</p></div>
880 </div>
881 </div>
882 <div class="sect1">
883 <h2 id="_populating_a_git_repository">Populating a Git repository</h2>
884 <div class="sectionbody">
885 <div class="paragraph"><p>We&#8217;ll keep this simple and stupid, so we&#8217;ll start off with populating a
886 few trivial files just to get a feel for it.</p></div>
887 <div class="paragraph"><p>Start off with just creating any random files that you want to maintain
888 in your Git repository. We&#8217;ll start off with a few bad examples, just to
889 get a feel for how this works:</p></div>
890 <div class="listingblock">
891 <div class="content">
892 <pre><code>$ echo "Hello World" &gt;hello
893 $ echo "Silly example" &gt;example</code></pre>
894 </div></div>
895 <div class="paragraph"><p>you have now created two files in your working tree (aka <em>working directory</em>),
896 but to actually check in your hard work, you will have to go through two steps:</p></div>
897 <div class="ulist"><ul>
898 <li>
900 fill in the <em>index</em> file (aka <em>cache</em>) with the information about your
901 working tree state.
902 </p>
903 </li>
904 <li>
906 commit that index file as an object.
907 </p>
908 </li>
909 </ul></div>
910 <div class="paragraph"><p>The first step is trivial: when you want to tell Git about any changes
911 to your working tree, you use the <em>git update-index</em> program. That
912 program normally just takes a list of filenames you want to update, but
913 to avoid trivial mistakes, it refuses to add new entries to the index
914 (or remove existing ones) unless you explicitly tell it that you&#8217;re
915 adding a new entry with the <code>--add</code> flag (or removing an entry with the
916 <code>--remove</code>) flag.</p></div>
917 <div class="paragraph"><p>So to populate the index with the two files you just created, you can do</p></div>
918 <div class="listingblock">
919 <div class="content">
920 <pre><code>$ git update-index --add hello example</code></pre>
921 </div></div>
922 <div class="paragraph"><p>and you have now told Git to track those two files.</p></div>
923 <div class="paragraph"><p>In fact, as you did that, if you now look into your object directory,
924 you&#8217;ll notice that Git will have added two new objects to the object
925 database. If you did exactly the steps above, you should now be able to do</p></div>
926 <div class="listingblock">
927 <div class="content">
928 <pre><code>$ ls .git/objects/??/*</code></pre>
929 </div></div>
930 <div class="paragraph"><p>and see two files:</p></div>
931 <div class="listingblock">
932 <div class="content">
933 <pre><code>.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238
934 .git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962</code></pre>
935 </div></div>
936 <div class="paragraph"><p>which correspond with the objects with names of <code>557db...</code> and
937 <code>f24c7...</code> respectively.</p></div>
938 <div class="paragraph"><p>If you want to, you can use <em>git cat-file</em> to look at those objects, but
939 you&#8217;ll have to use the object name, not the filename of the object:</p></div>
940 <div class="listingblock">
941 <div class="content">
942 <pre><code>$ git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238</code></pre>
943 </div></div>
944 <div class="paragraph"><p>where the <code>-t</code> tells <em>git cat-file</em> to tell you what the "type" of the
945 object is. Git will tell you that you have a "blob" object (i.e., just a
946 regular file), and you can see the contents with</p></div>
947 <div class="listingblock">
948 <div class="content">
949 <pre><code>$ git cat-file blob 557db03</code></pre>
950 </div></div>
951 <div class="paragraph"><p>which will print out "Hello World". The object <code>557db03</code> is nothing
952 more than the contents of your file <code>hello</code>.</p></div>
953 <div class="admonitionblock">
954 <table><tr>
955 <td class="icon">
956 <div class="title">Note</div>
957 </td>
958 <td class="content">Don&#8217;t confuse that object with the file <code>hello</code> itself. The
959 object is literally just those specific <strong>contents</strong> of the file, and
960 however much you later change the contents in file <code>hello</code>, the object
961 we just looked at will never change. Objects are immutable.</td>
962 </tr></table>
963 </div>
964 <div class="admonitionblock">
965 <table><tr>
966 <td class="icon">
967 <div class="title">Note</div>
968 </td>
969 <td class="content">The second example demonstrates that you can
970 abbreviate the object name to only the first several
971 hexadecimal digits in most places.</td>
972 </tr></table>
973 </div>
974 <div class="paragraph"><p>Anyway, as we mentioned previously, you normally never actually take a
975 look at the objects themselves, and typing long 40-character hex
976 names is not something you&#8217;d normally want to do. The above digression
977 was just to show that <em>git update-index</em> did something magical, and
978 actually saved away the contents of your files into the Git object
979 database.</p></div>
980 <div class="paragraph"><p>Updating the index did something else too: it created a <code>.git/index</code>
981 file. This is the index that describes your current working tree, and
982 something you should be very aware of. Again, you normally never worry
983 about the index file itself, but you should be aware of the fact that
984 you have not actually really "checked in" your files into Git so far,
985 you&#8217;ve only <strong>told</strong> Git about them.</p></div>
986 <div class="paragraph"><p>However, since Git knows about them, you can now start using some of the
987 most basic Git commands to manipulate the files or look at their status.</p></div>
988 <div class="paragraph"><p>In particular, let&#8217;s not even check in the two files into Git yet, we&#8217;ll
989 start off by adding another line to <code>hello</code> first:</p></div>
990 <div class="listingblock">
991 <div class="content">
992 <pre><code>$ echo "It's a new day for git" &gt;&gt;hello</code></pre>
993 </div></div>
994 <div class="paragraph"><p>and you can now, since you told Git about the previous state of <code>hello</code>, ask
995 Git what has changed in the tree compared to your old index, using the
996 <em>git diff-files</em> command:</p></div>
997 <div class="listingblock">
998 <div class="content">
999 <pre><code>$ git diff-files</code></pre>
1000 </div></div>
1001 <div class="paragraph"><p>Oops. That wasn&#8217;t very readable. It just spit out its own internal
1002 version of a <em>diff</em>, but that internal version really just tells you
1003 that it has noticed that "hello" has been modified, and that the old object
1004 contents it had have been replaced with something else.</p></div>
1005 <div class="paragraph"><p>To make it readable, we can tell <em>git diff-files</em> to output the
1006 differences as a patch, using the <code>-p</code> flag:</p></div>
1007 <div class="listingblock">
1008 <div class="content">
1009 <pre><code>$ git diff-files -p
1010 diff --git a/hello b/hello
1011 index 557db03..263414f 100644
1012 --- a/hello
1013 +++ b/hello
1014 @@ -1 +1,2 @@
1015 Hello World
1016 +It's a new day for git</code></pre>
1017 </div></div>
1018 <div class="paragraph"><p>i.e. the diff of the change we caused by adding another line to <code>hello</code>.</p></div>
1019 <div class="paragraph"><p>In other words, <em>git diff-files</em> always shows us the difference between
1020 what is recorded in the index, and what is currently in the working
1021 tree. That&#8217;s very useful.</p></div>
1022 <div class="paragraph"><p>A common shorthand for <code>git diff-files -p</code> is to just write <code>git
1023 diff</code>, which will do the same thing.</p></div>
1024 <div class="listingblock">
1025 <div class="content">
1026 <pre><code>$ git diff
1027 diff --git a/hello b/hello
1028 index 557db03..263414f 100644
1029 --- a/hello
1030 +++ b/hello
1031 @@ -1 +1,2 @@
1032 Hello World
1033 +It's a new day for git</code></pre>
1034 </div></div>
1035 </div>
1036 </div>
1037 <div class="sect1">
1038 <h2 id="_committing_git_state">Committing Git state</h2>
1039 <div class="sectionbody">
1040 <div class="paragraph"><p>Now, we want to go to the next stage in Git, which is to take the files
1041 that Git knows about in the index, and commit them as a real tree. We do
1042 that in two phases: creating a <em>tree</em> object, and committing that <em>tree</em>
1043 object as a <em>commit</em> object together with an explanation of what the
1044 tree was all about, along with information of how we came to that state.</p></div>
1045 <div class="paragraph"><p>Creating a tree object is trivial, and is done with <em>git write-tree</em>.
1046 There are no options or other input: <code>git write-tree</code> will take the
1047 current index state, and write an object that describes that whole
1048 index. In other words, we&#8217;re now tying together all the different
1049 filenames with their contents (and their permissions), and we&#8217;re
1050 creating the equivalent of a Git "directory" object:</p></div>
1051 <div class="listingblock">
1052 <div class="content">
1053 <pre><code>$ git write-tree</code></pre>
1054 </div></div>
1055 <div class="paragraph"><p>and this will just output the name of the resulting tree, in this case
1056 (if you have done exactly as I&#8217;ve described) it should be</p></div>
1057 <div class="listingblock">
1058 <div class="content">
1059 <pre><code>8988da15d077d4829fc51d8544c097def6644dbb</code></pre>
1060 </div></div>
1061 <div class="paragraph"><p>which is another incomprehensible object name. Again, if you want to,
1062 you can use <code>git cat-file -t 8988d...</code> to see that this time the object
1063 is not a "blob" object, but a "tree" object (you can also use
1064 <code>git cat-file</code> to actually output the raw object contents, but you&#8217;ll see
1065 mainly a binary mess, so that&#8217;s less interesting).</p></div>
1066 <div class="paragraph"><p>However&#8201;&#8212;&#8201;normally you&#8217;d never use <em>git write-tree</em> on its own, because
1067 normally you always commit a tree into a commit object using the
1068 <em>git commit-tree</em> command. In fact, it&#8217;s easier to not actually use
1069 <em>git write-tree</em> on its own at all, but to just pass its result in as an
1070 argument to <em>git commit-tree</em>.</p></div>
1071 <div class="paragraph"><p><em>git commit-tree</em> normally takes several arguments&#8201;&#8212;&#8201;it wants to know
1072 what the <em>parent</em> of a commit was, but since this is the first commit
1073 ever in this new repository, and it has no parents, we only need to pass in
1074 the object name of the tree. However, <em>git commit-tree</em> also wants to get a
1075 commit message on its standard input, and it will write out the resulting
1076 object name for the commit to its standard output.</p></div>
1077 <div class="paragraph"><p>And this is where we create the <code>.git/refs/heads/master</code> file
1078 which is pointed at by <code>HEAD</code>. This file is supposed to contain
1079 the reference to the top-of-tree of the master branch, and since
1080 that&#8217;s exactly what <em>git commit-tree</em> spits out, we can do this
1081 all with a sequence of simple shell commands:</p></div>
1082 <div class="listingblock">
1083 <div class="content">
1084 <pre><code>$ tree=$(git write-tree)
1085 $ commit=$(echo 'Initial commit' | git commit-tree $tree)
1086 $ git update-ref HEAD $commit</code></pre>
1087 </div></div>
1088 <div class="paragraph"><p>In this case this creates a totally new commit that is not related to
1089 anything else. Normally you do this only <strong>once</strong> for a project ever, and
1090 all later commits will be parented on top of an earlier commit.</p></div>
1091 <div class="paragraph"><p>Again, normally you&#8217;d never actually do this by hand. There is a
1092 helpful script called <code>git commit</code> that will do all of this for you. So
1093 you could have just written <code>git commit</code>
1094 instead, and it would have done the above magic scripting for you.</p></div>
1095 </div>
1096 </div>
1097 <div class="sect1">
1098 <h2 id="_making_a_change">Making a change</h2>
1099 <div class="sectionbody">
1100 <div class="paragraph"><p>Remember how we did the <em>git update-index</em> on file <code>hello</code> and then we
1101 changed <code>hello</code> afterward, and could compare the new state of <code>hello</code> with the
1102 state we saved in the index file?</p></div>
1103 <div class="paragraph"><p>Further, remember how I said that <em>git write-tree</em> writes the contents
1104 of the <strong>index</strong> file to the tree, and thus what we just committed was in
1105 fact the <strong>original</strong> contents of the file <code>hello</code>, not the new ones. We did
1106 that on purpose, to show the difference between the index state, and the
1107 state in the working tree, and how they don&#8217;t have to match, even
1108 when we commit things.</p></div>
1109 <div class="paragraph"><p>As before, if we do <code>git diff-files -p</code> in our git-tutorial project,
1110 we&#8217;ll still see the same difference we saw last time: the index file
1111 hasn&#8217;t changed by the act of committing anything. However, now that we
1112 have committed something, we can also learn to use a new command:
1113 <em>git diff-index</em>.</p></div>
1114 <div class="paragraph"><p>Unlike <em>git diff-files</em>, which showed the difference between the index
1115 file and the working tree, <em>git diff-index</em> shows the differences
1116 between a committed <strong>tree</strong> and either the index file or the working
1117 tree. In other words, <em>git diff-index</em> wants a tree to be diffed
1118 against, and before we did the commit, we couldn&#8217;t do that, because we
1119 didn&#8217;t have anything to diff against.</p></div>
1120 <div class="paragraph"><p>But now we can do</p></div>
1121 <div class="listingblock">
1122 <div class="content">
1123 <pre><code>$ git diff-index -p HEAD</code></pre>
1124 </div></div>
1125 <div class="paragraph"><p>(where <code>-p</code> has the same meaning as it did in <em>git diff-files</em>), and it
1126 will show us the same difference, but for a totally different reason.
1127 Now we&#8217;re comparing the working tree not against the index file,
1128 but against the tree we just wrote. It just so happens that those two
1129 are obviously the same, so we get the same result.</p></div>
1130 <div class="paragraph"><p>Again, because this is a common operation, you can also just shorthand
1131 it with</p></div>
1132 <div class="listingblock">
1133 <div class="content">
1134 <pre><code>$ git diff HEAD</code></pre>
1135 </div></div>
1136 <div class="paragraph"><p>which ends up doing the above for you.</p></div>
1137 <div class="paragraph"><p>In other words, <em>git diff-index</em> normally compares a tree against the
1138 working tree, but when given the <code>--cached</code> flag, it is told to
1139 instead compare against just the index cache contents, and ignore the
1140 current working tree state entirely. Since we just wrote the index
1141 file to HEAD, doing <code>git diff-index --cached -p HEAD</code> should thus return
1142 an empty set of differences, and that&#8217;s exactly what it does.</p></div>
1143 <div class="admonitionblock">
1144 <table><tr>
1145 <td class="icon">
1146 <div class="title">Note</div>
1147 </td>
1148 <td class="content">
1149 <div class="paragraph"><p><em>git diff-index</em> really always uses the index for its
1150 comparisons, and saying that it compares a tree against the working
1151 tree is thus not strictly accurate. In particular, the list of
1152 files to compare (the "meta-data") <strong>always</strong> comes from the index file,
1153 regardless of whether the <code>--cached</code> flag is used or not. The <code>--cached</code>
1154 flag really only determines whether the file <strong>contents</strong> to be compared
1155 come from the working tree or not.</p></div>
1156 <div class="paragraph"><p>This is not hard to understand, as soon as you realize that Git simply
1157 never knows (or cares) about files that it is not told about
1158 explicitly. Git will never go <strong>looking</strong> for files to compare, it
1159 expects you to tell it what the files are, and that&#8217;s what the index
1160 is there for.</p></div>
1161 </td>
1162 </tr></table>
1163 </div>
1164 <div class="paragraph"><p>However, our next step is to commit the <strong>change</strong> we did, and again, to
1165 understand what&#8217;s going on, keep in mind the difference between "working
1166 tree contents", "index file" and "committed tree". We have changes
1167 in the working tree that we want to commit, and we always have to
1168 work through the index file, so the first thing we need to do is to
1169 update the index cache:</p></div>
1170 <div class="listingblock">
1171 <div class="content">
1172 <pre><code>$ git update-index hello</code></pre>
1173 </div></div>
1174 <div class="paragraph"><p>(note how we didn&#8217;t need the <code>--add</code> flag this time, since Git knew
1175 about the file already).</p></div>
1176 <div class="paragraph"><p>Note what happens to the different <em>git diff-&#42;</em> versions here.
1177 After we&#8217;ve updated <code>hello</code> in the index, <code>git diff-files -p</code> now shows no
1178 differences, but <code>git diff-index -p HEAD</code> still <strong>does</strong> show that the
1179 current state is different from the state we committed. In fact, now
1180 <em>git diff-index</em> shows the same difference whether we use the <code>--cached</code>
1181 flag or not, since now the index is coherent with the working tree.</p></div>
1182 <div class="paragraph"><p>Now, since we&#8217;ve updated <code>hello</code> in the index, we can commit the new
1183 version. We could do it by writing the tree by hand again, and
1184 committing the tree (this time we&#8217;d have to use the <code>-p HEAD</code> flag to
1185 tell commit that the HEAD was the <strong>parent</strong> of the new commit, and that
1186 this wasn&#8217;t an initial commit any more), but you&#8217;ve done that once
1187 already, so let&#8217;s just use the helpful script this time:</p></div>
1188 <div class="listingblock">
1189 <div class="content">
1190 <pre><code>$ git commit</code></pre>
1191 </div></div>
1192 <div class="paragraph"><p>which starts an editor for you to write the commit message and tells you
1193 a bit about what you have done.</p></div>
1194 <div class="paragraph"><p>Write whatever message you want, and all the lines that start with <em>#</em>
1195 will be pruned out, and the rest will be used as the commit message for
1196 the change. If you decide you don&#8217;t want to commit anything after all at
1197 this point (you can continue to edit things and update the index), you
1198 can just leave an empty message. Otherwise <code>git commit</code> will commit
1199 the change for you.</p></div>
1200 <div class="paragraph"><p>You&#8217;ve now made your first real Git commit. And if you&#8217;re interested in
1201 looking at what <code>git commit</code> really does, feel free to investigate:
1202 it&#8217;s a few very simple shell scripts to generate the helpful (?) commit
1203 message headers, and a few one-liners that actually do the
1204 commit itself (<em>git commit</em>).</p></div>
1205 </div>
1206 </div>
1207 <div class="sect1">
1208 <h2 id="_inspecting_changes">Inspecting Changes</h2>
1209 <div class="sectionbody">
1210 <div class="paragraph"><p>While creating changes is useful, it&#8217;s even more useful if you can tell
1211 later what changed. The most useful command for this is another of the
1212 <em>diff</em> family, namely <em>git diff-tree</em>.</p></div>
1213 <div class="paragraph"><p><em>git diff-tree</em> can be given two arbitrary trees, and it will tell you the
1214 differences between them. Perhaps even more commonly, though, you can
1215 give it just a single commit object, and it will figure out the parent
1216 of that commit itself, and show the difference directly. Thus, to get
1217 the same diff that we&#8217;ve already seen several times, we can now do</p></div>
1218 <div class="listingblock">
1219 <div class="content">
1220 <pre><code>$ git diff-tree -p HEAD</code></pre>
1221 </div></div>
1222 <div class="paragraph"><p>(again, <code>-p</code> means to show the difference as a human-readable patch),
1223 and it will show what the last commit (in <code>HEAD</code>) actually changed.</p></div>
1224 <div class="admonitionblock">
1225 <table><tr>
1226 <td class="icon">
1227 <div class="title">Note</div>
1228 </td>
1229 <td class="content">
1230 <div class="paragraph"><p>Here is an ASCII art by Jon Loeliger that illustrates how
1231 various <em>diff-&#42;</em> commands compare things.</p></div>
1232 <div class="literalblock">
1233 <div class="content">
1234 <pre><code> diff-tree
1235 +----+
1239 +-----------+
1240 | Object DB |
1241 | Backing |
1242 | Store |
1243 +-----------+
1246 | | diff-index --cached
1248 diff-index | V
1249 | +-----------+
1250 | | Index |
1251 | | "cache" |
1252 | +-----------+
1255 | | diff-files
1258 +-----------+
1259 | Working |
1260 | Directory |
1261 +-----------+</code></pre>
1262 </div></div>
1263 </td>
1264 </tr></table>
1265 </div>
1266 <div class="paragraph"><p>More interestingly, you can also give <em>git diff-tree</em> the <code>--pretty</code> flag,
1267 which tells it to also show the commit message and author and date of the
1268 commit, and you can tell it to show a whole series of diffs.
1269 Alternatively, you can tell it to be "silent", and not show the diffs at
1270 all, but just show the actual commit message.</p></div>
1271 <div class="paragraph"><p>In fact, together with the <em>git rev-list</em> program (which generates a
1272 list of revisions), <em>git diff-tree</em> ends up being a veritable fount of
1273 changes. You can emulate <code>git log</code>, <code>git log -p</code>, etc. with a trivial
1274 script that pipes the output of <code>git rev-list</code> to <code>git diff-tree --stdin</code>,
1275 which was exactly how early versions of <code>git log</code> were implemented.</p></div>
1276 </div>
1277 </div>
1278 <div class="sect1">
1279 <h2 id="_tagging_a_version">Tagging a version</h2>
1280 <div class="sectionbody">
1281 <div class="paragraph"><p>In Git, there are two kinds of tags, a "light" one, and an "annotated tag".</p></div>
1282 <div class="paragraph"><p>A "light" tag is technically nothing more than a branch, except we put
1283 it in the <code>.git/refs/tags/</code> subdirectory instead of calling it a <code>head</code>.
1284 So the simplest form of tag involves nothing more than</p></div>
1285 <div class="listingblock">
1286 <div class="content">
1287 <pre><code>$ git tag my-first-tag</code></pre>
1288 </div></div>
1289 <div class="paragraph"><p>which just writes the current <code>HEAD</code> into the <code>.git/refs/tags/my-first-tag</code>
1290 file, after which point you can then use this symbolic name for that
1291 particular state. You can, for example, do</p></div>
1292 <div class="listingblock">
1293 <div class="content">
1294 <pre><code>$ git diff my-first-tag</code></pre>
1295 </div></div>
1296 <div class="paragraph"><p>to diff your current state against that tag which at this point will
1297 obviously be an empty diff, but if you continue to develop and commit
1298 stuff, you can use your tag as an "anchor-point" to see what has changed
1299 since you tagged it.</p></div>
1300 <div class="paragraph"><p>An "annotated tag" is actually a real Git object, and contains not only a
1301 pointer to the state you want to tag, but also a small tag name and
1302 message, along with optionally a PGP signature that says that yes,
1303 you really did
1304 that tag. You create these annotated tags with either the <code>-a</code> or
1305 <code>-s</code> flag to <em>git tag</em>:</p></div>
1306 <div class="listingblock">
1307 <div class="content">
1308 <pre><code>$ git tag -s &lt;tagname&gt;</code></pre>
1309 </div></div>
1310 <div class="paragraph"><p>which will sign the current <code>HEAD</code> (but you can also give it another
1311 argument that specifies the thing to tag, e.g., you could have tagged the
1312 current <code>mybranch</code> point by using <code>git tag &lt;tagname&gt; mybranch</code>).</p></div>
1313 <div class="paragraph"><p>You normally only do signed tags for major releases or things
1314 like that, while the light-weight tags are useful for any marking you
1315 want to do&#8201;&#8212;&#8201;any time you decide that you want to remember a certain
1316 point, just create a private tag for it, and you have a nice symbolic
1317 name for the state at that point.</p></div>
1318 </div>
1319 </div>
1320 <div class="sect1">
1321 <h2 id="_copying_repositories">Copying repositories</h2>
1322 <div class="sectionbody">
1323 <div class="paragraph"><p>Git repositories are normally totally self-sufficient and relocatable.
1324 Unlike CVS, for example, there is no separate notion of
1325 "repository" and "working tree". A Git repository normally <strong>is</strong> the
1326 working tree, with the local Git information hidden in the <code>.git</code>
1327 subdirectory. There is nothing else. What you see is what you got.</p></div>
1328 <div class="admonitionblock">
1329 <table><tr>
1330 <td class="icon">
1331 <div class="title">Note</div>
1332 </td>
1333 <td class="content">You can tell Git to split the Git internal information from
1334 the directory that it tracks, but we&#8217;ll ignore that for now: it&#8217;s not
1335 how normal projects work, and it&#8217;s really only meant for special uses.
1336 So the mental model of "the Git information is always tied directly to
1337 the working tree that it describes" may not be technically 100%
1338 accurate, but it&#8217;s a good model for all normal use.</td>
1339 </tr></table>
1340 </div>
1341 <div class="paragraph"><p>This has two implications:</p></div>
1342 <div class="ulist"><ul>
1343 <li>
1345 if you grow bored with the tutorial repository you created (or you&#8217;ve
1346 made a mistake and want to start all over), you can just do simple
1347 </p>
1348 <div class="listingblock">
1349 <div class="content">
1350 <pre><code>$ rm -rf git-tutorial</code></pre>
1351 </div></div>
1352 <div class="paragraph"><p>and it will be gone. There&#8217;s no external repository, and there&#8217;s no
1353 history outside the project you created.</p></div>
1354 </li>
1355 <li>
1357 if you want to move or duplicate a Git repository, you can do so. There
1358 is <em>git clone</em> command, but if all you want to do is just to
1359 create a copy of your repository (with all the full history that
1360 went along with it), you can do so with a regular
1361 <code>cp -a git-tutorial new-git-tutorial</code>.
1362 </p>
1363 <div class="paragraph"><p>Note that when you&#8217;ve moved or copied a Git repository, your Git index
1364 file (which caches various information, notably some of the "stat"
1365 information for the files involved) will likely need to be refreshed.
1366 So after you do a <code>cp -a</code> to create a new copy, you&#8217;ll want to do</p></div>
1367 <div class="listingblock">
1368 <div class="content">
1369 <pre><code>$ git update-index --refresh</code></pre>
1370 </div></div>
1371 <div class="paragraph"><p>in the new repository to make sure that the index file is up to date.</p></div>
1372 </li>
1373 </ul></div>
1374 <div class="paragraph"><p>Note that the second point is true even across machines. You can
1375 duplicate a remote Git repository with <strong>any</strong> regular copy mechanism, be it
1376 <em>scp</em>, <em>rsync</em> or <em>wget</em>.</p></div>
1377 <div class="paragraph"><p>When copying a remote repository, you&#8217;ll want to at a minimum update the
1378 index cache when you do this, and especially with other peoples'
1379 repositories you often want to make sure that the index cache is in some
1380 known state (you don&#8217;t know <strong>what</strong> they&#8217;ve done and not yet checked in),
1381 so usually you&#8217;ll precede the <em>git update-index</em> with a</p></div>
1382 <div class="listingblock">
1383 <div class="content">
1384 <pre><code>$ git read-tree --reset HEAD
1385 $ git update-index --refresh</code></pre>
1386 </div></div>
1387 <div class="paragraph"><p>which will force a total index re-build from the tree pointed to by <code>HEAD</code>.
1388 It resets the index contents to <code>HEAD</code>, and then the <em>git update-index</em>
1389 makes sure to match up all index entries with the checked-out files.
1390 If the original repository had uncommitted changes in its
1391 working tree, <code>git update-index --refresh</code> notices them and
1392 tells you they need to be updated.</p></div>
1393 <div class="paragraph"><p>The above can also be written as simply</p></div>
1394 <div class="listingblock">
1395 <div class="content">
1396 <pre><code>$ git reset</code></pre>
1397 </div></div>
1398 <div class="paragraph"><p>and in fact a lot of the common Git command combinations can be scripted
1399 with the <code>git xyz</code> interfaces. You can learn things by just looking
1400 at what the various git scripts do. For example, <code>git reset</code> used to be
1401 the above two lines implemented in <em>git reset</em>, but some things like
1402 <em>git status</em> and <em>git commit</em> are slightly more complex scripts around
1403 the basic Git commands.</p></div>
1404 <div class="paragraph"><p>Many (most?) public remote repositories will not contain any of
1405 the checked out files or even an index file, and will <strong>only</strong> contain the
1406 actual core Git files. Such a repository usually doesn&#8217;t even have the
1407 <code>.git</code> subdirectory, but has all the Git files directly in the
1408 repository.</p></div>
1409 <div class="paragraph"><p>To create your own local live copy of such a "raw" Git repository, you&#8217;d
1410 first create your own subdirectory for the project, and then copy the
1411 raw repository contents into the <code>.git</code> directory. For example, to
1412 create your own copy of the Git repository, you&#8217;d do the following</p></div>
1413 <div class="listingblock">
1414 <div class="content">
1415 <pre><code>$ mkdir my-git
1416 $ cd my-git
1417 $ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git</code></pre>
1418 </div></div>
1419 <div class="paragraph"><p>followed by</p></div>
1420 <div class="listingblock">
1421 <div class="content">
1422 <pre><code>$ git read-tree HEAD</code></pre>
1423 </div></div>
1424 <div class="paragraph"><p>to populate the index. However, now you have populated the index, and
1425 you have all the Git internal files, but you will notice that you don&#8217;t
1426 actually have any of the working tree files to work on. To get
1427 those, you&#8217;d check them out with</p></div>
1428 <div class="listingblock">
1429 <div class="content">
1430 <pre><code>$ git checkout-index -u -a</code></pre>
1431 </div></div>
1432 <div class="paragraph"><p>where the <code>-u</code> flag means that you want the checkout to keep the index
1433 up to date (so that you don&#8217;t have to refresh it afterward), and the
1434 <code>-a</code> flag means "check out all files" (if you have a stale copy or an
1435 older version of a checked out tree you may also need to add the <code>-f</code>
1436 flag first, to tell <em>git checkout-index</em> to <strong>force</strong> overwriting of any old
1437 files).</p></div>
1438 <div class="paragraph"><p>Again, this can all be simplified with</p></div>
1439 <div class="listingblock">
1440 <div class="content">
1441 <pre><code>$ git clone git://git.kernel.org/pub/scm/git/git.git/ my-git
1442 $ cd my-git
1443 $ git checkout</code></pre>
1444 </div></div>
1445 <div class="paragraph"><p>which will end up doing all of the above for you.</p></div>
1446 <div class="paragraph"><p>You have now successfully copied somebody else&#8217;s (mine) remote
1447 repository, and checked it out.</p></div>
1448 </div>
1449 </div>
1450 <div class="sect1">
1451 <h2 id="_creating_a_new_branch">Creating a new branch</h2>
1452 <div class="sectionbody">
1453 <div class="paragraph"><p>Branches in Git are really nothing more than pointers into the Git
1454 object database from within the <code>.git/refs/</code> subdirectory, and as we
1455 already discussed, the <code>HEAD</code> branch is nothing but a symlink to one of
1456 these object pointers.</p></div>
1457 <div class="paragraph"><p>You can at any time create a new branch by just picking an arbitrary
1458 point in the project history, and just writing the SHA-1 name of that
1459 object into a file under <code>.git/refs/heads/</code>. You can use any filename you
1460 want (and indeed, subdirectories), but the convention is that the
1461 "normal" branch is called <code>master</code>. That&#8217;s just a convention, though,
1462 and nothing enforces it.</p></div>
1463 <div class="paragraph"><p>To show that as an example, let&#8217;s go back to the git-tutorial repository we
1464 used earlier, and create a branch in it. You do that by simply just
1465 saying that you want to check out a new branch:</p></div>
1466 <div class="listingblock">
1467 <div class="content">
1468 <pre><code>$ git switch -c mybranch</code></pre>
1469 </div></div>
1470 <div class="paragraph"><p>will create a new branch based at the current <code>HEAD</code> position, and switch
1471 to it.</p></div>
1472 <div class="admonitionblock">
1473 <table><tr>
1474 <td class="icon">
1475 <div class="title">Note</div>
1476 </td>
1477 <td class="content">
1478 <div class="paragraph"><p>If you make the decision to start your new branch at some
1479 other point in the history than the current <code>HEAD</code>, you can do so by
1480 just telling <em>git switch</em> what the base of the checkout would be.
1481 In other words, if you have an earlier tag or branch, you&#8217;d just do</p></div>
1482 <div class="listingblock">
1483 <div class="content">
1484 <pre><code>$ git switch -c mybranch earlier-commit</code></pre>
1485 </div></div>
1486 <div class="paragraph"><p>and it would create the new branch <code>mybranch</code> at the earlier commit,
1487 and check out the state at that time.</p></div>
1488 </td>
1489 </tr></table>
1490 </div>
1491 <div class="paragraph"><p>You can always just jump back to your original <code>master</code> branch by doing</p></div>
1492 <div class="listingblock">
1493 <div class="content">
1494 <pre><code>$ git switch master</code></pre>
1495 </div></div>
1496 <div class="paragraph"><p>(or any other branch-name, for that matter) and if you forget which
1497 branch you happen to be on, a simple</p></div>
1498 <div class="listingblock">
1499 <div class="content">
1500 <pre><code>$ cat .git/HEAD</code></pre>
1501 </div></div>
1502 <div class="paragraph"><p>will tell you where it&#8217;s pointing. To get the list of branches
1503 you have, you can say</p></div>
1504 <div class="listingblock">
1505 <div class="content">
1506 <pre><code>$ git branch</code></pre>
1507 </div></div>
1508 <div class="paragraph"><p>which used to be nothing more than a simple script around <code>ls .git/refs/heads</code>.
1509 There will be an asterisk in front of the branch you are currently on.</p></div>
1510 <div class="paragraph"><p>Sometimes you may wish to create a new branch <em>without</em> actually
1511 checking it out and switching to it. If so, just use the command</p></div>
1512 <div class="listingblock">
1513 <div class="content">
1514 <pre><code>$ git branch &lt;branchname&gt; [startingpoint]</code></pre>
1515 </div></div>
1516 <div class="paragraph"><p>which will simply <em>create</em> the branch, but will not do anything further.
1517 You can then later&#8201;&#8212;&#8201;once you decide that you want to actually develop
1518 on that branch&#8201;&#8212;&#8201;switch to that branch with a regular <em>git switch</em>
1519 with the branchname as the argument.</p></div>
1520 </div>
1521 </div>
1522 <div class="sect1">
1523 <h2 id="_merging_two_branches">Merging two branches</h2>
1524 <div class="sectionbody">
1525 <div class="paragraph"><p>One of the ideas of having a branch is that you do some (possibly
1526 experimental) work in it, and eventually merge it back to the main
1527 branch. So assuming you created the above <code>mybranch</code> that started out
1528 being the same as the original <code>master</code> branch, let&#8217;s make sure we&#8217;re in
1529 that branch, and do some work there.</p></div>
1530 <div class="listingblock">
1531 <div class="content">
1532 <pre><code>$ git switch mybranch
1533 $ echo "Work, work, work" &gt;&gt;hello
1534 $ git commit -m "Some work." -i hello</code></pre>
1535 </div></div>
1536 <div class="paragraph"><p>Here, we just added another line to <code>hello</code>, and we used a shorthand for
1537 doing both <code>git update-index hello</code> and <code>git commit</code> by just giving the
1538 filename directly to <code>git commit</code>, with an <code>-i</code> flag (it tells
1539 Git to <em>include</em> that file in addition to what you have done to
1540 the index file so far when making the commit). The <code>-m</code> flag is to give the
1541 commit log message from the command line.</p></div>
1542 <div class="paragraph"><p>Now, to make it a bit more interesting, let&#8217;s assume that somebody else
1543 does some work in the original branch, and simulate that by going back
1544 to the master branch, and editing the same file differently there:</p></div>
1545 <div class="listingblock">
1546 <div class="content">
1547 <pre><code>$ git switch master</code></pre>
1548 </div></div>
1549 <div class="paragraph"><p>Here, take a moment to look at the contents of <code>hello</code>, and notice how they
1550 don&#8217;t contain the work we just did in <code>mybranch</code>&#8201;&#8212;&#8201;because that work
1551 hasn&#8217;t happened in the <code>master</code> branch at all. Then do</p></div>
1552 <div class="listingblock">
1553 <div class="content">
1554 <pre><code>$ echo "Play, play, play" &gt;&gt;hello
1555 $ echo "Lots of fun" &gt;&gt;example
1556 $ git commit -m "Some fun." -i hello example</code></pre>
1557 </div></div>
1558 <div class="paragraph"><p>since the master branch is obviously in a much better mood.</p></div>
1559 <div class="paragraph"><p>Now, you&#8217;ve got two branches, and you decide that you want to merge the
1560 work done. Before we do that, let&#8217;s introduce a cool graphical tool that
1561 helps you view what&#8217;s going on:</p></div>
1562 <div class="listingblock">
1563 <div class="content">
1564 <pre><code>$ gitk --all</code></pre>
1565 </div></div>
1566 <div class="paragraph"><p>will show you graphically both of your branches (that&#8217;s what the <code>--all</code>
1567 means: normally it will just show you your current <code>HEAD</code>) and their
1568 histories. You can also see exactly how they came to be from a common
1569 source.</p></div>
1570 <div class="paragraph"><p>Anyway, let&#8217;s exit <em>gitk</em> (<code>^Q</code> or the File menu), and decide that we want
1571 to merge the work we did on the <code>mybranch</code> branch into the <code>master</code>
1572 branch (which is currently our <code>HEAD</code> too). To do that, there&#8217;s a nice
1573 script called <em>git merge</em>, which wants to know which branches you want
1574 to resolve and what the merge is all about:</p></div>
1575 <div class="listingblock">
1576 <div class="content">
1577 <pre><code>$ git merge -m "Merge work in mybranch" mybranch</code></pre>
1578 </div></div>
1579 <div class="paragraph"><p>where the first argument is going to be used as the commit message if
1580 the merge can be resolved automatically.</p></div>
1581 <div class="paragraph"><p>Now, in this case we&#8217;ve intentionally created a situation where the
1582 merge will need to be fixed up by hand, though, so Git will do as much
1583 of it as it can automatically (which in this case is just merge the <code>example</code>
1584 file, which had no differences in the <code>mybranch</code> branch), and say:</p></div>
1585 <div class="listingblock">
1586 <div class="content">
1587 <pre><code> Auto-merging hello
1588 CONFLICT (content): Merge conflict in hello
1589 Automatic merge failed; fix conflicts and then commit the result.</code></pre>
1590 </div></div>
1591 <div class="paragraph"><p>It tells you that it did an "Automatic merge", which
1592 failed due to conflicts in <code>hello</code>.</p></div>
1593 <div class="paragraph"><p>Not to worry. It left the (trivial) conflict in <code>hello</code> in the same form you
1594 should already be well used to if you&#8217;ve ever used CVS, so let&#8217;s just
1595 open <code>hello</code> in our editor (whatever that may be), and fix it up somehow.
1596 I&#8217;d suggest just making it so that <code>hello</code> contains all four lines:</p></div>
1597 <div class="listingblock">
1598 <div class="content">
1599 <pre><code>Hello World
1600 It's a new day for git
1601 Play, play, play
1602 Work, work, work</code></pre>
1603 </div></div>
1604 <div class="paragraph"><p>and once you&#8217;re happy with your manual merge, just do a</p></div>
1605 <div class="listingblock">
1606 <div class="content">
1607 <pre><code>$ git commit -i hello</code></pre>
1608 </div></div>
1609 <div class="paragraph"><p>which will very loudly warn you that you&#8217;re now committing a merge
1610 (which is correct, so never mind), and you can write a small merge
1611 message about your adventures in <em>git merge</em>-land.</p></div>
1612 <div class="paragraph"><p>After you&#8217;re done, start up <code>gitk --all</code> to see graphically what the
1613 history looks like. Notice that <code>mybranch</code> still exists, and you can
1614 switch to it, and continue to work with it if you want to. The
1615 <code>mybranch</code> branch will not contain the merge, but next time you merge it
1616 from the <code>master</code> branch, Git will know how you merged it, so you&#8217;ll not
1617 have to do <em>that</em> merge again.</p></div>
1618 <div class="paragraph"><p>Another useful tool, especially if you do not always work in X-Window
1619 environment, is <code>git show-branch</code>.</p></div>
1620 <div class="listingblock">
1621 <div class="content">
1622 <pre><code>$ git show-branch --topo-order --more=1 master mybranch
1623 * [master] Merge work in mybranch
1624 ! [mybranch] Some work.
1626 - [master] Merge work in mybranch
1627 *+ [mybranch] Some work.
1628 * [master^] Some fun.</code></pre>
1629 </div></div>
1630 <div class="paragraph"><p>The first two lines indicate that it is showing the two branches
1631 with the titles of their top-of-the-tree commits, you are currently on
1632 <code>master</code> branch (notice the asterisk <code>*</code> character), and the first
1633 column for the later output lines is used to show commits contained in the
1634 <code>master</code> branch, and the second column for the <code>mybranch</code>
1635 branch. Three commits are shown along with their titles.
1636 All of them have non blank characters in the first column (<code>*</code>
1637 shows an ordinary commit on the current branch, <code>-</code> is a merge commit), which
1638 means they are now part of the <code>master</code> branch. Only the "Some
1639 work" commit has the plus <code>+</code> character in the second column,
1640 because <code>mybranch</code> has not been merged to incorporate these
1641 commits from the master branch. The string inside brackets
1642 before the commit log message is a short name you can use to
1643 name the commit. In the above example, <em>master</em> and <em>mybranch</em>
1644 are branch heads. <em>master^</em> is the first parent of <em>master</em>
1645 branch head. Please see <a href="gitrevisions.html">gitrevisions(7)</a> if you want to
1646 see more complex cases.</p></div>
1647 <div class="admonitionblock">
1648 <table><tr>
1649 <td class="icon">
1650 <div class="title">Note</div>
1651 </td>
1652 <td class="content">Without the <em>--more=1</em> option, <em>git show-branch</em> would not output the
1653 <em>[master^]</em> commit, as <em>[mybranch]</em> commit is a common ancestor of
1654 both <em>master</em> and <em>mybranch</em> tips. Please see <a href="git-show-branch.html">git-show-branch(1)</a>
1655 for details.</td>
1656 </tr></table>
1657 </div>
1658 <div class="admonitionblock">
1659 <table><tr>
1660 <td class="icon">
1661 <div class="title">Note</div>
1662 </td>
1663 <td class="content">If there were more commits on the <em>master</em> branch after the merge, the
1664 merge commit itself would not be shown by <em>git show-branch</em> by
1665 default. You would need to provide <code>--sparse</code> option to make the
1666 merge commit visible in this case.</td>
1667 </tr></table>
1668 </div>
1669 <div class="paragraph"><p>Now, let&#8217;s pretend you are the one who did all the work in
1670 <code>mybranch</code>, and the fruit of your hard work has finally been merged
1671 to the <code>master</code> branch. Let&#8217;s go back to <code>mybranch</code>, and run
1672 <em>git merge</em> to get the "upstream changes" back to your branch.</p></div>
1673 <div class="listingblock">
1674 <div class="content">
1675 <pre><code>$ git switch mybranch
1676 $ git merge -m "Merge upstream changes." master</code></pre>
1677 </div></div>
1678 <div class="paragraph"><p>This outputs something like this (the actual commit object names
1679 would be different)</p></div>
1680 <div class="listingblock">
1681 <div class="content">
1682 <pre><code>Updating from ae3a2da... to a80b4aa....
1683 Fast-forward (no commit created; -m option ignored)
1684 example | 1 +
1685 hello | 1 +
1686 2 files changed, 2 insertions(+)</code></pre>
1687 </div></div>
1688 <div class="paragraph"><p>Because your branch did not contain anything more than what had
1689 already been merged into the <code>master</code> branch, the merge operation did
1690 not actually do a merge. Instead, it just updated the top of
1691 the tree of your branch to that of the <code>master</code> branch. This is
1692 often called <em>fast-forward</em> merge.</p></div>
1693 <div class="paragraph"><p>You can run <code>gitk --all</code> again to see how the commit ancestry
1694 looks like, or run <em>show-branch</em>, which tells you this.</p></div>
1695 <div class="listingblock">
1696 <div class="content">
1697 <pre><code>$ git show-branch master mybranch
1698 ! [master] Merge work in mybranch
1699 * [mybranch] Merge work in mybranch
1701 -- [master] Merge work in mybranch</code></pre>
1702 </div></div>
1703 </div>
1704 </div>
1705 <div class="sect1">
1706 <h2 id="_merging_external_work">Merging external work</h2>
1707 <div class="sectionbody">
1708 <div class="paragraph"><p>It&#8217;s usually much more common that you merge with somebody else than
1709 merging with your own branches, so it&#8217;s worth pointing out that Git
1710 makes that very easy too, and in fact, it&#8217;s not that different from
1711 doing a <em>git merge</em>. In fact, a remote merge ends up being nothing
1712 more than "fetch the work from a remote repository into a temporary tag"
1713 followed by a <em>git merge</em>.</p></div>
1714 <div class="paragraph"><p>Fetching from a remote repository is done by, unsurprisingly,
1715 <em>git fetch</em>:</p></div>
1716 <div class="listingblock">
1717 <div class="content">
1718 <pre><code>$ git fetch &lt;remote-repository&gt;</code></pre>
1719 </div></div>
1720 <div class="paragraph"><p>One of the following transports can be used to name the
1721 repository to download from:</p></div>
1722 <div class="dlist"><dl>
1723 <dt class="hdlist1">
1725 </dt>
1726 <dd>
1728 <code>remote.machine:/path/to/repo.git/</code> or
1729 </p>
1730 <div class="paragraph"><p><code>ssh://remote.machine/path/to/repo.git/</code></p></div>
1731 <div class="paragraph"><p>This transport can be used for both uploading and downloading,
1732 and requires you to have a log-in privilege over <code>ssh</code> to the
1733 remote machine. It finds out the set of objects the other side
1734 lacks by exchanging the head commits both ends have and
1735 transfers (close to) minimum set of objects. It is by far the
1736 most efficient way to exchange Git objects between repositories.</p></div>
1737 </dd>
1738 <dt class="hdlist1">
1739 Local directory
1740 </dt>
1741 <dd>
1743 <code>/path/to/repo.git/</code>
1744 </p>
1745 <div class="paragraph"><p>This transport is the same as SSH transport but uses <em>sh</em> to run
1746 both ends on the local machine instead of running other end on
1747 the remote machine via <em>ssh</em>.</p></div>
1748 </dd>
1749 <dt class="hdlist1">
1750 Git Native
1751 </dt>
1752 <dd>
1754 <code>git://remote.machine/path/to/repo.git/</code>
1755 </p>
1756 <div class="paragraph"><p>This transport was designed for anonymous downloading. Like SSH
1757 transport, it finds out the set of objects the downstream side
1758 lacks and transfers (close to) minimum set of objects.</p></div>
1759 </dd>
1760 <dt class="hdlist1">
1761 HTTP(S)
1762 </dt>
1763 <dd>
1765 <code>http://remote.machine/path/to/repo.git/</code>
1766 </p>
1767 <div class="paragraph"><p>Downloader from http and https URL
1768 first obtains the topmost commit object name from the remote site
1769 by looking at the specified refname under <code>repo.git/refs/</code> directory,
1770 and then tries to obtain the
1771 commit object by downloading from <code>repo.git/objects/xx/xxx...</code>
1772 using the object name of that commit object. Then it reads the
1773 commit object to find out its parent commits and the associate
1774 tree object; it repeats this process until it gets all the
1775 necessary objects. Because of this behavior, they are
1776 sometimes also called <em>commit walkers</em>.</p></div>
1777 <div class="paragraph"><p>The <em>commit walkers</em> are sometimes also called <em>dumb
1778 transports</em>, because they do not require any Git aware smart
1779 server like Git Native transport does. Any stock HTTP server
1780 that does not even support directory index would suffice. But
1781 you must prepare your repository with <em>git update-server-info</em>
1782 to help dumb transport downloaders.</p></div>
1783 </dd>
1784 </dl></div>
1785 <div class="paragraph"><p>Once you fetch from the remote repository, you <code>merge</code> that
1786 with your current branch.</p></div>
1787 <div class="paragraph"><p>However&#8201;&#8212;&#8201;it&#8217;s such a common thing to <code>fetch</code> and then
1788 immediately <code>merge</code>, that it&#8217;s called <code>git pull</code>, and you can
1789 simply do</p></div>
1790 <div class="listingblock">
1791 <div class="content">
1792 <pre><code>$ git pull &lt;remote-repository&gt;</code></pre>
1793 </div></div>
1794 <div class="paragraph"><p>and optionally give a branch-name for the remote end as a second
1795 argument.</p></div>
1796 <div class="admonitionblock">
1797 <table><tr>
1798 <td class="icon">
1799 <div class="title">Note</div>
1800 </td>
1801 <td class="content">You could do without using any branches at all, by
1802 keeping as many local repositories as you would like to have
1803 branches, and merging between them with <em>git pull</em>, just like
1804 you merge between branches. The advantage of this approach is
1805 that it lets you keep a set of files for each <code>branch</code> checked
1806 out and you may find it easier to switch back and forth if you
1807 juggle multiple lines of development simultaneously. Of
1808 course, you will pay the price of more disk usage to hold
1809 multiple working trees, but disk space is cheap these days.</td>
1810 </tr></table>
1811 </div>
1812 <div class="paragraph"><p>It is likely that you will be pulling from the same remote
1813 repository from time to time. As a short hand, you can store
1814 the remote repository URL in the local repository&#8217;s config file
1815 like this:</p></div>
1816 <div class="listingblock">
1817 <div class="content">
1818 <pre><code>$ git config remote.linus.url https://git.kernel.org/pub/scm/git/git.git/</code></pre>
1819 </div></div>
1820 <div class="paragraph"><p>and use the "linus" keyword with <em>git pull</em> instead of the full URL.</p></div>
1821 <div class="paragraph"><p>Examples.</p></div>
1822 <div class="olist arabic"><ol class="arabic">
1823 <li>
1825 <code>git pull linus</code>
1826 </p>
1827 </li>
1828 <li>
1830 <code>git pull linus tag v0.99.1</code>
1831 </p>
1832 </li>
1833 </ol></div>
1834 <div class="paragraph"><p>the above are equivalent to:</p></div>
1835 <div class="olist arabic"><ol class="arabic">
1836 <li>
1838 <code>git pull http://www.kernel.org/pub/scm/git/git.git/ HEAD</code>
1839 </p>
1840 </li>
1841 <li>
1843 <code>git pull http://www.kernel.org/pub/scm/git/git.git/ tag v0.99.1</code>
1844 </p>
1845 </li>
1846 </ol></div>
1847 </div>
1848 </div>
1849 <div class="sect1">
1850 <h2 id="_how_does_the_merge_work">How does the merge work?</h2>
1851 <div class="sectionbody">
1852 <div class="paragraph"><p>We said this tutorial shows what plumbing does to help you cope
1853 with the porcelain that isn&#8217;t flushing, but we so far did not
1854 talk about how the merge really works. If you are following
1855 this tutorial the first time, I&#8217;d suggest to skip to "Publishing
1856 your work" section and come back here later.</p></div>
1857 <div class="paragraph"><p>OK, still with me? To give us an example to look at, let&#8217;s go
1858 back to the earlier repository with "hello" and "example" file,
1859 and bring ourselves back to the pre-merge state:</p></div>
1860 <div class="listingblock">
1861 <div class="content">
1862 <pre><code>$ git show-branch --more=2 master mybranch
1863 ! [master] Merge work in mybranch
1864 * [mybranch] Merge work in mybranch
1866 -- [master] Merge work in mybranch
1867 +* [master^2] Some work.
1868 +* [master^] Some fun.</code></pre>
1869 </div></div>
1870 <div class="paragraph"><p>Remember, before running <em>git merge</em>, our <code>master</code> head was at
1871 "Some fun." commit, while our <code>mybranch</code> head was at "Some
1872 work." commit.</p></div>
1873 <div class="listingblock">
1874 <div class="content">
1875 <pre><code>$ git switch -C mybranch master^2
1876 $ git switch master
1877 $ git reset --hard master^</code></pre>
1878 </div></div>
1879 <div class="paragraph"><p>After rewinding, the commit structure should look like this:</p></div>
1880 <div class="listingblock">
1881 <div class="content">
1882 <pre><code>$ git show-branch
1883 * [master] Some fun.
1884 ! [mybranch] Some work.
1886 * [master] Some fun.
1887 + [mybranch] Some work.
1888 *+ [master^] Initial commit</code></pre>
1889 </div></div>
1890 <div class="paragraph"><p>Now we are ready to experiment with the merge by hand.</p></div>
1891 <div class="paragraph"><p><code>git merge</code> command, when merging two branches, uses 3-way merge
1892 algorithm. First, it finds the common ancestor between them.
1893 The command it uses is <em>git merge-base</em>:</p></div>
1894 <div class="listingblock">
1895 <div class="content">
1896 <pre><code>$ mb=$(git merge-base HEAD mybranch)</code></pre>
1897 </div></div>
1898 <div class="paragraph"><p>The command writes the commit object name of the common ancestor
1899 to the standard output, so we captured its output to a variable,
1900 because we will be using it in the next step. By the way, the common
1901 ancestor commit is the "Initial commit" commit in this case. You can
1902 tell it by:</p></div>
1903 <div class="listingblock">
1904 <div class="content">
1905 <pre><code>$ git name-rev --name-only --tags $mb
1906 my-first-tag</code></pre>
1907 </div></div>
1908 <div class="paragraph"><p>After finding out a common ancestor commit, the second step is
1909 this:</p></div>
1910 <div class="listingblock">
1911 <div class="content">
1912 <pre><code>$ git read-tree -m -u $mb HEAD mybranch</code></pre>
1913 </div></div>
1914 <div class="paragraph"><p>This is the same <em>git read-tree</em> command we have already seen,
1915 but it takes three trees, unlike previous examples. This reads
1916 the contents of each tree into different <em>stage</em> in the index
1917 file (the first tree goes to stage 1, the second to stage 2,
1918 etc.). After reading three trees into three stages, the paths
1919 that are the same in all three stages are <em>collapsed</em> into stage
1920 0. Also paths that are the same in two of three stages are
1921 collapsed into stage 0, taking the SHA-1 from either stage 2 or
1922 stage 3, whichever is different from stage 1 (i.e. only one side
1923 changed from the common ancestor).</p></div>
1924 <div class="paragraph"><p>After <em>collapsing</em> operation, paths that are different in three
1925 trees are left in non-zero stages. At this point, you can
1926 inspect the index file with this command:</p></div>
1927 <div class="listingblock">
1928 <div class="content">
1929 <pre><code>$ git ls-files --stage
1930 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1931 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1932 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1933 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello</code></pre>
1934 </div></div>
1935 <div class="paragraph"><p>In our example of only two files, we did not have unchanged
1936 files so only <em>example</em> resulted in collapsing. But in real-life
1937 large projects, when only a small number of files change in one commit,
1938 this <em>collapsing</em> tends to trivially merge most of the paths
1939 fairly quickly, leaving only a handful of real changes in non-zero
1940 stages.</p></div>
1941 <div class="paragraph"><p>To look at only non-zero stages, use <code>--unmerged</code> flag:</p></div>
1942 <div class="listingblock">
1943 <div class="content">
1944 <pre><code>$ git ls-files --unmerged
1945 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1946 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1947 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello</code></pre>
1948 </div></div>
1949 <div class="paragraph"><p>The next step of merging is to merge these three versions of the
1950 file, using 3-way merge. This is done by giving
1951 <em>git merge-one-file</em> command as one of the arguments to
1952 <em>git merge-index</em> command:</p></div>
1953 <div class="listingblock">
1954 <div class="content">
1955 <pre><code>$ git merge-index git-merge-one-file hello
1956 Auto-merging hello
1957 ERROR: Merge conflict in hello
1958 fatal: merge program failed</code></pre>
1959 </div></div>
1960 <div class="paragraph"><p><em>git merge-one-file</em> script is called with parameters to
1961 describe those three versions, and is responsible to leave the
1962 merge results in the working tree.
1963 It is a fairly straightforward shell script, and
1964 eventually calls <em>merge</em> program from RCS suite to perform a
1965 file-level 3-way merge. In this case, <em>merge</em> detects
1966 conflicts, and the merge result with conflict marks is left in
1967 the working tree.. This can be seen if you run <code>ls-files
1968 --stage</code> again at this point:</p></div>
1969 <div class="listingblock">
1970 <div class="content">
1971 <pre><code>$ git ls-files --stage
1972 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1973 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1974 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1975 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello</code></pre>
1976 </div></div>
1977 <div class="paragraph"><p>This is the state of the index file and the working file after
1978 <em>git merge</em> returns control back to you, leaving the conflicting
1979 merge for you to resolve. Notice that the path <code>hello</code> is still
1980 unmerged, and what you see with <em>git diff</em> at this point is
1981 differences since stage 2 (i.e. your version).</p></div>
1982 </div>
1983 </div>
1984 <div class="sect1">
1985 <h2 id="_publishing_your_work">Publishing your work</h2>
1986 <div class="sectionbody">
1987 <div class="paragraph"><p>So, we can use somebody else&#8217;s work from a remote repository, but
1988 how can <strong>you</strong> prepare a repository to let other people pull from
1989 it?</p></div>
1990 <div class="paragraph"><p>You do your real work in your working tree that has your
1991 primary repository hanging under it as its <code>.git</code> subdirectory.
1992 You <strong>could</strong> make that repository accessible remotely and ask
1993 people to pull from it, but in practice that is not the way
1994 things are usually done. A recommended way is to have a public
1995 repository, make it reachable by other people, and when the
1996 changes you made in your primary working tree are in good shape,
1997 update the public repository from it. This is often called
1998 <em>pushing</em>.</p></div>
1999 <div class="admonitionblock">
2000 <table><tr>
2001 <td class="icon">
2002 <div class="title">Note</div>
2003 </td>
2004 <td class="content">This public repository could further be mirrored, and that is
2005 how Git repositories at <code>kernel.org</code> are managed.</td>
2006 </tr></table>
2007 </div>
2008 <div class="paragraph"><p>Publishing the changes from your local (private) repository to
2009 your remote (public) repository requires a write privilege on
2010 the remote machine. You need to have an SSH account there to
2011 run a single command, <em>git-receive-pack</em>.</p></div>
2012 <div class="paragraph"><p>First, you need to create an empty repository on the remote
2013 machine that will house your public repository. This empty
2014 repository will be populated and be kept up to date by pushing
2015 into it later. Obviously, this repository creation needs to be
2016 done only once.</p></div>
2017 <div class="admonitionblock">
2018 <table><tr>
2019 <td class="icon">
2020 <div class="title">Note</div>
2021 </td>
2022 <td class="content"><em>git push</em> uses a pair of commands,
2023 <em>git send-pack</em> on your local machine, and <em>git-receive-pack</em>
2024 on the remote machine. The communication between the two over
2025 the network internally uses an SSH connection.</td>
2026 </tr></table>
2027 </div>
2028 <div class="paragraph"><p>Your private repository&#8217;s Git directory is usually <code>.git</code>, but
2029 your public repository is often named after the project name,
2030 i.e. <code>&lt;project&gt;.git</code>. Let&#8217;s create such a public repository for
2031 project <code>my-git</code>. After logging into the remote machine, create
2032 an empty directory:</p></div>
2033 <div class="listingblock">
2034 <div class="content">
2035 <pre><code>$ mkdir my-git.git</code></pre>
2036 </div></div>
2037 <div class="paragraph"><p>Then, make that directory into a Git repository by running
2038 <em>git init</em>, but this time, since its name is not the usual
2039 <code>.git</code>, we do things slightly differently:</p></div>
2040 <div class="listingblock">
2041 <div class="content">
2042 <pre><code>$ GIT_DIR=my-git.git git init</code></pre>
2043 </div></div>
2044 <div class="paragraph"><p>Make sure this directory is available for others you want your
2045 changes to be pulled via the transport of your choice. Also
2046 you need to make sure that you have the <em>git-receive-pack</em>
2047 program on the <code>$PATH</code>.</p></div>
2048 <div class="admonitionblock">
2049 <table><tr>
2050 <td class="icon">
2051 <div class="title">Note</div>
2052 </td>
2053 <td class="content">Many installations of sshd do not invoke your shell as the login
2054 shell when you directly run programs; what this means is that if
2055 your login shell is <em>bash</em>, only <code>.bashrc</code> is read and not
2056 <code>.bash_profile</code>. As a workaround, make sure <code>.bashrc</code> sets up
2057 <code>$PATH</code> so that you can run <em>git-receive-pack</em> program.</td>
2058 </tr></table>
2059 </div>
2060 <div class="admonitionblock">
2061 <table><tr>
2062 <td class="icon">
2063 <div class="title">Note</div>
2064 </td>
2065 <td class="content">If you plan to publish this repository to be accessed over http,
2066 you should do <code>mv my-git.git/hooks/post-update.sample
2067 my-git.git/hooks/post-update</code> at this point.
2068 This makes sure that every time you push into this
2069 repository, <code>git update-server-info</code> is run.</td>
2070 </tr></table>
2071 </div>
2072 <div class="paragraph"><p>Your "public repository" is now ready to accept your changes.
2073 Come back to the machine you have your private repository. From
2074 there, run this command:</p></div>
2075 <div class="listingblock">
2076 <div class="content">
2077 <pre><code>$ git push &lt;public-host&gt;:/path/to/my-git.git master</code></pre>
2078 </div></div>
2079 <div class="paragraph"><p>This synchronizes your public repository to match the named
2080 branch head (i.e. <code>master</code> in this case) and objects reachable
2081 from them in your current repository.</p></div>
2082 <div class="paragraph"><p>As a real example, this is how I update my public Git
2083 repository. Kernel.org mirror network takes care of the
2084 propagation to other publicly visible machines:</p></div>
2085 <div class="listingblock">
2086 <div class="content">
2087 <pre><code>$ git push master.kernel.org:/pub/scm/git/git.git/</code></pre>
2088 </div></div>
2089 </div>
2090 </div>
2091 <div class="sect1">
2092 <h2 id="_packing_your_repository">Packing your repository</h2>
2093 <div class="sectionbody">
2094 <div class="paragraph"><p>Earlier, we saw that one file under <code>.git/objects/??/</code> directory
2095 is stored for each Git object you create. This representation
2096 is efficient to create atomically and safely, but
2097 not so convenient to transport over the network. Since Git objects are
2098 immutable once they are created, there is a way to optimize the
2099 storage by "packing them together". The command</p></div>
2100 <div class="listingblock">
2101 <div class="content">
2102 <pre><code>$ git repack</code></pre>
2103 </div></div>
2104 <div class="paragraph"><p>will do it for you. If you followed the tutorial examples, you
2105 would have accumulated about 17 objects in <code>.git/objects/??/</code>
2106 directories by now. <em>git repack</em> tells you how many objects it
2107 packed, and stores the packed file in the <code>.git/objects/pack</code>
2108 directory.</p></div>
2109 <div class="admonitionblock">
2110 <table><tr>
2111 <td class="icon">
2112 <div class="title">Note</div>
2113 </td>
2114 <td class="content">You will see two files, <code>pack-*.pack</code> and <code>pack-*.idx</code>,
2115 in <code>.git/objects/pack</code> directory. They are closely related to
2116 each other, and if you ever copy them by hand to a different
2117 repository for whatever reason, you should make sure you copy
2118 them together. The former holds all the data from the objects
2119 in the pack, and the latter holds the index for random
2120 access.</td>
2121 </tr></table>
2122 </div>
2123 <div class="paragraph"><p>If you are paranoid, running <em>git verify-pack</em> command would
2124 detect if you have a corrupt pack, but do not worry too much.
2125 Our programs are always perfect ;-).</p></div>
2126 <div class="paragraph"><p>Once you have packed objects, you do not need to leave the
2127 unpacked objects that are contained in the pack file anymore.</p></div>
2128 <div class="listingblock">
2129 <div class="content">
2130 <pre><code>$ git prune-packed</code></pre>
2131 </div></div>
2132 <div class="paragraph"><p>would remove them for you.</p></div>
2133 <div class="paragraph"><p>You can try running <code>find .git/objects -type f</code> before and after
2134 you run <code>git prune-packed</code> if you are curious. Also <code>git
2135 count-objects</code> would tell you how many unpacked objects are in
2136 your repository and how much space they are consuming.</p></div>
2137 <div class="admonitionblock">
2138 <table><tr>
2139 <td class="icon">
2140 <div class="title">Note</div>
2141 </td>
2142 <td class="content"><code>git pull</code> is slightly cumbersome for HTTP transport, as a
2143 packed repository may contain relatively few objects in a
2144 relatively large pack. If you expect many HTTP pulls from your
2145 public repository you might want to repack &amp; prune often, or
2146 never.</td>
2147 </tr></table>
2148 </div>
2149 <div class="paragraph"><p>If you run <code>git repack</code> again at this point, it will say
2150 "Nothing new to pack.". Once you continue your development and
2151 accumulate the changes, running <code>git repack</code> again will create a
2152 new pack, that contains objects created since you packed your
2153 repository the last time. We recommend that you pack your project
2154 soon after the initial import (unless you are starting your
2155 project from scratch), and then run <code>git repack</code> every once in a
2156 while, depending on how active your project is.</p></div>
2157 <div class="paragraph"><p>When a repository is synchronized via <code>git push</code> and <code>git pull</code>
2158 objects packed in the source repository are usually stored
2159 unpacked in the destination.
2160 While this allows you to use different packing strategies on
2161 both ends, it also means you may need to repack both
2162 repositories every once in a while.</p></div>
2163 </div>
2164 </div>
2165 <div class="sect1">
2166 <h2 id="_working_with_others">Working with Others</h2>
2167 <div class="sectionbody">
2168 <div class="paragraph"><p>Although Git is a truly distributed system, it is often
2169 convenient to organize your project with an informal hierarchy
2170 of developers. Linux kernel development is run this way. There
2171 is a nice illustration (page 17, "Merges to Mainline") in
2172 <a href="https://web.archive.org/web/20120915203609/http://www.xenotime.net/linux/mentor/linux-mentoring-2006.pdf">Randy Dunlap&#8217;s presentation</a>.</p></div>
2173 <div class="paragraph"><p>It should be stressed that this hierarchy is purely <strong>informal</strong>.
2174 There is nothing fundamental in Git that enforces the "chain of
2175 patch flow" this hierarchy implies. You do not have to pull
2176 from only one remote repository.</p></div>
2177 <div class="paragraph"><p>A recommended workflow for a "project lead" goes like this:</p></div>
2178 <div class="olist arabic"><ol class="arabic">
2179 <li>
2181 Prepare your primary repository on your local machine. Your
2182 work is done there.
2183 </p>
2184 </li>
2185 <li>
2187 Prepare a public repository accessible to others.
2188 </p>
2189 <div class="paragraph"><p>If other people are pulling from your repository over dumb
2190 transport protocols (HTTP), you need to keep this repository
2191 <em>dumb transport friendly</em>. After <code>git init</code>,
2192 <code>$GIT_DIR/hooks/post-update.sample</code> copied from the standard templates
2193 would contain a call to <em>git update-server-info</em>
2194 but you need to manually enable the hook with
2195 <code>mv post-update.sample post-update</code>. This makes sure
2196 <em>git update-server-info</em> keeps the necessary files up to date.</p></div>
2197 </li>
2198 <li>
2200 Push into the public repository from your primary
2201 repository.
2202 </p>
2203 </li>
2204 <li>
2206 <em>git repack</em> the public repository. This establishes a big
2207 pack that contains the initial set of objects as the
2208 baseline, and possibly <em>git prune</em> if the transport
2209 used for pulling from your repository supports packed
2210 repositories.
2211 </p>
2212 </li>
2213 <li>
2215 Keep working in your primary repository. Your changes
2216 include modifications of your own, patches you receive via
2217 e-mails, and merges resulting from pulling the "public"
2218 repositories of your "subsystem maintainers".
2219 </p>
2220 <div class="paragraph"><p>You can repack this private repository whenever you feel like.</p></div>
2221 </li>
2222 <li>
2224 Push your changes to the public repository, and announce it
2225 to the public.
2226 </p>
2227 </li>
2228 <li>
2230 Every once in a while, <em>git repack</em> the public repository.
2231 Go back to step 5. and continue working.
2232 </p>
2233 </li>
2234 </ol></div>
2235 <div class="paragraph"><p>A recommended work cycle for a "subsystem maintainer" who works
2236 on that project and has an own "public repository" goes like this:</p></div>
2237 <div class="olist arabic"><ol class="arabic">
2238 <li>
2240 Prepare your work repository, by running <em>git clone</em> on the public
2241 repository of the "project lead". The URL used for the
2242 initial cloning is stored in the remote.origin.url
2243 configuration variable.
2244 </p>
2245 </li>
2246 <li>
2248 Prepare a public repository accessible to others, just like
2249 the "project lead" person does.
2250 </p>
2251 </li>
2252 <li>
2254 Copy over the packed files from "project lead" public
2255 repository to your public repository, unless the "project
2256 lead" repository lives on the same machine as yours. In the
2257 latter case, you can use <code>objects/info/alternates</code> file to
2258 point at the repository you are borrowing from.
2259 </p>
2260 </li>
2261 <li>
2263 Push into the public repository from your primary
2264 repository. Run <em>git repack</em>, and possibly <em>git prune</em> if the
2265 transport used for pulling from your repository supports
2266 packed repositories.
2267 </p>
2268 </li>
2269 <li>
2271 Keep working in your primary repository. Your changes
2272 include modifications of your own, patches you receive via
2273 e-mails, and merges resulting from pulling the "public"
2274 repositories of your "project lead" and possibly your
2275 "sub-subsystem maintainers".
2276 </p>
2277 <div class="paragraph"><p>You can repack this private repository whenever you feel
2278 like.</p></div>
2279 </li>
2280 <li>
2282 Push your changes to your public repository, and ask your
2283 "project lead" and possibly your "sub-subsystem
2284 maintainers" to pull from it.
2285 </p>
2286 </li>
2287 <li>
2289 Every once in a while, <em>git repack</em> the public repository.
2290 Go back to step 5. and continue working.
2291 </p>
2292 </li>
2293 </ol></div>
2294 <div class="paragraph"><p>A recommended work cycle for an "individual developer" who does
2295 not have a "public" repository is somewhat different. It goes
2296 like this:</p></div>
2297 <div class="olist arabic"><ol class="arabic">
2298 <li>
2300 Prepare your work repository, by <em>git clone</em> the public
2301 repository of the "project lead" (or a "subsystem
2302 maintainer", if you work on a subsystem). The URL used for
2303 the initial cloning is stored in the remote.origin.url
2304 configuration variable.
2305 </p>
2306 </li>
2307 <li>
2309 Do your work in your repository on <em>master</em> branch.
2310 </p>
2311 </li>
2312 <li>
2314 Run <code>git fetch origin</code> from the public repository of your
2315 upstream every once in a while. This does only the first
2316 half of <code>git pull</code> but does not merge. The head of the
2317 public repository is stored in <code>.git/refs/remotes/origin/master</code>.
2318 </p>
2319 </li>
2320 <li>
2322 Use <code>git cherry origin</code> to see which ones of your patches
2323 were accepted, and/or use <code>git rebase origin</code> to port your
2324 unmerged changes forward to the updated upstream.
2325 </p>
2326 </li>
2327 <li>
2329 Use <code>git format-patch origin</code> to prepare patches for e-mail
2330 submission to your upstream and send it out. Go back to
2331 step 2. and continue.
2332 </p>
2333 </li>
2334 </ol></div>
2335 </div>
2336 </div>
2337 <div class="sect1">
2338 <h2 id="_working_with_others_shared_repository_style">Working with Others, Shared Repository Style</h2>
2339 <div class="sectionbody">
2340 <div class="paragraph"><p>If you are coming from a CVS background, the style of cooperation
2341 suggested in the previous section may be new to you. You do not
2342 have to worry. Git supports the "shared public repository" style of
2343 cooperation you are probably more familiar with as well.</p></div>
2344 <div class="paragraph"><p>See <a href="gitcvs-migration.html">gitcvs-migration(7)</a> for the details.</p></div>
2345 </div>
2346 </div>
2347 <div class="sect1">
2348 <h2 id="_bundling_your_work_together">Bundling your work together</h2>
2349 <div class="sectionbody">
2350 <div class="paragraph"><p>It is likely that you will be working on more than one thing at
2351 a time. It is easy to manage those more-or-less independent tasks
2352 using branches with Git.</p></div>
2353 <div class="paragraph"><p>We have already seen how branches work previously,
2354 with "fun and work" example using two branches. The idea is the
2355 same if there are more than two branches. Let&#8217;s say you started
2356 out from "master" head, and have some new code in the "master"
2357 branch, and two independent fixes in the "commit-fix" and
2358 "diff-fix" branches:</p></div>
2359 <div class="listingblock">
2360 <div class="content">
2361 <pre><code>$ git show-branch
2362 ! [commit-fix] Fix commit message normalization.
2363 ! [diff-fix] Fix rename detection.
2364 * [master] Release candidate #1
2366 + [diff-fix] Fix rename detection.
2367 + [diff-fix~1] Better common substring algorithm.
2368 + [commit-fix] Fix commit message normalization.
2369 * [master] Release candidate #1
2370 ++* [diff-fix~2] Pretty-print messages.</code></pre>
2371 </div></div>
2372 <div class="paragraph"><p>Both fixes are tested well, and at this point, you want to merge
2373 in both of them. You could merge in <em>diff-fix</em> first and then
2374 <em>commit-fix</em> next, like this:</p></div>
2375 <div class="listingblock">
2376 <div class="content">
2377 <pre><code>$ git merge -m "Merge fix in diff-fix" diff-fix
2378 $ git merge -m "Merge fix in commit-fix" commit-fix</code></pre>
2379 </div></div>
2380 <div class="paragraph"><p>Which would result in:</p></div>
2381 <div class="listingblock">
2382 <div class="content">
2383 <pre><code>$ git show-branch
2384 ! [commit-fix] Fix commit message normalization.
2385 ! [diff-fix] Fix rename detection.
2386 * [master] Merge fix in commit-fix
2388 - [master] Merge fix in commit-fix
2389 + * [commit-fix] Fix commit message normalization.
2390 - [master~1] Merge fix in diff-fix
2391 +* [diff-fix] Fix rename detection.
2392 +* [diff-fix~1] Better common substring algorithm.
2393 * [master~2] Release candidate #1
2394 ++* [master~3] Pretty-print messages.</code></pre>
2395 </div></div>
2396 <div class="paragraph"><p>However, there is no particular reason to merge in one branch
2397 first and the other next, when what you have are a set of truly
2398 independent changes (if the order mattered, then they are not
2399 independent by definition). You could instead merge those two
2400 branches into the current branch at once. First let&#8217;s undo what
2401 we just did and start over. We would want to get the master
2402 branch before these two merges by resetting it to <em>master~2</em>:</p></div>
2403 <div class="listingblock">
2404 <div class="content">
2405 <pre><code>$ git reset --hard master~2</code></pre>
2406 </div></div>
2407 <div class="paragraph"><p>You can make sure <code>git show-branch</code> matches the state before
2408 those two <em>git merge</em> you just did. Then, instead of running
2409 two <em>git merge</em> commands in a row, you would merge these two
2410 branch heads (this is known as <em>making an Octopus</em>):</p></div>
2411 <div class="listingblock">
2412 <div class="content">
2413 <pre><code>$ git merge commit-fix diff-fix
2414 $ git show-branch
2415 ! [commit-fix] Fix commit message normalization.
2416 ! [diff-fix] Fix rename detection.
2417 * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
2419 - [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
2420 + * [commit-fix] Fix commit message normalization.
2421 +* [diff-fix] Fix rename detection.
2422 +* [diff-fix~1] Better common substring algorithm.
2423 * [master~1] Release candidate #1
2424 ++* [master~2] Pretty-print messages.</code></pre>
2425 </div></div>
2426 <div class="paragraph"><p>Note that you should not do Octopus just because you can. An octopus
2427 is a valid thing to do and often makes it easier to view the
2428 commit history if you are merging more than two independent
2429 changes at the same time. However, if you have merge conflicts
2430 with any of the branches you are merging in and need to hand
2431 resolve, that is an indication that the development happened in
2432 those branches were not independent after all, and you should
2433 merge two at a time, documenting how you resolved the conflicts,
2434 and the reason why you preferred changes made in one side over
2435 the other. Otherwise it would make the project history harder
2436 to follow, not easier.</p></div>
2437 </div>
2438 </div>
2439 <div class="sect1">
2440 <h2 id="_see_also">SEE ALSO</h2>
2441 <div class="sectionbody">
2442 <div class="paragraph"><p><a href="gittutorial.html">gittutorial(7)</a>,
2443 <a href="gittutorial-2.html">gittutorial-2(7)</a>,
2444 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>,
2445 <a href="git-help.html">git-help(1)</a>,
2446 <a href="giteveryday.html">giteveryday(7)</a>,
2447 <a href="user-manual.html">The Git User&#8217;s Manual</a></p></div>
2448 </div>
2449 </div>
2450 <div class="sect1">
2451 <h2 id="_git">GIT</h2>
2452 <div class="sectionbody">
2453 <div class="paragraph"><p>Part of the <a href="git.html">git(1)</a> suite</p></div>
2454 </div>
2455 </div>
2456 </div>
2457 <div id="footnotes"><hr /></div>
2458 <div id="footer">
2459 <div id="footer-text">
2460 Last updated
2461 2023-12-18 14:49:41 PST
2462 </div>
2463 </div>
2464 </body>
2465 </html>