Autogenerated HTML docs for v1.7.5.2-315-g11bc3
[git/jnareb-git.git] / gitcore-tutorial.html
blob119b18f09d3f6085e98e4249d49140aab73b6433
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <meta name="generator" content="AsciiDoc 8.4.5" />
7 <title>gitcore-tutorial(7)</title>
8 <style type="text/css">
9 /* Debug borders */
10 p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
12 border: 1px solid red;
16 body {
17 margin: 1em 5% 1em 5%;
20 a {
21 color: blue;
22 text-decoration: underline;
24 a:visited {
25 color: fuchsia;
28 em {
29 font-style: italic;
30 color: navy;
33 strong {
34 font-weight: bold;
35 color: #083194;
38 tt {
39 color: navy;
42 h1, h2, h3, h4, h5, h6 {
43 color: #527bbd;
44 font-family: sans-serif;
45 margin-top: 1.2em;
46 margin-bottom: 0.5em;
47 line-height: 1.3;
50 h1, h2, h3 {
51 border-bottom: 2px solid silver;
53 h2 {
54 padding-top: 0.5em;
56 h3 {
57 float: left;
59 h3 + * {
60 clear: left;
63 div.sectionbody {
64 font-family: serif;
65 margin-left: 0;
68 hr {
69 border: 1px solid silver;
72 p {
73 margin-top: 0.5em;
74 margin-bottom: 0.5em;
77 ul, ol, li > p {
78 margin-top: 0;
81 pre {
82 padding: 0;
83 margin: 0;
86 span#author {
87 color: #527bbd;
88 font-family: sans-serif;
89 font-weight: bold;
90 font-size: 1.1em;
92 span#email {
94 span#revnumber, span#revdate, span#revremark {
95 font-family: sans-serif;
98 div#footer {
99 font-family: sans-serif;
100 font-size: small;
101 border-top: 2px solid silver;
102 padding-top: 0.5em;
103 margin-top: 4.0em;
105 div#footer-text {
106 float: left;
107 padding-bottom: 0.5em;
109 div#footer-badges {
110 float: right;
111 padding-bottom: 0.5em;
114 div#preamble {
115 margin-top: 1.5em;
116 margin-bottom: 1.5em;
118 div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
119 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
120 div.admonitionblock {
121 margin-top: 1.5em;
122 margin-bottom: 1.5em;
124 div.admonitionblock {
125 margin-top: 2.5em;
126 margin-bottom: 2.5em;
129 div.content { /* Block element content. */
130 padding: 0;
133 /* Block element titles. */
134 div.title, caption.title {
135 color: #527bbd;
136 font-family: sans-serif;
137 font-weight: bold;
138 text-align: left;
139 margin-top: 1.0em;
140 margin-bottom: 0.5em;
142 div.title + * {
143 margin-top: 0;
146 td div.title:first-child {
147 margin-top: 0.0em;
149 div.content div.title:first-child {
150 margin-top: 0.0em;
152 div.content + div.title {
153 margin-top: 0.0em;
156 div.sidebarblock > div.content {
157 background: #ffffee;
158 border: 1px solid silver;
159 padding: 0.5em;
162 div.listingblock > div.content {
163 border: 1px solid silver;
164 background: #f4f4f4;
165 padding: 0.5em;
168 div.quoteblock {
169 padding-left: 2.0em;
170 margin-right: 10%;
172 div.quoteblock > div.attribution {
173 padding-top: 0.5em;
174 text-align: right;
177 div.verseblock {
178 padding-left: 2.0em;
179 margin-right: 10%;
181 div.verseblock > div.content {
182 white-space: pre;
184 div.verseblock > div.attribution {
185 padding-top: 0.75em;
186 text-align: left;
188 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
189 div.verseblock + div.attribution {
190 text-align: left;
193 div.admonitionblock .icon {
194 vertical-align: top;
195 font-size: 1.1em;
196 font-weight: bold;
197 text-decoration: underline;
198 color: #527bbd;
199 padding-right: 0.5em;
201 div.admonitionblock td.content {
202 padding-left: 0.5em;
203 border-left: 2px solid silver;
206 div.exampleblock > div.content {
207 border-left: 2px solid silver;
208 padding: 0.5em;
211 div.imageblock div.content { padding-left: 0; }
212 span.image img { border-style: none; }
213 a.image:visited { color: white; }
215 dl {
216 margin-top: 0.8em;
217 margin-bottom: 0.8em;
219 dt {
220 margin-top: 0.5em;
221 margin-bottom: 0;
222 font-style: normal;
223 color: navy;
225 dd > *:first-child {
226 margin-top: 0.1em;
229 ul, ol {
230 list-style-position: outside;
232 ol.arabic {
233 list-style-type: decimal;
235 ol.loweralpha {
236 list-style-type: lower-alpha;
238 ol.upperalpha {
239 list-style-type: upper-alpha;
241 ol.lowerroman {
242 list-style-type: lower-roman;
244 ol.upperroman {
245 list-style-type: upper-roman;
248 div.compact ul, div.compact ol,
249 div.compact p, div.compact p,
250 div.compact div, div.compact div {
251 margin-top: 0.1em;
252 margin-bottom: 0.1em;
255 div.tableblock > table {
256 border: 3px solid #527bbd;
258 thead {
259 font-family: sans-serif;
260 font-weight: bold;
262 tfoot {
263 font-weight: bold;
265 td > div.verse {
266 white-space: pre;
268 p.table {
269 margin-top: 0;
271 /* Because the table frame attribute is overriden by CSS in most browsers. */
272 div.tableblock > table[frame="void"] {
273 border-style: none;
275 div.tableblock > table[frame="hsides"] {
276 border-left-style: none;
277 border-right-style: none;
279 div.tableblock > table[frame="vsides"] {
280 border-top-style: none;
281 border-bottom-style: none;
285 div.hdlist {
286 margin-top: 0.8em;
287 margin-bottom: 0.8em;
289 div.hdlist tr {
290 padding-bottom: 15px;
292 dt.hdlist1.strong, td.hdlist1.strong {
293 font-weight: bold;
295 td.hdlist1 {
296 vertical-align: top;
297 font-style: normal;
298 padding-right: 0.8em;
299 color: navy;
301 td.hdlist2 {
302 vertical-align: top;
304 div.hdlist.compact tr {
305 margin: 0;
306 padding-bottom: 0;
309 .comment {
310 background: yellow;
313 @media print {
314 div#footer-badges { display: none; }
317 div#toctitle {
318 color: #527bbd;
319 font-family: sans-serif;
320 font-size: 1.1em;
321 font-weight: bold;
322 margin-top: 1.0em;
323 margin-bottom: 0.1em;
326 div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
327 margin-top: 0;
328 margin-bottom: 0;
330 div.toclevel2 {
331 margin-left: 2em;
332 font-size: 0.9em;
334 div.toclevel3 {
335 margin-left: 4em;
336 font-size: 0.9em;
338 div.toclevel4 {
339 margin-left: 6em;
340 font-size: 0.9em;
342 /* Overrides for manpage documents */
343 h1 {
344 padding-top: 0.5em;
345 padding-bottom: 0.5em;
346 border-top: 2px solid silver;
347 border-bottom: 2px solid silver;
349 h2 {
350 border-style: none;
352 div.sectionbody {
353 margin-left: 5%;
356 @media print {
357 div#toc { display: none; }
360 /* Workarounds for IE6's broken and incomplete CSS2. */
362 div.sidebar-content {
363 background: #ffffee;
364 border: 1px solid silver;
365 padding: 0.5em;
367 div.sidebar-title, div.image-title {
368 color: #527bbd;
369 font-family: sans-serif;
370 font-weight: bold;
371 margin-top: 0.0em;
372 margin-bottom: 0.5em;
375 div.listingblock div.content {
376 border: 1px solid silver;
377 background: #f4f4f4;
378 padding: 0.5em;
381 div.quoteblock-attribution {
382 padding-top: 0.5em;
383 text-align: right;
386 div.verseblock-content {
387 white-space: pre;
389 div.verseblock-attribution {
390 padding-top: 0.75em;
391 text-align: left;
394 div.exampleblock-content {
395 border-left: 2px solid silver;
396 padding-left: 0.5em;
399 /* IE6 sets dynamically generated links as visited. */
400 div#toc a:visited { color: blue; }
401 </style>
402 </head>
403 <body>
404 <div id="header">
405 <h1>
406 gitcore-tutorial(7) Manual Page
407 </h1>
408 <h2>NAME</h2>
409 <div class="sectionbody">
410 <p>gitcore-tutorial -
411 A git core tutorial for developers
412 </p>
413 </div>
414 </div>
415 <h2 id="_synopsis">SYNOPSIS</h2>
416 <div class="sectionbody">
417 <div class="paragraph"><p>git *</p></div>
418 </div>
419 <h2 id="_description">DESCRIPTION</h2>
420 <div class="sectionbody">
421 <div class="paragraph"><p>This tutorial explains how to use the "core" git commands to set up and
422 work with a git repository.</p></div>
423 <div class="paragraph"><p>If you just need to use git as a revision control system you may prefer
424 to start with "A Tutorial Introduction to GIT" (<a href="gittutorial.html">gittutorial(7)</a>) or
425 <a href="user-manual.html">the GIT User Manual</a>.</p></div>
426 <div class="paragraph"><p>However, an understanding of these low-level tools can be helpful if
427 you want to understand git&#8217;s internals.</p></div>
428 <div class="paragraph"><p>The core git is often called "plumbing", with the prettier user
429 interfaces on top of it called "porcelain". You may not want to use the
430 plumbing directly very often, but it can be good to know what the
431 plumbing does for when the porcelain isn&#8217;t flushing.</p></div>
432 <div class="paragraph"><p>Back when this document was originally written, many porcelain
433 commands were shell scripts. For simplicity, it still uses them as
434 examples to illustrate how plumbing is fit together to form the
435 porcelain commands. The source tree includes some of these scripts in
436 contrib/examples/ for reference. Although these are not implemented as
437 shell scripts anymore, the description of what the plumbing layer
438 commands do is still valid.</p></div>
439 <div class="admonitionblock">
440 <table><tr>
441 <td class="icon">
442 <div class="title">Note</div>
443 </td>
444 <td class="content">Deeper technical details are often marked as Notes, which you can
445 skip on your first reading.</td>
446 </tr></table>
447 </div>
448 </div>
449 <h2 id="_creating_a_git_repository">Creating a git repository</h2>
450 <div class="sectionbody">
451 <div class="paragraph"><p>Creating a new git repository couldn&#8217;t be easier: all git repositories start
452 out empty, and the only thing you need to do is find yourself a
453 subdirectory that you want to use as a working tree - either an empty
454 one for a totally new project, or an existing working tree that you want
455 to import into git.</p></div>
456 <div class="paragraph"><p>For our first example, we&#8217;re going to start a totally new repository from
457 scratch, with no pre-existing files, and we&#8217;ll call it <em>git-tutorial</em>.
458 To start up, create a subdirectory for it, change into that
459 subdirectory, and initialize the git infrastructure with <em>git init</em>:</p></div>
460 <div class="listingblock">
461 <div class="content">
462 <pre><tt>$ mkdir git-tutorial
463 $ cd git-tutorial
464 $ git init</tt></pre>
465 </div></div>
466 <div class="paragraph"><p>to which git will reply</p></div>
467 <div class="listingblock">
468 <div class="content">
469 <pre><tt>Initialized empty Git repository in .git/</tt></pre>
470 </div></div>
471 <div class="paragraph"><p>which is just git&#8217;s way of saying that you haven&#8217;t been doing anything
472 strange, and that it will have created a local <tt>.git</tt> directory setup for
473 your new project. You will now have a <tt>.git</tt> directory, and you can
474 inspect that with <em>ls</em>. For your new empty project, it should show you
475 three entries, among other things:</p></div>
476 <div class="ulist"><ul>
477 <li>
479 a file called <tt>HEAD</tt>, that has <tt>ref: refs/heads/master</tt> in it.
480 This is similar to a symbolic link and points at
481 <tt>refs/heads/master</tt> relative to the <tt>HEAD</tt> file.
482 </p>
483 <div class="paragraph"><p>Don&#8217;t worry about the fact that the file that the <tt>HEAD</tt> link points to
484 doesn&#8217;t even exist yet&#8201;&#8212;&#8201;you haven&#8217;t created the commit that will
485 start your <tt>HEAD</tt> development branch yet.</p></div>
486 </li>
487 <li>
489 a subdirectory called <tt>objects</tt>, which will contain all the
490 objects of your project. You should never have any real reason to
491 look at the objects directly, but you might want to know that these
492 objects are what contains all the real <em>data</em> in your repository.
493 </p>
494 </li>
495 <li>
497 a subdirectory called <tt>refs</tt>, which contains references to objects.
498 </p>
499 </li>
500 </ul></div>
501 <div class="paragraph"><p>In particular, the <tt>refs</tt> subdirectory will contain two other
502 subdirectories, named <tt>heads</tt> and <tt>tags</tt> respectively. They do
503 exactly what their names imply: they contain references to any number
504 of different <em>heads</em> of development (aka <em>branches</em>), and to any
505 <em>tags</em> that you have created to name specific versions in your
506 repository.</p></div>
507 <div class="paragraph"><p>One note: the special <tt>master</tt> head is the default branch, which is
508 why the <tt>.git/HEAD</tt> file was created points to it even if it
509 doesn&#8217;t yet exist. Basically, the <tt>HEAD</tt> link is supposed to always
510 point to the branch you are working on right now, and you always
511 start out expecting to work on the <tt>master</tt> branch.</p></div>
512 <div class="paragraph"><p>However, this is only a convention, and you can name your branches
513 anything you want, and don&#8217;t have to ever even <em>have</em> a <tt>master</tt>
514 branch. A number of the git tools will assume that <tt>.git/HEAD</tt> is
515 valid, though.</p></div>
516 <div class="admonitionblock">
517 <table><tr>
518 <td class="icon">
519 <div class="title">Note</div>
520 </td>
521 <td class="content">An <em>object</em> is identified by its 160-bit SHA1 hash, aka <em>object name</em>,
522 and a reference to an object is always the 40-byte hex
523 representation of that SHA1 name. The files in the <tt>refs</tt>
524 subdirectory are expected to contain these hex references
525 (usually with a final <tt>\n</tt> at the end), and you should thus
526 expect to see a number of 41-byte files containing these
527 references in these <tt>refs</tt> subdirectories when you actually start
528 populating your tree.</td>
529 </tr></table>
530 </div>
531 <div class="admonitionblock">
532 <table><tr>
533 <td class="icon">
534 <div class="title">Note</div>
535 </td>
536 <td class="content">An advanced user may want to take a look at <a href="gitrepository-layout.html">gitrepository-layout(5)</a>
537 after finishing this tutorial.</td>
538 </tr></table>
539 </div>
540 <div class="paragraph"><p>You have now created your first git repository. Of course, since it&#8217;s
541 empty, that&#8217;s not very useful, so let&#8217;s start populating it with data.</p></div>
542 </div>
543 <h2 id="_populating_a_git_repository">Populating a git repository</h2>
544 <div class="sectionbody">
545 <div class="paragraph"><p>We&#8217;ll keep this simple and stupid, so we&#8217;ll start off with populating a
546 few trivial files just to get a feel for it.</p></div>
547 <div class="paragraph"><p>Start off with just creating any random files that you want to maintain
548 in your git repository. We&#8217;ll start off with a few bad examples, just to
549 get a feel for how this works:</p></div>
550 <div class="listingblock">
551 <div class="content">
552 <pre><tt>$ echo "Hello World" &gt;hello
553 $ echo "Silly example" &gt;example</tt></pre>
554 </div></div>
555 <div class="paragraph"><p>you have now created two files in your working tree (aka <em>working directory</em>),
556 but to actually check in your hard work, you will have to go through two steps:</p></div>
557 <div class="ulist"><ul>
558 <li>
560 fill in the <em>index</em> file (aka <em>cache</em>) with the information about your
561 working tree state.
562 </p>
563 </li>
564 <li>
566 commit that index file as an object.
567 </p>
568 </li>
569 </ul></div>
570 <div class="paragraph"><p>The first step is trivial: when you want to tell git about any changes
571 to your working tree, you use the <em>git update-index</em> program. That
572 program normally just takes a list of filenames you want to update, but
573 to avoid trivial mistakes, it refuses to add new entries to the index
574 (or remove existing ones) unless you explicitly tell it that you&#8217;re
575 adding a new entry with the <tt>--add</tt> flag (or removing an entry with the
576 <tt>--remove</tt>) flag.</p></div>
577 <div class="paragraph"><p>So to populate the index with the two files you just created, you can do</p></div>
578 <div class="listingblock">
579 <div class="content">
580 <pre><tt>$ git update-index --add hello example</tt></pre>
581 </div></div>
582 <div class="paragraph"><p>and you have now told git to track those two files.</p></div>
583 <div class="paragraph"><p>In fact, as you did that, if you now look into your object directory,
584 you&#8217;ll notice that git will have added two new objects to the object
585 database. If you did exactly the steps above, you should now be able to do</p></div>
586 <div class="listingblock">
587 <div class="content">
588 <pre><tt>$ ls .git/objects/??/*</tt></pre>
589 </div></div>
590 <div class="paragraph"><p>and see two files:</p></div>
591 <div class="listingblock">
592 <div class="content">
593 <pre><tt>.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238
594 .git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962</tt></pre>
595 </div></div>
596 <div class="paragraph"><p>which correspond with the objects with names of <tt>557db&#8230;</tt> and
597 <tt>f24c7&#8230;</tt> respectively.</p></div>
598 <div class="paragraph"><p>If you want to, you can use <em>git cat-file</em> to look at those objects, but
599 you&#8217;ll have to use the object name, not the filename of the object:</p></div>
600 <div class="listingblock">
601 <div class="content">
602 <pre><tt>$ git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238</tt></pre>
603 </div></div>
604 <div class="paragraph"><p>where the <tt>-t</tt> tells <em>git cat-file</em> to tell you what the "type" of the
605 object is. git will tell you that you have a "blob" object (i.e., just a
606 regular file), and you can see the contents with</p></div>
607 <div class="listingblock">
608 <div class="content">
609 <pre><tt>$ git cat-file blob 557db03</tt></pre>
610 </div></div>
611 <div class="paragraph"><p>which will print out "Hello World". The object <tt>557db03</tt> is nothing
612 more than the contents of your file <tt>hello</tt>.</p></div>
613 <div class="admonitionblock">
614 <table><tr>
615 <td class="icon">
616 <div class="title">Note</div>
617 </td>
618 <td class="content">Don&#8217;t confuse that object with the file <tt>hello</tt> itself. The
619 object is literally just those specific <strong>contents</strong> of the file, and
620 however much you later change the contents in file <tt>hello</tt>, the object
621 we just looked at will never change. Objects are immutable.</td>
622 </tr></table>
623 </div>
624 <div class="admonitionblock">
625 <table><tr>
626 <td class="icon">
627 <div class="title">Note</div>
628 </td>
629 <td class="content">The second example demonstrates that you can
630 abbreviate the object name to only the first several
631 hexadecimal digits in most places.</td>
632 </tr></table>
633 </div>
634 <div class="paragraph"><p>Anyway, as we mentioned previously, you normally never actually take a
635 look at the objects themselves, and typing long 40-character hex
636 names is not something you&#8217;d normally want to do. The above digression
637 was just to show that <em>git update-index</em> did something magical, and
638 actually saved away the contents of your files into the git object
639 database.</p></div>
640 <div class="paragraph"><p>Updating the index did something else too: it created a <tt>.git/index</tt>
641 file. This is the index that describes your current working tree, and
642 something you should be very aware of. Again, you normally never worry
643 about the index file itself, but you should be aware of the fact that
644 you have not actually really "checked in" your files into git so far,
645 you&#8217;ve only <strong>told</strong> git about them.</p></div>
646 <div class="paragraph"><p>However, since git knows about them, you can now start using some of the
647 most basic git commands to manipulate the files or look at their status.</p></div>
648 <div class="paragraph"><p>In particular, let&#8217;s not even check in the two files into git yet, we&#8217;ll
649 start off by adding another line to <tt>hello</tt> first:</p></div>
650 <div class="listingblock">
651 <div class="content">
652 <pre><tt>$ echo "It's a new day for git" &gt;&gt;hello</tt></pre>
653 </div></div>
654 <div class="paragraph"><p>and you can now, since you told git about the previous state of <tt>hello</tt>, ask
655 git what has changed in the tree compared to your old index, using the
656 <em>git diff-files</em> command:</p></div>
657 <div class="listingblock">
658 <div class="content">
659 <pre><tt>$ git diff-files</tt></pre>
660 </div></div>
661 <div class="paragraph"><p>Oops. That wasn&#8217;t very readable. It just spit out its own internal
662 version of a <em>diff</em>, but that internal version really just tells you
663 that it has noticed that "hello" has been modified, and that the old object
664 contents it had have been replaced with something else.</p></div>
665 <div class="paragraph"><p>To make it readable, we can tell <em>git diff-files</em> to output the
666 differences as a patch, using the <tt>-p</tt> flag:</p></div>
667 <div class="listingblock">
668 <div class="content">
669 <pre><tt>$ git diff-files -p
670 diff --git a/hello b/hello
671 index 557db03..263414f 100644
672 --- a/hello
673 +++ b/hello
674 @@ -1 +1,2 @@
675 Hello World
676 +It's a new day for git</tt></pre>
677 </div></div>
678 <div class="paragraph"><p>i.e. the diff of the change we caused by adding another line to <tt>hello</tt>.</p></div>
679 <div class="paragraph"><p>In other words, <em>git diff-files</em> always shows us the difference between
680 what is recorded in the index, and what is currently in the working
681 tree. That&#8217;s very useful.</p></div>
682 <div class="paragraph"><p>A common shorthand for <tt>git diff-files -p</tt> is to just write <tt>git
683 diff</tt>, which will do the same thing.</p></div>
684 <div class="listingblock">
685 <div class="content">
686 <pre><tt>$ git diff
687 diff --git a/hello b/hello
688 index 557db03..263414f 100644
689 --- a/hello
690 +++ b/hello
691 @@ -1 +1,2 @@
692 Hello World
693 +It's a new day for git</tt></pre>
694 </div></div>
695 </div>
696 <h2 id="_committing_git_state">Committing git state</h2>
697 <div class="sectionbody">
698 <div class="paragraph"><p>Now, we want to go to the next stage in git, which is to take the files
699 that git knows about in the index, and commit them as a real tree. We do
700 that in two phases: creating a <em>tree</em> object, and committing that <em>tree</em>
701 object as a <em>commit</em> object together with an explanation of what the
702 tree was all about, along with information of how we came to that state.</p></div>
703 <div class="paragraph"><p>Creating a tree object is trivial, and is done with <em>git write-tree</em>.
704 There are no options or other input: <tt>git write-tree</tt> will take the
705 current index state, and write an object that describes that whole
706 index. In other words, we&#8217;re now tying together all the different
707 filenames with their contents (and their permissions), and we&#8217;re
708 creating the equivalent of a git "directory" object:</p></div>
709 <div class="listingblock">
710 <div class="content">
711 <pre><tt>$ git write-tree</tt></pre>
712 </div></div>
713 <div class="paragraph"><p>and this will just output the name of the resulting tree, in this case
714 (if you have done exactly as I&#8217;ve described) it should be</p></div>
715 <div class="listingblock">
716 <div class="content">
717 <pre><tt>8988da15d077d4829fc51d8544c097def6644dbb</tt></pre>
718 </div></div>
719 <div class="paragraph"><p>which is another incomprehensible object name. Again, if you want to,
720 you can use <tt>git cat-file -t 8988d&#8230;</tt> to see that this time the object
721 is not a "blob" object, but a "tree" object (you can also use
722 <tt>git cat-file</tt> to actually output the raw object contents, but you&#8217;ll see
723 mainly a binary mess, so that&#8217;s less interesting).</p></div>
724 <div class="paragraph"><p>However&#8201;&#8212;&#8201;normally you&#8217;d never use <em>git write-tree</em> on its own, because
725 normally you always commit a tree into a commit object using the
726 <em>git commit-tree</em> command. In fact, it&#8217;s easier to not actually use
727 <em>git write-tree</em> on its own at all, but to just pass its result in as an
728 argument to <em>git commit-tree</em>.</p></div>
729 <div class="paragraph"><p><em>git commit-tree</em> normally takes several arguments&#8201;&#8212;&#8201;it wants to know
730 what the <em>parent</em> of a commit was, but since this is the first commit
731 ever in this new repository, and it has no parents, we only need to pass in
732 the object name of the tree. However, <em>git commit-tree</em> also wants to get a
733 commit message on its standard input, and it will write out the resulting
734 object name for the commit to its standard output.</p></div>
735 <div class="paragraph"><p>And this is where we create the <tt>.git/refs/heads/master</tt> file
736 which is pointed at by <tt>HEAD</tt>. This file is supposed to contain
737 the reference to the top-of-tree of the master branch, and since
738 that&#8217;s exactly what <em>git commit-tree</em> spits out, we can do this
739 all with a sequence of simple shell commands:</p></div>
740 <div class="listingblock">
741 <div class="content">
742 <pre><tt>$ tree=$(git write-tree)
743 $ commit=$(echo 'Initial commit' | git commit-tree $tree)
744 $ git update-ref HEAD $commit</tt></pre>
745 </div></div>
746 <div class="paragraph"><p>In this case this creates a totally new commit that is not related to
747 anything else. Normally you do this only <strong>once</strong> for a project ever, and
748 all later commits will be parented on top of an earlier commit.</p></div>
749 <div class="paragraph"><p>Again, normally you&#8217;d never actually do this by hand. There is a
750 helpful script called <tt>git commit</tt> that will do all of this for you. So
751 you could have just written <tt>git commit</tt>
752 instead, and it would have done the above magic scripting for you.</p></div>
753 </div>
754 <h2 id="_making_a_change">Making a change</h2>
755 <div class="sectionbody">
756 <div class="paragraph"><p>Remember how we did the <em>git update-index</em> on file <tt>hello</tt> and then we
757 changed <tt>hello</tt> afterward, and could compare the new state of <tt>hello</tt> with the
758 state we saved in the index file?</p></div>
759 <div class="paragraph"><p>Further, remember how I said that <em>git write-tree</em> writes the contents
760 of the <strong>index</strong> file to the tree, and thus what we just committed was in
761 fact the <strong>original</strong> contents of the file <tt>hello</tt>, not the new ones. We did
762 that on purpose, to show the difference between the index state, and the
763 state in the working tree, and how they don&#8217;t have to match, even
764 when we commit things.</p></div>
765 <div class="paragraph"><p>As before, if we do <tt>git diff-files -p</tt> in our git-tutorial project,
766 we&#8217;ll still see the same difference we saw last time: the index file
767 hasn&#8217;t changed by the act of committing anything. However, now that we
768 have committed something, we can also learn to use a new command:
769 <em>git diff-index</em>.</p></div>
770 <div class="paragraph"><p>Unlike <em>git diff-files</em>, which showed the difference between the index
771 file and the working tree, <em>git diff-index</em> shows the differences
772 between a committed <strong>tree</strong> and either the index file or the working
773 tree. In other words, <em>git diff-index</em> wants a tree to be diffed
774 against, and before we did the commit, we couldn&#8217;t do that, because we
775 didn&#8217;t have anything to diff against.</p></div>
776 <div class="paragraph"><p>But now we can do</p></div>
777 <div class="listingblock">
778 <div class="content">
779 <pre><tt>$ git diff-index -p HEAD</tt></pre>
780 </div></div>
781 <div class="paragraph"><p>(where <tt>-p</tt> has the same meaning as it did in <em>git diff-files</em>), and it
782 will show us the same difference, but for a totally different reason.
783 Now we&#8217;re comparing the working tree not against the index file,
784 but against the tree we just wrote. It just so happens that those two
785 are obviously the same, so we get the same result.</p></div>
786 <div class="paragraph"><p>Again, because this is a common operation, you can also just shorthand
787 it with</p></div>
788 <div class="listingblock">
789 <div class="content">
790 <pre><tt>$ git diff HEAD</tt></pre>
791 </div></div>
792 <div class="paragraph"><p>which ends up doing the above for you.</p></div>
793 <div class="paragraph"><p>In other words, <em>git diff-index</em> normally compares a tree against the
794 working tree, but when given the <tt>--cached</tt> flag, it is told to
795 instead compare against just the index cache contents, and ignore the
796 current working tree state entirely. Since we just wrote the index
797 file to HEAD, doing <tt>git diff-index --cached -p HEAD</tt> should thus return
798 an empty set of differences, and that&#8217;s exactly what it does.</p></div>
799 <div class="admonitionblock">
800 <table><tr>
801 <td class="icon">
802 <div class="title">Note</div>
803 </td>
804 <td class="content">
805 <div class="paragraph"><p><em>git diff-index</em> really always uses the index for its
806 comparisons, and saying that it compares a tree against the working
807 tree is thus not strictly accurate. In particular, the list of
808 files to compare (the "meta-data") <strong>always</strong> comes from the index file,
809 regardless of whether the <tt>--cached</tt> flag is used or not. The <tt>--cached</tt>
810 flag really only determines whether the file <strong>contents</strong> to be compared
811 come from the working tree or not.</p></div>
812 <div class="paragraph"><p>This is not hard to understand, as soon as you realize that git simply
813 never knows (or cares) about files that it is not told about
814 explicitly. git will never go <strong>looking</strong> for files to compare, it
815 expects you to tell it what the files are, and that&#8217;s what the index
816 is there for.</p></div>
817 </td>
818 </tr></table>
819 </div>
820 <div class="paragraph"><p>However, our next step is to commit the <strong>change</strong> we did, and again, to
821 understand what&#8217;s going on, keep in mind the difference between "working
822 tree contents", "index file" and "committed tree". We have changes
823 in the working tree that we want to commit, and we always have to
824 work through the index file, so the first thing we need to do is to
825 update the index cache:</p></div>
826 <div class="listingblock">
827 <div class="content">
828 <pre><tt>$ git update-index hello</tt></pre>
829 </div></div>
830 <div class="paragraph"><p>(note how we didn&#8217;t need the <tt>--add</tt> flag this time, since git knew
831 about the file already).</p></div>
832 <div class="paragraph"><p>Note what happens to the different <em>git diff-&#42;</em> versions here.
833 After we&#8217;ve updated <tt>hello</tt> in the index, <tt>git diff-files -p</tt> now shows no
834 differences, but <tt>git diff-index -p HEAD</tt> still <strong>does</strong> show that the
835 current state is different from the state we committed. In fact, now
836 <em>git diff-index</em> shows the same difference whether we use the <tt>--cached</tt>
837 flag or not, since now the index is coherent with the working tree.</p></div>
838 <div class="paragraph"><p>Now, since we&#8217;ve updated <tt>hello</tt> in the index, we can commit the new
839 version. We could do it by writing the tree by hand again, and
840 committing the tree (this time we&#8217;d have to use the <tt>-p HEAD</tt> flag to
841 tell commit that the HEAD was the <strong>parent</strong> of the new commit, and that
842 this wasn&#8217;t an initial commit any more), but you&#8217;ve done that once
843 already, so let&#8217;s just use the helpful script this time:</p></div>
844 <div class="listingblock">
845 <div class="content">
846 <pre><tt>$ git commit</tt></pre>
847 </div></div>
848 <div class="paragraph"><p>which starts an editor for you to write the commit message and tells you
849 a bit about what you have done.</p></div>
850 <div class="paragraph"><p>Write whatever message you want, and all the lines that start with <em>#</em>
851 will be pruned out, and the rest will be used as the commit message for
852 the change. If you decide you don&#8217;t want to commit anything after all at
853 this point (you can continue to edit things and update the index), you
854 can just leave an empty message. Otherwise <tt>git commit</tt> will commit
855 the change for you.</p></div>
856 <div class="paragraph"><p>You&#8217;ve now made your first real git commit. And if you&#8217;re interested in
857 looking at what <tt>git commit</tt> really does, feel free to investigate:
858 it&#8217;s a few very simple shell scripts to generate the helpful (?) commit
859 message headers, and a few one-liners that actually do the
860 commit itself (<em>git commit</em>).</p></div>
861 </div>
862 <h2 id="_inspecting_changes">Inspecting Changes</h2>
863 <div class="sectionbody">
864 <div class="paragraph"><p>While creating changes is useful, it&#8217;s even more useful if you can tell
865 later what changed. The most useful command for this is another of the
866 <em>diff</em> family, namely <em>git diff-tree</em>.</p></div>
867 <div class="paragraph"><p><em>git diff-tree</em> can be given two arbitrary trees, and it will tell you the
868 differences between them. Perhaps even more commonly, though, you can
869 give it just a single commit object, and it will figure out the parent
870 of that commit itself, and show the difference directly. Thus, to get
871 the same diff that we&#8217;ve already seen several times, we can now do</p></div>
872 <div class="listingblock">
873 <div class="content">
874 <pre><tt>$ git diff-tree -p HEAD</tt></pre>
875 </div></div>
876 <div class="paragraph"><p>(again, <tt>-p</tt> means to show the difference as a human-readable patch),
877 and it will show what the last commit (in <tt>HEAD</tt>) actually changed.</p></div>
878 <div class="admonitionblock">
879 <table><tr>
880 <td class="icon">
881 <div class="title">Note</div>
882 </td>
883 <td class="content">
884 <div class="paragraph"><p>Here is an ASCII art by Jon Loeliger that illustrates how
885 various <em>diff-&#42;</em> commands compare things.</p></div>
886 <div class="literalblock">
887 <div class="content">
888 <pre><tt> diff-tree
889 +----+
893 +-----------+
894 | Object DB |
895 | Backing |
896 | Store |
897 +-----------+
900 | | diff-index --cached
902 diff-index | V
903 | +-----------+
904 | | Index |
905 | | "cache" |
906 | +-----------+
909 | | diff-files
912 +-----------+
913 | Working |
914 | Directory |
915 +-----------+</tt></pre>
916 </div></div>
917 </td>
918 </tr></table>
919 </div>
920 <div class="paragraph"><p>More interestingly, you can also give <em>git diff-tree</em> the <tt>--pretty</tt> flag,
921 which tells it to also show the commit message and author and date of the
922 commit, and you can tell it to show a whole series of diffs.
923 Alternatively, you can tell it to be "silent", and not show the diffs at
924 all, but just show the actual commit message.</p></div>
925 <div class="paragraph"><p>In fact, together with the <em>git rev-list</em> program (which generates a
926 list of revisions), <em>git diff-tree</em> ends up being a veritable fount of
927 changes. A trivial (but very useful) script called <em>git whatchanged</em> is
928 included with git which does exactly this, and shows a log of recent
929 activities.</p></div>
930 <div class="paragraph"><p>To see the whole history of our pitiful little git-tutorial project, you
931 can do</p></div>
932 <div class="listingblock">
933 <div class="content">
934 <pre><tt>$ git log</tt></pre>
935 </div></div>
936 <div class="paragraph"><p>which shows just the log messages, or if we want to see the log together
937 with the associated patches use the more complex (and much more
938 powerful)</p></div>
939 <div class="listingblock">
940 <div class="content">
941 <pre><tt>$ git whatchanged -p</tt></pre>
942 </div></div>
943 <div class="paragraph"><p>and you will see exactly what has changed in the repository over its
944 short history.</p></div>
945 <div class="admonitionblock">
946 <table><tr>
947 <td class="icon">
948 <div class="title">Note</div>
949 </td>
950 <td class="content">When using the above two commands, the initial commit will be shown.
951 If this is a problem because it is huge, you can hide it by setting
952 the log.showroot configuration variable to false. Having this, you
953 can still show it for each command just adding the <tt>--root</tt> option,
954 which is a flag for <em>git diff-tree</em> accepted by both commands.</td>
955 </tr></table>
956 </div>
957 <div class="paragraph"><p>With that, you should now be having some inkling of what git does, and
958 can explore on your own.</p></div>
959 <div class="admonitionblock">
960 <table><tr>
961 <td class="icon">
962 <div class="title">Note</div>
963 </td>
964 <td class="content">Most likely, you are not directly using the core
965 git Plumbing commands, but using Porcelain such as <em>git add</em>, &#8216;git-rm&#8217;
966 and &#8216;git-commit&#8217;.</td>
967 </tr></table>
968 </div>
969 </div>
970 <h2 id="_tagging_a_version">Tagging a version</h2>
971 <div class="sectionbody">
972 <div class="paragraph"><p>In git, there are two kinds of tags, a "light" one, and an "annotated tag".</p></div>
973 <div class="paragraph"><p>A "light" tag is technically nothing more than a branch, except we put
974 it in the <tt>.git/refs/tags/</tt> subdirectory instead of calling it a <tt>head</tt>.
975 So the simplest form of tag involves nothing more than</p></div>
976 <div class="listingblock">
977 <div class="content">
978 <pre><tt>$ git tag my-first-tag</tt></pre>
979 </div></div>
980 <div class="paragraph"><p>which just writes the current <tt>HEAD</tt> into the <tt>.git/refs/tags/my-first-tag</tt>
981 file, after which point you can then use this symbolic name for that
982 particular state. You can, for example, do</p></div>
983 <div class="listingblock">
984 <div class="content">
985 <pre><tt>$ git diff my-first-tag</tt></pre>
986 </div></div>
987 <div class="paragraph"><p>to diff your current state against that tag which at this point will
988 obviously be an empty diff, but if you continue to develop and commit
989 stuff, you can use your tag as an "anchor-point" to see what has changed
990 since you tagged it.</p></div>
991 <div class="paragraph"><p>An "annotated tag" is actually a real git object, and contains not only a
992 pointer to the state you want to tag, but also a small tag name and
993 message, along with optionally a PGP signature that says that yes,
994 you really did
995 that tag. You create these annotated tags with either the <tt>-a</tt> or
996 <tt>-s</tt> flag to <em>git tag</em>:</p></div>
997 <div class="listingblock">
998 <div class="content">
999 <pre><tt>$ git tag -s &lt;tagname&gt;</tt></pre>
1000 </div></div>
1001 <div class="paragraph"><p>which will sign the current <tt>HEAD</tt> (but you can also give it another
1002 argument that specifies the thing to tag, e.g., you could have tagged the
1003 current <tt>mybranch</tt> point by using <tt>git tag &lt;tagname&gt; mybranch</tt>).</p></div>
1004 <div class="paragraph"><p>You normally only do signed tags for major releases or things
1005 like that, while the light-weight tags are useful for any marking you
1006 want to do&#8201;&#8212;&#8201;any time you decide that you want to remember a certain
1007 point, just create a private tag for it, and you have a nice symbolic
1008 name for the state at that point.</p></div>
1009 </div>
1010 <h2 id="_copying_repositories">Copying repositories</h2>
1011 <div class="sectionbody">
1012 <div class="paragraph"><p>git repositories are normally totally self-sufficient and relocatable.
1013 Unlike CVS, for example, there is no separate notion of
1014 "repository" and "working tree". A git repository normally <strong>is</strong> the
1015 working tree, with the local git information hidden in the <tt>.git</tt>
1016 subdirectory. There is nothing else. What you see is what you got.</p></div>
1017 <div class="admonitionblock">
1018 <table><tr>
1019 <td class="icon">
1020 <div class="title">Note</div>
1021 </td>
1022 <td class="content">You can tell git to split the git internal information from
1023 the directory that it tracks, but we&#8217;ll ignore that for now: it&#8217;s not
1024 how normal projects work, and it&#8217;s really only meant for special uses.
1025 So the mental model of "the git information is always tied directly to
1026 the working tree that it describes" may not be technically 100%
1027 accurate, but it&#8217;s a good model for all normal use.</td>
1028 </tr></table>
1029 </div>
1030 <div class="paragraph"><p>This has two implications:</p></div>
1031 <div class="ulist"><ul>
1032 <li>
1034 if you grow bored with the tutorial repository you created (or you&#8217;ve
1035 made a mistake and want to start all over), you can just do simple
1036 </p>
1037 <div class="listingblock">
1038 <div class="content">
1039 <pre><tt>$ rm -rf git-tutorial</tt></pre>
1040 </div></div>
1041 <div class="paragraph"><p>and it will be gone. There&#8217;s no external repository, and there&#8217;s no
1042 history outside the project you created.</p></div>
1043 </li>
1044 <li>
1046 if you want to move or duplicate a git repository, you can do so. There
1047 is <em>git clone</em> command, but if all you want to do is just to
1048 create a copy of your repository (with all the full history that
1049 went along with it), you can do so with a regular
1050 <tt>cp -a git-tutorial new-git-tutorial</tt>.
1051 </p>
1052 <div class="paragraph"><p>Note that when you&#8217;ve moved or copied a git repository, your git index
1053 file (which caches various information, notably some of the "stat"
1054 information for the files involved) will likely need to be refreshed.
1055 So after you do a <tt>cp -a</tt> to create a new copy, you&#8217;ll want to do</p></div>
1056 <div class="listingblock">
1057 <div class="content">
1058 <pre><tt>$ git update-index --refresh</tt></pre>
1059 </div></div>
1060 <div class="paragraph"><p>in the new repository to make sure that the index file is up-to-date.</p></div>
1061 </li>
1062 </ul></div>
1063 <div class="paragraph"><p>Note that the second point is true even across machines. You can
1064 duplicate a remote git repository with <strong>any</strong> regular copy mechanism, be it
1065 <em>scp</em>, <em>rsync</em> or <em>wget</em>.</p></div>
1066 <div class="paragraph"><p>When copying a remote repository, you&#8217;ll want to at a minimum update the
1067 index cache when you do this, and especially with other peoples'
1068 repositories you often want to make sure that the index cache is in some
1069 known state (you don&#8217;t know <strong>what</strong> they&#8217;ve done and not yet checked in),
1070 so usually you&#8217;ll precede the <em>git update-index</em> with a</p></div>
1071 <div class="listingblock">
1072 <div class="content">
1073 <pre><tt>$ git read-tree --reset HEAD
1074 $ git update-index --refresh</tt></pre>
1075 </div></div>
1076 <div class="paragraph"><p>which will force a total index re-build from the tree pointed to by <tt>HEAD</tt>.
1077 It resets the index contents to <tt>HEAD</tt>, and then the <em>git update-index</em>
1078 makes sure to match up all index entries with the checked-out files.
1079 If the original repository had uncommitted changes in its
1080 working tree, <tt>git update-index --refresh</tt> notices them and
1081 tells you they need to be updated.</p></div>
1082 <div class="paragraph"><p>The above can also be written as simply</p></div>
1083 <div class="listingblock">
1084 <div class="content">
1085 <pre><tt>$ git reset</tt></pre>
1086 </div></div>
1087 <div class="paragraph"><p>and in fact a lot of the common git command combinations can be scripted
1088 with the <tt>git xyz</tt> interfaces. You can learn things by just looking
1089 at what the various git scripts do. For example, <tt>git reset</tt> used to be
1090 the above two lines implemented in <em>git reset</em>, but some things like
1091 <em>git status</em> and <em>git commit</em> are slightly more complex scripts around
1092 the basic git commands.</p></div>
1093 <div class="paragraph"><p>Many (most?) public remote repositories will not contain any of
1094 the checked out files or even an index file, and will <strong>only</strong> contain the
1095 actual core git files. Such a repository usually doesn&#8217;t even have the
1096 <tt>.git</tt> subdirectory, but has all the git files directly in the
1097 repository.</p></div>
1098 <div class="paragraph"><p>To create your own local live copy of such a "raw" git repository, you&#8217;d
1099 first create your own subdirectory for the project, and then copy the
1100 raw repository contents into the <tt>.git</tt> directory. For example, to
1101 create your own copy of the git repository, you&#8217;d do the following</p></div>
1102 <div class="listingblock">
1103 <div class="content">
1104 <pre><tt>$ mkdir my-git
1105 $ cd my-git
1106 $ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git</tt></pre>
1107 </div></div>
1108 <div class="paragraph"><p>followed by</p></div>
1109 <div class="listingblock">
1110 <div class="content">
1111 <pre><tt>$ git read-tree HEAD</tt></pre>
1112 </div></div>
1113 <div class="paragraph"><p>to populate the index. However, now you have populated the index, and
1114 you have all the git internal files, but you will notice that you don&#8217;t
1115 actually have any of the working tree files to work on. To get
1116 those, you&#8217;d check them out with</p></div>
1117 <div class="listingblock">
1118 <div class="content">
1119 <pre><tt>$ git checkout-index -u -a</tt></pre>
1120 </div></div>
1121 <div class="paragraph"><p>where the <tt>-u</tt> flag means that you want the checkout to keep the index
1122 up-to-date (so that you don&#8217;t have to refresh it afterward), and the
1123 <tt>-a</tt> flag means "check out all files" (if you have a stale copy or an
1124 older version of a checked out tree you may also need to add the <tt>-f</tt>
1125 flag first, to tell <em>git checkout-index</em> to <strong>force</strong> overwriting of any old
1126 files).</p></div>
1127 <div class="paragraph"><p>Again, this can all be simplified with</p></div>
1128 <div class="listingblock">
1129 <div class="content">
1130 <pre><tt>$ git clone rsync://rsync.kernel.org/pub/scm/git/git.git/ my-git
1131 $ cd my-git
1132 $ git checkout</tt></pre>
1133 </div></div>
1134 <div class="paragraph"><p>which will end up doing all of the above for you.</p></div>
1135 <div class="paragraph"><p>You have now successfully copied somebody else&#8217;s (mine) remote
1136 repository, and checked it out.</p></div>
1137 </div>
1138 <h2 id="_creating_a_new_branch">Creating a new branch</h2>
1139 <div class="sectionbody">
1140 <div class="paragraph"><p>Branches in git are really nothing more than pointers into the git
1141 object database from within the <tt>.git/refs/</tt> subdirectory, and as we
1142 already discussed, the <tt>HEAD</tt> branch is nothing but a symlink to one of
1143 these object pointers.</p></div>
1144 <div class="paragraph"><p>You can at any time create a new branch by just picking an arbitrary
1145 point in the project history, and just writing the SHA1 name of that
1146 object into a file under <tt>.git/refs/heads/</tt>. You can use any filename you
1147 want (and indeed, subdirectories), but the convention is that the
1148 "normal" branch is called <tt>master</tt>. That&#8217;s just a convention, though,
1149 and nothing enforces it.</p></div>
1150 <div class="paragraph"><p>To show that as an example, let&#8217;s go back to the git-tutorial repository we
1151 used earlier, and create a branch in it. You do that by simply just
1152 saying that you want to check out a new branch:</p></div>
1153 <div class="listingblock">
1154 <div class="content">
1155 <pre><tt>$ git checkout -b mybranch</tt></pre>
1156 </div></div>
1157 <div class="paragraph"><p>will create a new branch based at the current <tt>HEAD</tt> position, and switch
1158 to it.</p></div>
1159 <div class="admonitionblock">
1160 <table><tr>
1161 <td class="icon">
1162 <div class="title">Note</div>
1163 </td>
1164 <td class="content">
1165 <div class="paragraph"><p>If you make the decision to start your new branch at some
1166 other point in the history than the current <tt>HEAD</tt>, you can do so by
1167 just telling <em>git checkout</em> what the base of the checkout would be.
1168 In other words, if you have an earlier tag or branch, you&#8217;d just do</p></div>
1169 <div class="listingblock">
1170 <div class="content">
1171 <pre><tt>$ git checkout -b mybranch earlier-commit</tt></pre>
1172 </div></div>
1173 <div class="paragraph"><p>and it would create the new branch <tt>mybranch</tt> at the earlier commit,
1174 and check out the state at that time.</p></div>
1175 </td>
1176 </tr></table>
1177 </div>
1178 <div class="paragraph"><p>You can always just jump back to your original <tt>master</tt> branch by doing</p></div>
1179 <div class="listingblock">
1180 <div class="content">
1181 <pre><tt>$ git checkout master</tt></pre>
1182 </div></div>
1183 <div class="paragraph"><p>(or any other branch-name, for that matter) and if you forget which
1184 branch you happen to be on, a simple</p></div>
1185 <div class="listingblock">
1186 <div class="content">
1187 <pre><tt>$ cat .git/HEAD</tt></pre>
1188 </div></div>
1189 <div class="paragraph"><p>will tell you where it&#8217;s pointing. To get the list of branches
1190 you have, you can say</p></div>
1191 <div class="listingblock">
1192 <div class="content">
1193 <pre><tt>$ git branch</tt></pre>
1194 </div></div>
1195 <div class="paragraph"><p>which used to be nothing more than a simple script around <tt>ls .git/refs/heads</tt>.
1196 There will be an asterisk in front of the branch you are currently on.</p></div>
1197 <div class="paragraph"><p>Sometimes you may wish to create a new branch <em>without</em> actually
1198 checking it out and switching to it. If so, just use the command</p></div>
1199 <div class="listingblock">
1200 <div class="content">
1201 <pre><tt>$ git branch &lt;branchname&gt; [startingpoint]</tt></pre>
1202 </div></div>
1203 <div class="paragraph"><p>which will simply <em>create</em> the branch, but will not do anything further.
1204 You can then later&#8201;&#8212;&#8201;once you decide that you want to actually develop
1205 on that branch&#8201;&#8212;&#8201;switch to that branch with a regular <em>git checkout</em>
1206 with the branchname as the argument.</p></div>
1207 </div>
1208 <h2 id="_merging_two_branches">Merging two branches</h2>
1209 <div class="sectionbody">
1210 <div class="paragraph"><p>One of the ideas of having a branch is that you do some (possibly
1211 experimental) work in it, and eventually merge it back to the main
1212 branch. So assuming you created the above <tt>mybranch</tt> that started out
1213 being the same as the original <tt>master</tt> branch, let&#8217;s make sure we&#8217;re in
1214 that branch, and do some work there.</p></div>
1215 <div class="listingblock">
1216 <div class="content">
1217 <pre><tt>$ git checkout mybranch
1218 $ echo "Work, work, work" &gt;&gt;hello
1219 $ git commit -m "Some work." -i hello</tt></pre>
1220 </div></div>
1221 <div class="paragraph"><p>Here, we just added another line to <tt>hello</tt>, and we used a shorthand for
1222 doing both <tt>git update-index hello</tt> and <tt>git commit</tt> by just giving the
1223 filename directly to <tt>git commit</tt>, with an <tt>-i</tt> flag (it tells
1224 git to <em>include</em> that file in addition to what you have done to
1225 the index file so far when making the commit). The <tt>-m</tt> flag is to give the
1226 commit log message from the command line.</p></div>
1227 <div class="paragraph"><p>Now, to make it a bit more interesting, let&#8217;s assume that somebody else
1228 does some work in the original branch, and simulate that by going back
1229 to the master branch, and editing the same file differently there:</p></div>
1230 <div class="listingblock">
1231 <div class="content">
1232 <pre><tt>$ git checkout master</tt></pre>
1233 </div></div>
1234 <div class="paragraph"><p>Here, take a moment to look at the contents of <tt>hello</tt>, and notice how they
1235 don&#8217;t contain the work we just did in <tt>mybranch</tt>&#8201;&#8212;&#8201;because that work
1236 hasn&#8217;t happened in the <tt>master</tt> branch at all. Then do</p></div>
1237 <div class="listingblock">
1238 <div class="content">
1239 <pre><tt>$ echo "Play, play, play" &gt;&gt;hello
1240 $ echo "Lots of fun" &gt;&gt;example
1241 $ git commit -m "Some fun." -i hello example</tt></pre>
1242 </div></div>
1243 <div class="paragraph"><p>since the master branch is obviously in a much better mood.</p></div>
1244 <div class="paragraph"><p>Now, you&#8217;ve got two branches, and you decide that you want to merge the
1245 work done. Before we do that, let&#8217;s introduce a cool graphical tool that
1246 helps you view what&#8217;s going on:</p></div>
1247 <div class="listingblock">
1248 <div class="content">
1249 <pre><tt>$ gitk --all</tt></pre>
1250 </div></div>
1251 <div class="paragraph"><p>will show you graphically both of your branches (that&#8217;s what the <tt>--all</tt>
1252 means: normally it will just show you your current <tt>HEAD</tt>) and their
1253 histories. You can also see exactly how they came to be from a common
1254 source.</p></div>
1255 <div class="paragraph"><p>Anyway, let&#8217;s exit <em>gitk</em> (<tt>^Q</tt> or the File menu), and decide that we want
1256 to merge the work we did on the <tt>mybranch</tt> branch into the <tt>master</tt>
1257 branch (which is currently our <tt>HEAD</tt> too). To do that, there&#8217;s a nice
1258 script called <em>git merge</em>, which wants to know which branches you want
1259 to resolve and what the merge is all about:</p></div>
1260 <div class="listingblock">
1261 <div class="content">
1262 <pre><tt>$ git merge -m "Merge work in mybranch" mybranch</tt></pre>
1263 </div></div>
1264 <div class="paragraph"><p>where the first argument is going to be used as the commit message if
1265 the merge can be resolved automatically.</p></div>
1266 <div class="paragraph"><p>Now, in this case we&#8217;ve intentionally created a situation where the
1267 merge will need to be fixed up by hand, though, so git will do as much
1268 of it as it can automatically (which in this case is just merge the <tt>example</tt>
1269 file, which had no differences in the <tt>mybranch</tt> branch), and say:</p></div>
1270 <div class="listingblock">
1271 <div class="content">
1272 <pre><tt> Auto-merging hello
1273 CONFLICT (content): Merge conflict in hello
1274 Automatic merge failed; fix conflicts and then commit the result.</tt></pre>
1275 </div></div>
1276 <div class="paragraph"><p>It tells you that it did an "Automatic merge", which
1277 failed due to conflicts in <tt>hello</tt>.</p></div>
1278 <div class="paragraph"><p>Not to worry. It left the (trivial) conflict in <tt>hello</tt> in the same form you
1279 should already be well used to if you&#8217;ve ever used CVS, so let&#8217;s just
1280 open <tt>hello</tt> in our editor (whatever that may be), and fix it up somehow.
1281 I&#8217;d suggest just making it so that <tt>hello</tt> contains all four lines:</p></div>
1282 <div class="listingblock">
1283 <div class="content">
1284 <pre><tt>Hello World
1285 It's a new day for git
1286 Play, play, play
1287 Work, work, work</tt></pre>
1288 </div></div>
1289 <div class="paragraph"><p>and once you&#8217;re happy with your manual merge, just do a</p></div>
1290 <div class="listingblock">
1291 <div class="content">
1292 <pre><tt>$ git commit -i hello</tt></pre>
1293 </div></div>
1294 <div class="paragraph"><p>which will very loudly warn you that you&#8217;re now committing a merge
1295 (which is correct, so never mind), and you can write a small merge
1296 message about your adventures in <em>git merge</em>-land.</p></div>
1297 <div class="paragraph"><p>After you&#8217;re done, start up <tt>gitk --all</tt> to see graphically what the
1298 history looks like. Notice that <tt>mybranch</tt> still exists, and you can
1299 switch to it, and continue to work with it if you want to. The
1300 <tt>mybranch</tt> branch will not contain the merge, but next time you merge it
1301 from the <tt>master</tt> branch, git will know how you merged it, so you&#8217;ll not
1302 have to do <em>that</em> merge again.</p></div>
1303 <div class="paragraph"><p>Another useful tool, especially if you do not always work in X-Window
1304 environment, is <tt>git show-branch</tt>.</p></div>
1305 <div class="listingblock">
1306 <div class="content">
1307 <pre><tt>$ git show-branch --topo-order --more=1 master mybranch
1308 * [master] Merge work in mybranch
1309 ! [mybranch] Some work.
1311 - [master] Merge work in mybranch
1312 *+ [mybranch] Some work.
1313 * [master^] Some fun.</tt></pre>
1314 </div></div>
1315 <div class="paragraph"><p>The first two lines indicate that it is showing the two branches
1316 and the first line of the commit log message from their
1317 top-of-the-tree commits, you are currently on <tt>master</tt> branch
1318 (notice the asterisk <tt>&#42;</tt> character), and the first column for
1319 the later output lines is used to show commits contained in the
1320 <tt>master</tt> branch, and the second column for the <tt>mybranch</tt>
1321 branch. Three commits are shown along with their log messages.
1322 All of them have non blank characters in the first column (<tt>&#42;</tt>
1323 shows an ordinary commit on the current branch, <tt>-</tt> is a merge commit), which
1324 means they are now part of the <tt>master</tt> branch. Only the "Some
1325 work" commit has the plus <tt>+</tt> character in the second column,
1326 because <tt>mybranch</tt> has not been merged to incorporate these
1327 commits from the master branch. The string inside brackets
1328 before the commit log message is a short name you can use to
1329 name the commit. In the above example, <em>master</em> and <em>mybranch</em>
1330 are branch heads. <em>master^</em> is the first parent of <em>master</em>
1331 branch head. Please see <a href="gitrevisions.html">gitrevisions(7)</a> if you want to
1332 see more complex cases.</p></div>
1333 <div class="admonitionblock">
1334 <table><tr>
1335 <td class="icon">
1336 <div class="title">Note</div>
1337 </td>
1338 <td class="content">Without the <em>--more=1</em> option, <em>git show-branch</em> would not output the
1339 <em>[master^]</em> commit, as <em>[mybranch]</em> commit is a common ancestor of
1340 both <em>master</em> and <em>mybranch</em> tips. Please see <a href="git-show-branch.html">git-show-branch(1)</a>
1341 for details.</td>
1342 </tr></table>
1343 </div>
1344 <div class="admonitionblock">
1345 <table><tr>
1346 <td class="icon">
1347 <div class="title">Note</div>
1348 </td>
1349 <td class="content">If there were more commits on the <em>master</em> branch after the merge, the
1350 merge commit itself would not be shown by <em>git show-branch</em> by
1351 default. You would need to provide <em>--sparse</em> option to make the
1352 merge commit visible in this case.</td>
1353 </tr></table>
1354 </div>
1355 <div class="paragraph"><p>Now, let&#8217;s pretend you are the one who did all the work in
1356 <tt>mybranch</tt>, and the fruit of your hard work has finally been merged
1357 to the <tt>master</tt> branch. Let&#8217;s go back to <tt>mybranch</tt>, and run
1358 <em>git merge</em> to get the "upstream changes" back to your branch.</p></div>
1359 <div class="listingblock">
1360 <div class="content">
1361 <pre><tt>$ git checkout mybranch
1362 $ git merge -m "Merge upstream changes." master</tt></pre>
1363 </div></div>
1364 <div class="paragraph"><p>This outputs something like this (the actual commit object names
1365 would be different)</p></div>
1366 <div class="listingblock">
1367 <div class="content">
1368 <pre><tt>Updating from ae3a2da... to a80b4aa....
1369 Fast-forward (no commit created; -m option ignored)
1370 example | 1 +
1371 hello | 1 +
1372 2 files changed, 2 insertions(+), 0 deletions(-)</tt></pre>
1373 </div></div>
1374 <div class="paragraph"><p>Because your branch did not contain anything more than what had
1375 already been merged into the <tt>master</tt> branch, the merge operation did
1376 not actually do a merge. Instead, it just updated the top of
1377 the tree of your branch to that of the <tt>master</tt> branch. This is
1378 often called <em>fast-forward</em> merge.</p></div>
1379 <div class="paragraph"><p>You can run <tt>gitk --all</tt> again to see how the commit ancestry
1380 looks like, or run <em>show-branch</em>, which tells you this.</p></div>
1381 <div class="listingblock">
1382 <div class="content">
1383 <pre><tt>$ git show-branch master mybranch
1384 ! [master] Merge work in mybranch
1385 * [mybranch] Merge work in mybranch
1387 -- [master] Merge work in mybranch</tt></pre>
1388 </div></div>
1389 </div>
1390 <h2 id="_merging_external_work">Merging external work</h2>
1391 <div class="sectionbody">
1392 <div class="paragraph"><p>It&#8217;s usually much more common that you merge with somebody else than
1393 merging with your own branches, so it&#8217;s worth pointing out that git
1394 makes that very easy too, and in fact, it&#8217;s not that different from
1395 doing a <em>git merge</em>. In fact, a remote merge ends up being nothing
1396 more than "fetch the work from a remote repository into a temporary tag"
1397 followed by a <em>git merge</em>.</p></div>
1398 <div class="paragraph"><p>Fetching from a remote repository is done by, unsurprisingly,
1399 <em>git fetch</em>:</p></div>
1400 <div class="listingblock">
1401 <div class="content">
1402 <pre><tt>$ git fetch &lt;remote-repository&gt;</tt></pre>
1403 </div></div>
1404 <div class="paragraph"><p>One of the following transports can be used to name the
1405 repository to download from:</p></div>
1406 <div class="dlist"><dl>
1407 <dt class="hdlist1">
1408 Rsync
1409 </dt>
1410 <dd>
1412 <tt>rsync://remote.machine/path/to/repo.git/</tt>
1413 </p>
1414 <div class="paragraph"><p>Rsync transport is usable for both uploading and downloading,
1415 but is completely unaware of what git does, and can produce
1416 unexpected results when you download from the public repository
1417 while the repository owner is uploading into it via <tt>rsync</tt>
1418 transport. Most notably, it could update the files under
1419 <tt>refs/</tt> which holds the object name of the topmost commits
1420 before uploading the files in <tt>objects/</tt>&#8201;&#8212;&#8201;the downloader would
1421 obtain head commit object name while that object itself is still
1422 not available in the repository. For this reason, it is
1423 considered deprecated.</p></div>
1424 </dd>
1425 <dt class="hdlist1">
1427 </dt>
1428 <dd>
1430 <tt>remote.machine:/path/to/repo.git/</tt> or
1431 </p>
1432 <div class="paragraph"><p><tt>ssh://remote.machine/path/to/repo.git/</tt></p></div>
1433 <div class="paragraph"><p>This transport can be used for both uploading and downloading,
1434 and requires you to have a log-in privilege over <tt>ssh</tt> to the
1435 remote machine. It finds out the set of objects the other side
1436 lacks by exchanging the head commits both ends have and
1437 transfers (close to) minimum set of objects. It is by far the
1438 most efficient way to exchange git objects between repositories.</p></div>
1439 </dd>
1440 <dt class="hdlist1">
1441 Local directory
1442 </dt>
1443 <dd>
1445 <tt>/path/to/repo.git/</tt>
1446 </p>
1447 <div class="paragraph"><p>This transport is the same as SSH transport but uses <em>sh</em> to run
1448 both ends on the local machine instead of running other end on
1449 the remote machine via <em>ssh</em>.</p></div>
1450 </dd>
1451 <dt class="hdlist1">
1452 git Native
1453 </dt>
1454 <dd>
1456 <tt>git://remote.machine/path/to/repo.git/</tt>
1457 </p>
1458 <div class="paragraph"><p>This transport was designed for anonymous downloading. Like SSH
1459 transport, it finds out the set of objects the downstream side
1460 lacks and transfers (close to) minimum set of objects.</p></div>
1461 </dd>
1462 <dt class="hdlist1">
1463 HTTP(S)
1464 </dt>
1465 <dd>
1467 <tt>http://remote.machine/path/to/repo.git/</tt>
1468 </p>
1469 <div class="paragraph"><p>Downloader from http and https URL
1470 first obtains the topmost commit object name from the remote site
1471 by looking at the specified refname under <tt>repo.git/refs/</tt> directory,
1472 and then tries to obtain the
1473 commit object by downloading from <tt>repo.git/objects/xx/xxx&#8230;</tt>
1474 using the object name of that commit object. Then it reads the
1475 commit object to find out its parent commits and the associate
1476 tree object; it repeats this process until it gets all the
1477 necessary objects. Because of this behavior, they are
1478 sometimes also called <em>commit walkers</em>.</p></div>
1479 <div class="paragraph"><p>The <em>commit walkers</em> are sometimes also called <em>dumb
1480 transports</em>, because they do not require any git aware smart
1481 server like git Native transport does. Any stock HTTP server
1482 that does not even support directory index would suffice. But
1483 you must prepare your repository with <em>git update-server-info</em>
1484 to help dumb transport downloaders.</p></div>
1485 </dd>
1486 </dl></div>
1487 <div class="paragraph"><p>Once you fetch from the remote repository, you <tt>merge</tt> that
1488 with your current branch.</p></div>
1489 <div class="paragraph"><p>However&#8201;&#8212;&#8201;it&#8217;s such a common thing to <tt>fetch</tt> and then
1490 immediately <tt>merge</tt>, that it&#8217;s called <tt>git pull</tt>, and you can
1491 simply do</p></div>
1492 <div class="listingblock">
1493 <div class="content">
1494 <pre><tt>$ git pull &lt;remote-repository&gt;</tt></pre>
1495 </div></div>
1496 <div class="paragraph"><p>and optionally give a branch-name for the remote end as a second
1497 argument.</p></div>
1498 <div class="admonitionblock">
1499 <table><tr>
1500 <td class="icon">
1501 <div class="title">Note</div>
1502 </td>
1503 <td class="content">You could do without using any branches at all, by
1504 keeping as many local repositories as you would like to have
1505 branches, and merging between them with <em>git pull</em>, just like
1506 you merge between branches. The advantage of this approach is
1507 that it lets you keep a set of files for each <tt>branch</tt> checked
1508 out and you may find it easier to switch back and forth if you
1509 juggle multiple lines of development simultaneously. Of
1510 course, you will pay the price of more disk usage to hold
1511 multiple working trees, but disk space is cheap these days.</td>
1512 </tr></table>
1513 </div>
1514 <div class="paragraph"><p>It is likely that you will be pulling from the same remote
1515 repository from time to time. As a short hand, you can store
1516 the remote repository URL in the local repository&#8217;s config file
1517 like this:</p></div>
1518 <div class="listingblock">
1519 <div class="content">
1520 <pre><tt>$ git config remote.linus.url http://www.kernel.org/pub/scm/git/git.git/</tt></pre>
1521 </div></div>
1522 <div class="paragraph"><p>and use the "linus" keyword with <em>git pull</em> instead of the full URL.</p></div>
1523 <div class="paragraph"><p>Examples.</p></div>
1524 <div class="olist arabic"><ol class="arabic">
1525 <li>
1527 <tt>git pull linus</tt>
1528 </p>
1529 </li>
1530 <li>
1532 <tt>git pull linus tag v0.99.1</tt>
1533 </p>
1534 </li>
1535 </ol></div>
1536 <div class="paragraph"><p>the above are equivalent to:</p></div>
1537 <div class="olist arabic"><ol class="arabic">
1538 <li>
1540 <tt>git pull <a href="http://www.kernel.org/pub/scm/git/git.git/">http://www.kernel.org/pub/scm/git/git.git/</a> HEAD</tt>
1541 </p>
1542 </li>
1543 <li>
1545 <tt>git pull <a href="http://www.kernel.org/pub/scm/git/git.git/">http://www.kernel.org/pub/scm/git/git.git/</a> tag v0.99.1</tt>
1546 </p>
1547 </li>
1548 </ol></div>
1549 </div>
1550 <h2 id="_how_does_the_merge_work">How does the merge work?</h2>
1551 <div class="sectionbody">
1552 <div class="paragraph"><p>We said this tutorial shows what plumbing does to help you cope
1553 with the porcelain that isn&#8217;t flushing, but we so far did not
1554 talk about how the merge really works. If you are following
1555 this tutorial the first time, I&#8217;d suggest to skip to "Publishing
1556 your work" section and come back here later.</p></div>
1557 <div class="paragraph"><p>OK, still with me? To give us an example to look at, let&#8217;s go
1558 back to the earlier repository with "hello" and "example" file,
1559 and bring ourselves back to the pre-merge state:</p></div>
1560 <div class="listingblock">
1561 <div class="content">
1562 <pre><tt>$ git show-branch --more=2 master mybranch
1563 ! [master] Merge work in mybranch
1564 * [mybranch] Merge work in mybranch
1566 -- [master] Merge work in mybranch
1567 +* [master^2] Some work.
1568 +* [master^] Some fun.</tt></pre>
1569 </div></div>
1570 <div class="paragraph"><p>Remember, before running <em>git merge</em>, our <tt>master</tt> head was at
1571 "Some fun." commit, while our <tt>mybranch</tt> head was at "Some
1572 work." commit.</p></div>
1573 <div class="listingblock">
1574 <div class="content">
1575 <pre><tt>$ git checkout mybranch
1576 $ git reset --hard master^2
1577 $ git checkout master
1578 $ git reset --hard master^</tt></pre>
1579 </div></div>
1580 <div class="paragraph"><p>After rewinding, the commit structure should look like this:</p></div>
1581 <div class="listingblock">
1582 <div class="content">
1583 <pre><tt>$ git show-branch
1584 * [master] Some fun.
1585 ! [mybranch] Some work.
1587 * [master] Some fun.
1588 + [mybranch] Some work.
1589 *+ [master^] Initial commit</tt></pre>
1590 </div></div>
1591 <div class="paragraph"><p>Now we are ready to experiment with the merge by hand.</p></div>
1592 <div class="paragraph"><p><tt>git merge</tt> command, when merging two branches, uses 3-way merge
1593 algorithm. First, it finds the common ancestor between them.
1594 The command it uses is <em>git merge-base</em>:</p></div>
1595 <div class="listingblock">
1596 <div class="content">
1597 <pre><tt>$ mb=$(git merge-base HEAD mybranch)</tt></pre>
1598 </div></div>
1599 <div class="paragraph"><p>The command writes the commit object name of the common ancestor
1600 to the standard output, so we captured its output to a variable,
1601 because we will be using it in the next step. By the way, the common
1602 ancestor commit is the "Initial commit" commit in this case. You can
1603 tell it by:</p></div>
1604 <div class="listingblock">
1605 <div class="content">
1606 <pre><tt>$ git name-rev --name-only --tags $mb
1607 my-first-tag</tt></pre>
1608 </div></div>
1609 <div class="paragraph"><p>After finding out a common ancestor commit, the second step is
1610 this:</p></div>
1611 <div class="listingblock">
1612 <div class="content">
1613 <pre><tt>$ git read-tree -m -u $mb HEAD mybranch</tt></pre>
1614 </div></div>
1615 <div class="paragraph"><p>This is the same <em>git read-tree</em> command we have already seen,
1616 but it takes three trees, unlike previous examples. This reads
1617 the contents of each tree into different <em>stage</em> in the index
1618 file (the first tree goes to stage 1, the second to stage 2,
1619 etc.). After reading three trees into three stages, the paths
1620 that are the same in all three stages are <em>collapsed</em> into stage
1621 0. Also paths that are the same in two of three stages are
1622 collapsed into stage 0, taking the SHA1 from either stage 2 or
1623 stage 3, whichever is different from stage 1 (i.e. only one side
1624 changed from the common ancestor).</p></div>
1625 <div class="paragraph"><p>After <em>collapsing</em> operation, paths that are different in three
1626 trees are left in non-zero stages. At this point, you can
1627 inspect the index file with this command:</p></div>
1628 <div class="listingblock">
1629 <div class="content">
1630 <pre><tt>$ git ls-files --stage
1631 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1632 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1633 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1634 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello</tt></pre>
1635 </div></div>
1636 <div class="paragraph"><p>In our example of only two files, we did not have unchanged
1637 files so only <em>example</em> resulted in collapsing. But in real-life
1638 large projects, when only a small number of files change in one commit,
1639 this <em>collapsing</em> tends to trivially merge most of the paths
1640 fairly quickly, leaving only a handful of real changes in non-zero
1641 stages.</p></div>
1642 <div class="paragraph"><p>To look at only non-zero stages, use <tt>--unmerged</tt> flag:</p></div>
1643 <div class="listingblock">
1644 <div class="content">
1645 <pre><tt>$ git ls-files --unmerged
1646 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1647 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1648 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello</tt></pre>
1649 </div></div>
1650 <div class="paragraph"><p>The next step of merging is to merge these three versions of the
1651 file, using 3-way merge. This is done by giving
1652 <em>git merge-one-file</em> command as one of the arguments to
1653 <em>git merge-index</em> command:</p></div>
1654 <div class="listingblock">
1655 <div class="content">
1656 <pre><tt>$ git merge-index git-merge-one-file hello
1657 Auto-merging hello
1658 ERROR: Merge conflict in hello
1659 fatal: merge program failed</tt></pre>
1660 </div></div>
1661 <div class="paragraph"><p><em>git merge-one-file</em> script is called with parameters to
1662 describe those three versions, and is responsible to leave the
1663 merge results in the working tree.
1664 It is a fairly straightforward shell script, and
1665 eventually calls <em>merge</em> program from RCS suite to perform a
1666 file-level 3-way merge. In this case, <em>merge</em> detects
1667 conflicts, and the merge result with conflict marks is left in
1668 the working tree.. This can be seen if you run <tt>ls-files
1669 --stage</tt> again at this point:</p></div>
1670 <div class="listingblock">
1671 <div class="content">
1672 <pre><tt>$ git ls-files --stage
1673 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1674 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1675 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1676 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello</tt></pre>
1677 </div></div>
1678 <div class="paragraph"><p>This is the state of the index file and the working file after
1679 <em>git merge</em> returns control back to you, leaving the conflicting
1680 merge for you to resolve. Notice that the path <tt>hello</tt> is still
1681 unmerged, and what you see with <em>git diff</em> at this point is
1682 differences since stage 2 (i.e. your version).</p></div>
1683 </div>
1684 <h2 id="_publishing_your_work">Publishing your work</h2>
1685 <div class="sectionbody">
1686 <div class="paragraph"><p>So, we can use somebody else&#8217;s work from a remote repository, but
1687 how can <strong>you</strong> prepare a repository to let other people pull from
1688 it?</p></div>
1689 <div class="paragraph"><p>You do your real work in your working tree that has your
1690 primary repository hanging under it as its <tt>.git</tt> subdirectory.
1691 You <strong>could</strong> make that repository accessible remotely and ask
1692 people to pull from it, but in practice that is not the way
1693 things are usually done. A recommended way is to have a public
1694 repository, make it reachable by other people, and when the
1695 changes you made in your primary working tree are in good shape,
1696 update the public repository from it. This is often called
1697 <em>pushing</em>.</p></div>
1698 <div class="admonitionblock">
1699 <table><tr>
1700 <td class="icon">
1701 <div class="title">Note</div>
1702 </td>
1703 <td class="content">This public repository could further be mirrored, and that is
1704 how git repositories at <tt>kernel.org</tt> are managed.</td>
1705 </tr></table>
1706 </div>
1707 <div class="paragraph"><p>Publishing the changes from your local (private) repository to
1708 your remote (public) repository requires a write privilege on
1709 the remote machine. You need to have an SSH account there to
1710 run a single command, <em>git-receive-pack</em>.</p></div>
1711 <div class="paragraph"><p>First, you need to create an empty repository on the remote
1712 machine that will house your public repository. This empty
1713 repository will be populated and be kept up-to-date by pushing
1714 into it later. Obviously, this repository creation needs to be
1715 done only once.</p></div>
1716 <div class="admonitionblock">
1717 <table><tr>
1718 <td class="icon">
1719 <div class="title">Note</div>
1720 </td>
1721 <td class="content"><em>git push</em> uses a pair of commands,
1722 <em>git send-pack</em> on your local machine, and <em>git-receive-pack</em>
1723 on the remote machine. The communication between the two over
1724 the network internally uses an SSH connection.</td>
1725 </tr></table>
1726 </div>
1727 <div class="paragraph"><p>Your private repository&#8217;s git directory is usually <tt>.git</tt>, but
1728 your public repository is often named after the project name,
1729 i.e. <tt>&lt;project&gt;.git</tt>. Let&#8217;s create such a public repository for
1730 project <tt>my-git</tt>. After logging into the remote machine, create
1731 an empty directory:</p></div>
1732 <div class="listingblock">
1733 <div class="content">
1734 <pre><tt>$ mkdir my-git.git</tt></pre>
1735 </div></div>
1736 <div class="paragraph"><p>Then, make that directory into a git repository by running
1737 <em>git init</em>, but this time, since its name is not the usual
1738 <tt>.git</tt>, we do things slightly differently:</p></div>
1739 <div class="listingblock">
1740 <div class="content">
1741 <pre><tt>$ GIT_DIR=my-git.git git init</tt></pre>
1742 </div></div>
1743 <div class="paragraph"><p>Make sure this directory is available for others you want your
1744 changes to be pulled via the transport of your choice. Also
1745 you need to make sure that you have the <em>git-receive-pack</em>
1746 program on the <tt>$PATH</tt>.</p></div>
1747 <div class="admonitionblock">
1748 <table><tr>
1749 <td class="icon">
1750 <div class="title">Note</div>
1751 </td>
1752 <td class="content">Many installations of sshd do not invoke your shell as the login
1753 shell when you directly run programs; what this means is that if
1754 your login shell is <em>bash</em>, only <tt>.bashrc</tt> is read and not
1755 <tt>.bash_profile</tt>. As a workaround, make sure <tt>.bashrc</tt> sets up
1756 <tt>$PATH</tt> so that you can run <em>git-receive-pack</em> program.</td>
1757 </tr></table>
1758 </div>
1759 <div class="admonitionblock">
1760 <table><tr>
1761 <td class="icon">
1762 <div class="title">Note</div>
1763 </td>
1764 <td class="content">If you plan to publish this repository to be accessed over http,
1765 you should do <tt>mv my-git.git/hooks/post-update.sample
1766 my-git.git/hooks/post-update</tt> at this point.
1767 This makes sure that every time you push into this
1768 repository, <tt>git update-server-info</tt> is run.</td>
1769 </tr></table>
1770 </div>
1771 <div class="paragraph"><p>Your "public repository" is now ready to accept your changes.
1772 Come back to the machine you have your private repository. From
1773 there, run this command:</p></div>
1774 <div class="listingblock">
1775 <div class="content">
1776 <pre><tt>$ git push &lt;public-host&gt;:/path/to/my-git.git master</tt></pre>
1777 </div></div>
1778 <div class="paragraph"><p>This synchronizes your public repository to match the named
1779 branch head (i.e. <tt>master</tt> in this case) and objects reachable
1780 from them in your current repository.</p></div>
1781 <div class="paragraph"><p>As a real example, this is how I update my public git
1782 repository. Kernel.org mirror network takes care of the
1783 propagation to other publicly visible machines:</p></div>
1784 <div class="listingblock">
1785 <div class="content">
1786 <pre><tt>$ git push master.kernel.org:/pub/scm/git/git.git/</tt></pre>
1787 </div></div>
1788 </div>
1789 <h2 id="_packing_your_repository">Packing your repository</h2>
1790 <div class="sectionbody">
1791 <div class="paragraph"><p>Earlier, we saw that one file under <tt>.git/objects/??/</tt> directory
1792 is stored for each git object you create. This representation
1793 is efficient to create atomically and safely, but
1794 not so convenient to transport over the network. Since git objects are
1795 immutable once they are created, there is a way to optimize the
1796 storage by "packing them together". The command</p></div>
1797 <div class="listingblock">
1798 <div class="content">
1799 <pre><tt>$ git repack</tt></pre>
1800 </div></div>
1801 <div class="paragraph"><p>will do it for you. If you followed the tutorial examples, you
1802 would have accumulated about 17 objects in <tt>.git/objects/??/</tt>
1803 directories by now. <em>git repack</em> tells you how many objects it
1804 packed, and stores the packed file in <tt>.git/objects/pack</tt>
1805 directory.</p></div>
1806 <div class="admonitionblock">
1807 <table><tr>
1808 <td class="icon">
1809 <div class="title">Note</div>
1810 </td>
1811 <td class="content">You will see two files, <tt>pack-&#42;.pack</tt> and <tt>pack-&#42;.idx</tt>,
1812 in <tt>.git/objects/pack</tt> directory. They are closely related to
1813 each other, and if you ever copy them by hand to a different
1814 repository for whatever reason, you should make sure you copy
1815 them together. The former holds all the data from the objects
1816 in the pack, and the latter holds the index for random
1817 access.</td>
1818 </tr></table>
1819 </div>
1820 <div class="paragraph"><p>If you are paranoid, running <em>git verify-pack</em> command would
1821 detect if you have a corrupt pack, but do not worry too much.
1822 Our programs are always perfect ;-).</p></div>
1823 <div class="paragraph"><p>Once you have packed objects, you do not need to leave the
1824 unpacked objects that are contained in the pack file anymore.</p></div>
1825 <div class="listingblock">
1826 <div class="content">
1827 <pre><tt>$ git prune-packed</tt></pre>
1828 </div></div>
1829 <div class="paragraph"><p>would remove them for you.</p></div>
1830 <div class="paragraph"><p>You can try running <tt>find .git/objects -type f</tt> before and after
1831 you run <tt>git prune-packed</tt> if you are curious. Also <tt>git
1832 count-objects</tt> would tell you how many unpacked objects are in
1833 your repository and how much space they are consuming.</p></div>
1834 <div class="admonitionblock">
1835 <table><tr>
1836 <td class="icon">
1837 <div class="title">Note</div>
1838 </td>
1839 <td class="content"><tt>git pull</tt> is slightly cumbersome for HTTP transport, as a
1840 packed repository may contain relatively few objects in a
1841 relatively large pack. If you expect many HTTP pulls from your
1842 public repository you might want to repack &amp; prune often, or
1843 never.</td>
1844 </tr></table>
1845 </div>
1846 <div class="paragraph"><p>If you run <tt>git repack</tt> again at this point, it will say
1847 "Nothing new to pack.". Once you continue your development and
1848 accumulate the changes, running <tt>git repack</tt> again will create a
1849 new pack, that contains objects created since you packed your
1850 repository the last time. We recommend that you pack your project
1851 soon after the initial import (unless you are starting your
1852 project from scratch), and then run <tt>git repack</tt> every once in a
1853 while, depending on how active your project is.</p></div>
1854 <div class="paragraph"><p>When a repository is synchronized via <tt>git push</tt> and <tt>git pull</tt>
1855 objects packed in the source repository are usually stored
1856 unpacked in the destination, unless rsync transport is used.
1857 While this allows you to use different packing strategies on
1858 both ends, it also means you may need to repack both
1859 repositories every once in a while.</p></div>
1860 </div>
1861 <h2 id="_working_with_others">Working with Others</h2>
1862 <div class="sectionbody">
1863 <div class="paragraph"><p>Although git is a truly distributed system, it is often
1864 convenient to organize your project with an informal hierarchy
1865 of developers. Linux kernel development is run this way. There
1866 is a nice illustration (page 17, "Merges to Mainline") in
1867 <a href="http://www.xenotime.net/linux/mentor/linux-mentoring-2006.pdf">Randy Dunlap&#8217;s presentation</a>.</p></div>
1868 <div class="paragraph"><p>It should be stressed that this hierarchy is purely <strong>informal</strong>.
1869 There is nothing fundamental in git that enforces the "chain of
1870 patch flow" this hierarchy implies. You do not have to pull
1871 from only one remote repository.</p></div>
1872 <div class="paragraph"><p>A recommended workflow for a "project lead" goes like this:</p></div>
1873 <div class="olist arabic"><ol class="arabic">
1874 <li>
1876 Prepare your primary repository on your local machine. Your
1877 work is done there.
1878 </p>
1879 </li>
1880 <li>
1882 Prepare a public repository accessible to others.
1883 </p>
1884 <div class="paragraph"><p>If other people are pulling from your repository over dumb
1885 transport protocols (HTTP), you need to keep this repository
1886 <em>dumb transport friendly</em>. After <tt>git init</tt>,
1887 <tt>$GIT_DIR/hooks/post-update.sample</tt> copied from the standard templates
1888 would contain a call to <em>git update-server-info</em>
1889 but you need to manually enable the hook with
1890 <tt>mv post-update.sample post-update</tt>. This makes sure
1891 <em>git update-server-info</em> keeps the necessary files up-to-date.</p></div>
1892 </li>
1893 <li>
1895 Push into the public repository from your primary
1896 repository.
1897 </p>
1898 </li>
1899 <li>
1901 <em>git repack</em> the public repository. This establishes a big
1902 pack that contains the initial set of objects as the
1903 baseline, and possibly <em>git prune</em> if the transport
1904 used for pulling from your repository supports packed
1905 repositories.
1906 </p>
1907 </li>
1908 <li>
1910 Keep working in your primary repository. Your changes
1911 include modifications of your own, patches you receive via
1912 e-mails, and merges resulting from pulling the "public"
1913 repositories of your "subsystem maintainers".
1914 </p>
1915 <div class="paragraph"><p>You can repack this private repository whenever you feel like.</p></div>
1916 </li>
1917 <li>
1919 Push your changes to the public repository, and announce it
1920 to the public.
1921 </p>
1922 </li>
1923 <li>
1925 Every once in a while, <em>git repack</em> the public repository.
1926 Go back to step 5. and continue working.
1927 </p>
1928 </li>
1929 </ol></div>
1930 <div class="paragraph"><p>A recommended work cycle for a "subsystem maintainer" who works
1931 on that project and has an own "public repository" goes like this:</p></div>
1932 <div class="olist arabic"><ol class="arabic">
1933 <li>
1935 Prepare your work repository, by <em>git clone</em> the public
1936 repository of the "project lead". The URL used for the
1937 initial cloning is stored in the remote.origin.url
1938 configuration variable.
1939 </p>
1940 </li>
1941 <li>
1943 Prepare a public repository accessible to others, just like
1944 the "project lead" person does.
1945 </p>
1946 </li>
1947 <li>
1949 Copy over the packed files from "project lead" public
1950 repository to your public repository, unless the "project
1951 lead" repository lives on the same machine as yours. In the
1952 latter case, you can use <tt>objects/info/alternates</tt> file to
1953 point at the repository you are borrowing from.
1954 </p>
1955 </li>
1956 <li>
1958 Push into the public repository from your primary
1959 repository. Run <em>git repack</em>, and possibly <em>git prune</em> if the
1960 transport used for pulling from your repository supports
1961 packed repositories.
1962 </p>
1963 </li>
1964 <li>
1966 Keep working in your primary repository. Your changes
1967 include modifications of your own, patches you receive via
1968 e-mails, and merges resulting from pulling the "public"
1969 repositories of your "project lead" and possibly your
1970 "sub-subsystem maintainers".
1971 </p>
1972 <div class="paragraph"><p>You can repack this private repository whenever you feel
1973 like.</p></div>
1974 </li>
1975 <li>
1977 Push your changes to your public repository, and ask your
1978 "project lead" and possibly your "sub-subsystem
1979 maintainers" to pull from it.
1980 </p>
1981 </li>
1982 <li>
1984 Every once in a while, <em>git repack</em> the public repository.
1985 Go back to step 5. and continue working.
1986 </p>
1987 </li>
1988 </ol></div>
1989 <div class="paragraph"><p>A recommended work cycle for an "individual developer" who does
1990 not have a "public" repository is somewhat different. It goes
1991 like this:</p></div>
1992 <div class="olist arabic"><ol class="arabic">
1993 <li>
1995 Prepare your work repository, by <em>git clone</em> the public
1996 repository of the "project lead" (or a "subsystem
1997 maintainer", if you work on a subsystem). The URL used for
1998 the initial cloning is stored in the remote.origin.url
1999 configuration variable.
2000 </p>
2001 </li>
2002 <li>
2004 Do your work in your repository on <em>master</em> branch.
2005 </p>
2006 </li>
2007 <li>
2009 Run <tt>git fetch origin</tt> from the public repository of your
2010 upstream every once in a while. This does only the first
2011 half of <tt>git pull</tt> but does not merge. The head of the
2012 public repository is stored in <tt>.git/refs/remotes/origin/master</tt>.
2013 </p>
2014 </li>
2015 <li>
2017 Use <tt>git cherry origin</tt> to see which ones of your patches
2018 were accepted, and/or use <tt>git rebase origin</tt> to port your
2019 unmerged changes forward to the updated upstream.
2020 </p>
2021 </li>
2022 <li>
2024 Use <tt>git format-patch origin</tt> to prepare patches for e-mail
2025 submission to your upstream and send it out. Go back to
2026 step 2. and continue.
2027 </p>
2028 </li>
2029 </ol></div>
2030 </div>
2031 <h2 id="_working_with_others_shared_repository_style">Working with Others, Shared Repository Style</h2>
2032 <div class="sectionbody">
2033 <div class="paragraph"><p>If you are coming from CVS background, the style of cooperation
2034 suggested in the previous section may be new to you. You do not
2035 have to worry. git supports "shared public repository" style of
2036 cooperation you are probably more familiar with as well.</p></div>
2037 <div class="paragraph"><p>See <a href="gitcvs-migration.html">gitcvs-migration(7)</a> for the details.</p></div>
2038 </div>
2039 <h2 id="_bundling_your_work_together">Bundling your work together</h2>
2040 <div class="sectionbody">
2041 <div class="paragraph"><p>It is likely that you will be working on more than one thing at
2042 a time. It is easy to manage those more-or-less independent tasks
2043 using branches with git.</p></div>
2044 <div class="paragraph"><p>We have already seen how branches work previously,
2045 with "fun and work" example using two branches. The idea is the
2046 same if there are more than two branches. Let&#8217;s say you started
2047 out from "master" head, and have some new code in the "master"
2048 branch, and two independent fixes in the "commit-fix" and
2049 "diff-fix" branches:</p></div>
2050 <div class="listingblock">
2051 <div class="content">
2052 <pre><tt>$ git show-branch
2053 ! [commit-fix] Fix commit message normalization.
2054 ! [diff-fix] Fix rename detection.
2055 * [master] Release candidate #1
2057 + [diff-fix] Fix rename detection.
2058 + [diff-fix~1] Better common substring algorithm.
2059 + [commit-fix] Fix commit message normalization.
2060 * [master] Release candidate #1
2061 ++* [diff-fix~2] Pretty-print messages.</tt></pre>
2062 </div></div>
2063 <div class="paragraph"><p>Both fixes are tested well, and at this point, you want to merge
2064 in both of them. You could merge in <em>diff-fix</em> first and then
2065 <em>commit-fix</em> next, like this:</p></div>
2066 <div class="listingblock">
2067 <div class="content">
2068 <pre><tt>$ git merge -m "Merge fix in diff-fix" diff-fix
2069 $ git merge -m "Merge fix in commit-fix" commit-fix</tt></pre>
2070 </div></div>
2071 <div class="paragraph"><p>Which would result in:</p></div>
2072 <div class="listingblock">
2073 <div class="content">
2074 <pre><tt>$ git show-branch
2075 ! [commit-fix] Fix commit message normalization.
2076 ! [diff-fix] Fix rename detection.
2077 * [master] Merge fix in commit-fix
2079 - [master] Merge fix in commit-fix
2080 + * [commit-fix] Fix commit message normalization.
2081 - [master~1] Merge fix in diff-fix
2082 +* [diff-fix] Fix rename detection.
2083 +* [diff-fix~1] Better common substring algorithm.
2084 * [master~2] Release candidate #1
2085 ++* [master~3] Pretty-print messages.</tt></pre>
2086 </div></div>
2087 <div class="paragraph"><p>However, there is no particular reason to merge in one branch
2088 first and the other next, when what you have are a set of truly
2089 independent changes (if the order mattered, then they are not
2090 independent by definition). You could instead merge those two
2091 branches into the current branch at once. First let&#8217;s undo what
2092 we just did and start over. We would want to get the master
2093 branch before these two merges by resetting it to <em>master~2</em>:</p></div>
2094 <div class="listingblock">
2095 <div class="content">
2096 <pre><tt>$ git reset --hard master~2</tt></pre>
2097 </div></div>
2098 <div class="paragraph"><p>You can make sure <tt>git show-branch</tt> matches the state before
2099 those two <em>git merge</em> you just did. Then, instead of running
2100 two <em>git merge</em> commands in a row, you would merge these two
2101 branch heads (this is known as <em>making an Octopus</em>):</p></div>
2102 <div class="listingblock">
2103 <div class="content">
2104 <pre><tt>$ git merge commit-fix diff-fix
2105 $ git show-branch
2106 ! [commit-fix] Fix commit message normalization.
2107 ! [diff-fix] Fix rename detection.
2108 * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
2110 - [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
2111 + * [commit-fix] Fix commit message normalization.
2112 +* [diff-fix] Fix rename detection.
2113 +* [diff-fix~1] Better common substring algorithm.
2114 * [master~1] Release candidate #1
2115 ++* [master~2] Pretty-print messages.</tt></pre>
2116 </div></div>
2117 <div class="paragraph"><p>Note that you should not do Octopus because you can. An octopus
2118 is a valid thing to do and often makes it easier to view the
2119 commit history if you are merging more than two independent
2120 changes at the same time. However, if you have merge conflicts
2121 with any of the branches you are merging in and need to hand
2122 resolve, that is an indication that the development happened in
2123 those branches were not independent after all, and you should
2124 merge two at a time, documenting how you resolved the conflicts,
2125 and the reason why you preferred changes made in one side over
2126 the other. Otherwise it would make the project history harder
2127 to follow, not easier.</p></div>
2128 </div>
2129 <h2 id="_see_also">SEE ALSO</h2>
2130 <div class="sectionbody">
2131 <div class="paragraph"><p><a href="gittutorial.html">gittutorial(7)</a>,
2132 <a href="gittutorial-2.html">gittutorial-2(7)</a>,
2133 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>,
2134 <a href="git-help.html">git-help(1)</a>,
2135 <a href="everyday.html">Everyday git</a>,
2136 <a href="user-manual.html">The Git User&#8217;s Manual</a></p></div>
2137 </div>
2138 <h2 id="_git">GIT</h2>
2139 <div class="sectionbody">
2140 <div class="paragraph"><p>Part of the <a href="git.html">git(1)</a> suite.</p></div>
2141 </div>
2142 <div id="footer">
2143 <div id="footer-text">
2144 Last updated 2010-11-06 00:41:57 UTC
2145 </div>
2146 </div>
2147 </body>
2148 </html>