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