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