Autogenerated HTML docs for v1.3.1-g7464
[git.git] / core-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.2" />\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 file called <tt>HEAD</tt>, that has <tt>ref: refs/heads/master</tt> in it.\r
333    This is similar to a symbolic link and points at\r
334    <tt>refs/heads/master</tt> relative to the <tt>HEAD</tt> file.\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 points 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>),\r
410 but to 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 <tt>557db&#8230;</tt> and\r
451 <tt>f24c7&#8230;</tt> 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 <tt>557db03</tt> 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>&#8212;pretty</tt> flag,\r
783 which 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>$ cat .git/HEAD</tt></pre>\r
1055 </div></div>\r
1056 <p>will tell you where it's pointing.  To get the list of branches\r
1057 you have, you can say</p>\r
1058 <div class="listingblock">\r
1059 <div class="content">\r
1060 <pre><tt>$ git branch</tt></pre>\r
1061 </div></div>\r
1062 <p>which is nothing more than a simple script around <tt>ls .git/refs/heads</tt>.\r
1063 There will be asterisk in front of the branch you are currently on.</p>\r
1064 <p>Sometimes you may wish to create a new branch _without_ actually\r
1065 checking it out and switching to it. If so, just use the command</p>\r
1066 <div class="listingblock">\r
1067 <div class="content">\r
1068 <pre><tt>$ git branch &lt;branchname&gt; [startingpoint]</tt></pre>\r
1069 </div></div>\r
1070 <p>which will simply _create_ the branch, but will not do anything further.\r
1071 You can then later &#8212; once you decide that you want to actually develop\r
1072 on that branch &#8212; switch to that branch with a regular <tt>git checkout</tt>\r
1073 with the branchname as the argument.</p>\r
1074 </div>\r
1075 <h2>Merging two branches</h2>\r
1076 <div class="sectionbody">\r
1077 <p>One of the ideas of having a branch is that you do some (possibly\r
1078 experimental) work in it, and eventually merge it back to the main\r
1079 branch. So assuming you created the above <tt>mybranch</tt> that started out\r
1080 being the same as the original <tt>master</tt> branch, let's make sure we're in\r
1081 that branch, and do some work there.</p>\r
1082 <div class="listingblock">\r
1083 <div class="content">\r
1084 <pre><tt>$ git checkout mybranch\r
1085 $ echo "Work, work, work" &gt;&gt;hello\r
1086 $ git commit -m 'Some work.' -i hello</tt></pre>\r
1087 </div></div>\r
1088 <p>Here, we just added another line to <tt>hello</tt>, and we used a shorthand for\r
1089 doing both <tt>git-update-index hello</tt> and <tt>git commit</tt> by just giving the\r
1090 filename directly to <tt>git commit</tt>, with an <tt>-i</tt> flag (it tells\r
1091 git to <em>include</em> that file in addition to what you have done to\r
1092 the index file so far when making the commit).  The <tt>-m</tt> flag is to give the\r
1093 commit log message from the command line.</p>\r
1094 <p>Now, to make it a bit more interesting, let's assume that somebody else\r
1095 does some work in the original branch, and simulate that by going back\r
1096 to the master branch, and editing the same file differently there:</p>\r
1097 <div class="listingblock">\r
1098 <div class="content">\r
1099 <pre><tt>$ git checkout master</tt></pre>\r
1100 </div></div>\r
1101 <p>Here, take a moment to look at the contents of <tt>hello</tt>, and notice how they\r
1102 don't contain the work we just did in <tt>mybranch</tt> &#8212; because that work\r
1103 hasn't happened in the <tt>master</tt> branch at all. Then do</p>\r
1104 <div class="listingblock">\r
1105 <div class="content">\r
1106 <pre><tt>$ echo "Play, play, play" &gt;&gt;hello\r
1107 $ echo "Lots of fun" &gt;&gt;example\r
1108 $ git commit -m 'Some fun.' -i hello example</tt></pre>\r
1109 </div></div>\r
1110 <p>since the master branch is obviously in a much better mood.</p>\r
1111 <p>Now, you've got two branches, and you decide that you want to merge the\r
1112 work done. Before we do that, let's introduce a cool graphical tool that\r
1113 helps you view what's going on:</p>\r
1114 <div class="listingblock">\r
1115 <div class="content">\r
1116 <pre><tt>$ gitk --all</tt></pre>\r
1117 </div></div>\r
1118 <p>will show you graphically both of your branches (that's what the <tt>--all</tt>\r
1119 means: normally it will just show you your current <tt>HEAD</tt>) and their\r
1120 histories. You can also see exactly how they came to be from a common\r
1121 source.</p>\r
1122 <p>Anyway, let's exit <tt>gitk</tt> (<tt>^Q</tt> or the File menu), and decide that we want\r
1123 to merge the work we did on the <tt>mybranch</tt> branch into the <tt>master</tt>\r
1124 branch (which is currently our <tt>HEAD</tt> too). To do that, there's a nice\r
1125 script called <tt>git merge</tt>, which wants to know which branches you want\r
1126 to resolve and what the merge is all about:</p>\r
1127 <div class="listingblock">\r
1128 <div class="content">\r
1129 <pre><tt>$ git merge "Merge work in mybranch" HEAD mybranch</tt></pre>\r
1130 </div></div>\r
1131 <p>where the first argument is going to be used as the commit message if\r
1132 the merge can be resolved automatically.</p>\r
1133 <p>Now, in this case we've intentionally created a situation where the\r
1134 merge will need to be fixed up by hand, though, so git will do as much\r
1135 of it as it can automatically (which in this case is just merge the <tt>example</tt>\r
1136 file, which had no differences in the <tt>mybranch</tt> branch), and say:</p>\r
1137 <div class="listingblock">\r
1138 <div class="content">\r
1139 <pre><tt>        Trying really trivial in-index merge...\r
1140         fatal: Merge requires file-level merging\r
1141         Nope.\r
1142         ...\r
1143         Auto-merging hello\r
1144         CONFLICT (content): Merge conflict in hello\r
1145         Automatic merge failed; fix up by hand</tt></pre>\r
1146 </div></div>\r
1147 <p>which is way too verbose, but it basically tells you that it failed the\r
1148 really trivial merge ("Simple merge") and did an "Automatic merge"\r
1149 instead, but that too failed due to conflicts in <tt>hello</tt>.</p>\r
1150 <p>Not to worry. It left the (trivial) conflict in <tt>hello</tt> in the same form you\r
1151 should already be well used to if you've ever used CVS, so let's just\r
1152 open <tt>hello</tt> in our editor (whatever that may be), and fix it up somehow.\r
1153 I'd suggest just making it so that <tt>hello</tt> contains all four lines:</p>\r
1154 <div class="listingblock">\r
1155 <div class="content">\r
1156 <pre><tt>Hello World\r
1157 It's a new day for git\r
1158 Play, play, play\r
1159 Work, work, work</tt></pre>\r
1160 </div></div>\r
1161 <p>and once you're happy with your manual merge, just do a</p>\r
1162 <div class="listingblock">\r
1163 <div class="content">\r
1164 <pre><tt>$ git commit -i hello</tt></pre>\r
1165 </div></div>\r
1166 <p>which will very loudly warn you that you're now committing a merge\r
1167 (which is correct, so never mind), and you can write a small merge\r
1168 message about your adventures in git-merge-land.</p>\r
1169 <p>After you're done, start up <tt>gitk --all</tt> to see graphically what the\r
1170 history looks like. Notice that <tt>mybranch</tt> still exists, and you can\r
1171 switch to it, and continue to work with it if you want to. The\r
1172 <tt>mybranch</tt> branch will not contain the merge, but next time you merge it\r
1173 from the <tt>master</tt> branch, git will know how you merged it, so you'll not\r
1174 have to do _that_ merge again.</p>\r
1175 <p>Another useful tool, especially if you do not always work in X-Window\r
1176 environment, is <tt>git show-branch</tt>.</p>\r
1177 <div class="listingblock">\r
1178 <div class="content">\r
1179 <pre><tt>$ git show-branch --topo-order master mybranch\r
1180 * [master] Merge work in mybranch\r
1181  ! [mybranch] Some work.\r
1182 --\r
1183 -  [master] Merge work in mybranch\r
1184 *+ [mybranch] Some work.</tt></pre>\r
1185 </div></div>\r
1186 <p>The first two lines indicate that it is showing the two branches\r
1187 and the first line of the commit log message from their\r
1188 top-of-the-tree commits, you are currently on <tt>master</tt> branch\r
1189 (notice the asterisk <tt><strong></tt> character), and the first column for\r
1190 the later output lines is used to show commits contained in the\r
1191 <tt>master</tt> branch, and the second column for the <tt>mybranch</tt>\r
1192 branch. Three commits are shown along with their log messages.\r
1193 All of them have non blank characters in the first column (<tt></strong></tt>\r
1194 shows an ordinary commit on the current branch, <tt>.</tt> is a merge commit), which\r
1195 means they are now part of the <tt>master</tt> branch. Only the "Some\r
1196 work" commit has the plus <tt>+</tt> character in the second column,\r
1197 because <tt>mybranch</tt> has not been merged to incorporate these\r
1198 commits from the master branch.  The string inside brackets\r
1199 before the commit log message is a short name you can use to\r
1200 name the commit.  In the above example, <em>master</em> and <em>mybranch</em>\r
1201 are branch heads.  <em>master~1</em> is the first parent of <em>master</em>\r
1202 branch head.  Please see <em>git-rev-parse</em> documentation if you\r
1203 see more complex cases.</p>\r
1204 <p>Now, let's pretend you are the one who did all the work in\r
1205 <tt>mybranch</tt>, and the fruit of your hard work has finally been merged\r
1206 to the <tt>master</tt> branch. Let's go back to <tt>mybranch</tt>, and run\r
1207 resolve to get the "upstream changes" back to your branch.</p>\r
1208 <div class="listingblock">\r
1209 <div class="content">\r
1210 <pre><tt>$ git checkout mybranch\r
1211 $ git merge "Merge upstream changes." HEAD master</tt></pre>\r
1212 </div></div>\r
1213 <p>This outputs something like this (the actual commit object names\r
1214 would be different)</p>\r
1215 <div class="listingblock">\r
1216 <div class="content">\r
1217 <pre><tt>Updating from ae3a2da... to a80b4aa....\r
1218 Fast forward\r
1219  example |    1 +\r
1220  hello   |    1 +\r
1221  2 files changed, 2 insertions(+), 0 deletions(-)</tt></pre>\r
1222 </div></div>\r
1223 <p>Because your branch did not contain anything more than what are\r
1224 already merged into the <tt>master</tt> branch, the resolve operation did\r
1225 not actually do a merge. Instead, it just updated the top of\r
1226 the tree of your branch to that of the <tt>master</tt> branch. This is\r
1227 often called <em>fast forward</em> merge.</p>\r
1228 <p>You can run <tt>gitk --all</tt> again to see how the commit ancestry\r
1229 looks like, or run <tt>show-branch</tt>, which tells you this.</p>\r
1230 <div class="listingblock">\r
1231 <div class="content">\r
1232 <pre><tt>$ git show-branch master mybranch\r
1233 ! [master] Merge work in mybranch\r
1234  * [mybranch] Merge work in mybranch\r
1235 --\r
1236 -- [master] Merge work in mybranch</tt></pre>\r
1237 </div></div>\r
1238 </div>\r
1239 <h2>Merging external work</h2>\r
1240 <div class="sectionbody">\r
1241 <p>It's usually much more common that you merge with somebody else than\r
1242 merging with your own branches, so it's worth pointing out that git\r
1243 makes that very easy too, and in fact, it's not that different from\r
1244 doing a <tt>git merge</tt>. In fact, a remote merge ends up being nothing\r
1245 more than "fetch the work from a remote repository into a temporary tag"\r
1246 followed by a <tt>git merge</tt>.</p>\r
1247 <p>Fetching from a remote repository is done by, unsurprisingly,\r
1248 <tt>git fetch</tt>:</p>\r
1249 <div class="listingblock">\r
1250 <div class="content">\r
1251 <pre><tt>$ git fetch &lt;remote-repository&gt;</tt></pre>\r
1252 </div></div>\r
1253 <p>One of the following transports can be used to name the\r
1254 repository to download from:</p>\r
1255 <dl>\r
1256 <dt>\r
1257 Rsync\r
1258 </dt>\r
1259 <dd>\r
1260 <p>\r
1261         <tt>rsync://remote.machine/path/to/repo.git/</tt>\r
1262 </p>\r
1263 <p>Rsync transport is usable for both uploading and downloading,\r
1264 but is completely unaware of what git does, and can produce\r
1265 unexpected results when you download from the public repository\r
1266 while the repository owner is uploading into it via <tt>rsync</tt>\r
1267 transport.  Most notably, it could update the files under\r
1268 <tt>refs/</tt> which holds the object name of the topmost commits\r
1269 before uploading the files in <tt>objects/</tt> &#8212; the downloader would\r
1270 obtain head commit object name while that object itself is still\r
1271 not available in the repository.  For this reason, it is\r
1272 considered deprecated.</p>\r
1273 </dd>\r
1274 <dt>\r
1275 SSH\r
1276 </dt>\r
1277 <dd>\r
1278 <p>\r
1279         <tt>remote.machine:/path/to/repo.git/</tt> or\r
1280 </p>\r
1281 <p><tt>ssh://remote.machine/path/to/repo.git/</tt></p>\r
1282 <p>This transport can be used for both uploading and downloading,\r
1283 and requires you to have a log-in privilege over <tt>ssh</tt> to the\r
1284 remote machine.  It finds out the set of objects the other side\r
1285 lacks by exchanging the head commits both ends have and\r
1286 transfers (close to) minimum set of objects.  It is by far the\r
1287 most efficient way to exchange git objects between repositories.</p>\r
1288 </dd>\r
1289 <dt>\r
1290 Local directory\r
1291 </dt>\r
1292 <dd>\r
1293 <p>\r
1294         <tt>/path/to/repo.git/</tt>\r
1295 </p>\r
1296 <p>This transport is the same as SSH transport but uses <tt>sh</tt> to run\r
1297 both ends on the local machine instead of running other end on\r
1298 the remote machine via <tt>ssh</tt>.</p>\r
1299 </dd>\r
1300 <dt>\r
1301 git Native\r
1302 </dt>\r
1303 <dd>\r
1304 <p>\r
1305         <tt>git://remote.machine/path/to/repo.git/</tt>\r
1306 </p>\r
1307 <p>This transport was designed for anonymous downloading.  Like SSH\r
1308 transport, it finds out the set of objects the downstream side\r
1309 lacks and transfers (close to) minimum set of objects.</p>\r
1310 </dd>\r
1311 <dt>\r
1312 HTTP(S)\r
1313 </dt>\r
1314 <dd>\r
1315 <p>\r
1316         <tt>http://remote.machine/path/to/repo.git/</tt>\r
1317 </p>\r
1318 <p>Downloader from http and https URL\r
1319 first obtains the topmost commit object name from the remote site\r
1320 by looking at the specified refname under <tt>repo.git/refs/</tt> directory,\r
1321 and then tries to obtain the\r
1322 commit object by downloading from <tt>repo.git/objects/xx/xxx...</tt>\r
1323 using the object name of that commit object.  Then it reads the\r
1324 commit object to find out its parent commits and the associate\r
1325 tree object; it repeats this process until it gets all the\r
1326 necessary objects.  Because of this behaviour, they are\r
1327 sometimes also called <em>commit walkers</em>.</p>\r
1328 <p>The <em>commit walkers</em> are sometimes also called <em>dumb\r
1329 transports</em>, because they do not require any git aware smart\r
1330 server like git Native transport does.  Any stock HTTP server\r
1331 that does not even support directory index would suffice.  But\r
1332 you must prepare your repository with <tt>git-update-server-info</tt>\r
1333 to help dumb transport downloaders.</p>\r
1334 <p>There are (confusingly enough) <tt>git-ssh-fetch</tt> and <tt>git-ssh-upload</tt>\r
1335 programs, which are <em>commit walkers</em>; they outlived their\r
1336 usefulness when git Native and SSH transports were introduced,\r
1337 and not used by <tt>git pull</tt> or <tt>git push</tt> scripts.</p>\r
1338 </dd>\r
1339 </dl>\r
1340 <p>Once you fetch from the remote repository, you <tt>resolve</tt> that\r
1341 with your current branch.</p>\r
1342 <p>However &#8212; it's such a common thing to <tt>fetch</tt> and then\r
1343 immediately <tt>resolve</tt>, that it's called <tt>git pull</tt>, and you can\r
1344 simply do</p>\r
1345 <div class="listingblock">\r
1346 <div class="content">\r
1347 <pre><tt>$ git pull &lt;remote-repository&gt;</tt></pre>\r
1348 </div></div>\r
1349 <p>and optionally give a branch-name for the remote end as a second\r
1350 argument.</p>\r
1351 <div class="admonitionblock">\r
1352 <table><tr>\r
1353 <td class="icon">\r
1354 <div class="title">Note</div>\r
1355 </td>\r
1356 <td class="content">You could do without using any branches at all, by\r
1357 keeping as many local repositories as you would like to have\r
1358 branches, and merging between them with <tt>git pull</tt>, just like\r
1359 you merge between branches. The advantage of this approach is\r
1360 that it lets you keep set of files for each <tt>branch</tt> checked\r
1361 out and you may find it easier to switch back and forth if you\r
1362 juggle multiple lines of development simultaneously. Of\r
1363 course, you will pay the price of more disk usage to hold\r
1364 multiple working trees, but disk space is cheap these days.</td>\r
1365 </tr></table>\r
1366 </div>\r
1367 <div class="admonitionblock">\r
1368 <table><tr>\r
1369 <td class="icon">\r
1370 <div class="title">Note</div>\r
1371 </td>\r
1372 <td class="content">You could even pull from your own repository by\r
1373 giving <em>.</em> as &lt;remote-repository&gt; parameter to <tt>git pull</tt>.  This\r
1374 is useful when you want to merge a local branch (or more, if you\r
1375 are making an Octopus) into the current branch.</td>\r
1376 </tr></table>\r
1377 </div>\r
1378 <p>It is likely that you will be pulling from the same remote\r
1379 repository from time to time. As a short hand, you can store\r
1380 the remote repository URL in a file under .git/remotes/\r
1381 directory, like this:</p>\r
1382 <div class="listingblock">\r
1383 <div class="content">\r
1384 <pre><tt>$ mkdir -p .git/remotes/\r
1385 $ cat &gt;.git/remotes/linus &lt;&lt;\EOF\r
1386 URL: http://www.kernel.org/pub/scm/git/git.git/\r
1387 EOF</tt></pre>\r
1388 </div></div>\r
1389 <p>and use the filename to <tt>git pull</tt> instead of the full URL.\r
1390 The URL specified in such file can even be a prefix\r
1391 of a full URL, like this:</p>\r
1392 <div class="listingblock">\r
1393 <div class="content">\r
1394 <pre><tt>$ cat &gt;.git/remotes/jgarzik &lt;&lt;\EOF\r
1395 URL: http://www.kernel.org/pub/scm/linux/git/jgarzik/\r
1396 EOF</tt></pre>\r
1397 </div></div>\r
1398 <p>Examples.</p>\r
1399 <ol>\r
1400 <li>\r
1401 <p>\r
1402 <tt>git pull linus</tt>\r
1403 </p>\r
1404 </li>\r
1405 <li>\r
1406 <p>\r
1407 <tt>git pull linus tag v0.99.1</tt>\r
1408 </p>\r
1409 </li>\r
1410 <li>\r
1411 <p>\r
1412 <tt>git pull jgarzik/netdev-2.6.git/ e100</tt>\r
1413 </p>\r
1414 </li>\r
1415 </ol>\r
1416 <p>the above are equivalent to:</p>\r
1417 <ol>\r
1418 <li>\r
1419 <p>\r
1420 <tt>git pull http://www.kernel.org/pub/scm/git/git.git/ HEAD</tt>\r
1421 </p>\r
1422 </li>\r
1423 <li>\r
1424 <p>\r
1425 <tt>git pull http://www.kernel.org/pub/scm/git/git.git/ tag v0.99.1</tt>\r
1426 </p>\r
1427 </li>\r
1428 <li>\r
1429 <p>\r
1430 <tt>git pull http://www.kernel.org/pub/&#8230;/jgarzik/netdev-2.6.git e100</tt>\r
1431 </p>\r
1432 </li>\r
1433 </ol>\r
1434 </div>\r
1435 <h2>How does the merge work?</h2>\r
1436 <div class="sectionbody">\r
1437 <p>We said this tutorial shows what plumbing does to help you cope\r
1438 with the porcelain that isn't flushing, but we so far did not\r
1439 talk about how the merge really works.  If you are following\r
1440 this tutorial the first time, I'd suggest to skip to "Publishing\r
1441 your work" section and come back here later.</p>\r
1442 <p>OK, still with me?  To give us an example to look at, let's go\r
1443 back to the earlier repository with "hello" and "example" file,\r
1444 and bring ourselves back to the pre-merge state:</p>\r
1445 <div class="listingblock">\r
1446 <div class="content">\r
1447 <pre><tt>$ git show-branch --more=3 master mybranch\r
1448 ! [master] Merge work in mybranch\r
1449  * [mybranch] Merge work in mybranch\r
1450 --\r
1451 -- [master] Merge work in mybranch\r
1452 +* [master^2] Some work.\r
1453 +* [master^] Some fun.</tt></pre>\r
1454 </div></div>\r
1455 <p>Remember, before running <tt>git merge</tt>, our <tt>master</tt> head was at\r
1456 "Some fun." commit, while our <tt>mybranch</tt> head was at "Some\r
1457 work." commit.</p>\r
1458 <div class="listingblock">\r
1459 <div class="content">\r
1460 <pre><tt>$ git checkout mybranch\r
1461 $ git reset --hard master^2\r
1462 $ git checkout master\r
1463 $ git reset --hard master^</tt></pre>\r
1464 </div></div>\r
1465 <p>After rewinding, the commit structure should look like this:</p>\r
1466 <div class="listingblock">\r
1467 <div class="content">\r
1468 <pre><tt>$ git show-branch\r
1469 * [master] Some fun.\r
1470  ! [mybranch] Some work.\r
1471 --\r
1472  + [mybranch] Some work.\r
1473 *  [master] Some fun.\r
1474 *+ [mybranch^] New day.</tt></pre>\r
1475 </div></div>\r
1476 <p>Now we are ready to experiment with the merge by hand.</p>\r
1477 <p><tt>git merge</tt> command, when merging two branches, uses 3-way merge\r
1478 algorithm.  First, it finds the common ancestor between them.\r
1479 The command it uses is <tt>git-merge-base</tt>:</p>\r
1480 <div class="listingblock">\r
1481 <div class="content">\r
1482 <pre><tt>$ mb=$(git-merge-base HEAD mybranch)</tt></pre>\r
1483 </div></div>\r
1484 <p>The command writes the commit object name of the common ancestor\r
1485 to the standard output, so we captured its output to a variable,\r
1486 because we will be using it in the next step.  BTW, the common\r
1487 ancestor commit is the "New day." commit in this case.  You can\r
1488 tell it by:</p>\r
1489 <div class="listingblock">\r
1490 <div class="content">\r
1491 <pre><tt>$ git-name-rev $mb\r
1492 my-first-tag</tt></pre>\r
1493 </div></div>\r
1494 <p>After finding out a common ancestor commit, the second step is\r
1495 this:</p>\r
1496 <div class="listingblock">\r
1497 <div class="content">\r
1498 <pre><tt>$ git-read-tree -m -u $mb HEAD mybranch</tt></pre>\r
1499 </div></div>\r
1500 <p>This is the same <tt>git-read-tree</tt> command we have already seen,\r
1501 but it takes three trees, unlike previous examples.  This reads\r
1502 the contents of each tree into different <em>stage</em> in the index\r
1503 file (the first tree goes to stage 1, the second stage 2,\r
1504 etc.).  After reading three trees into three stages, the paths\r
1505 that are the same in all three stages are <em>collapsed</em> into stage\r
1506 0.  Also paths that are the same in two of three stages are\r
1507 collapsed into stage 0, taking the SHA1 from either stage 2 or\r
1508 stage 3, whichever is different from stage 1 (i.e. only one side\r
1509 changed from the common ancestor).</p>\r
1510 <p>After <em>collapsing</em> operation, paths that are different in three\r
1511 trees are left in non-zero stages.  At this point, you can\r
1512 inspect the index file with this command:</p>\r
1513 <div class="listingblock">\r
1514 <div class="content">\r
1515 <pre><tt>$ git-ls-files --stage\r
1516 100644 7f8b141b65fdcee47321e399a2598a235a032422 0       example\r
1517 100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1       hello\r
1518 100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2       hello\r
1519 100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello</tt></pre>\r
1520 </div></div>\r
1521 <p>In our example of only two files, we did not have unchanged\r
1522 files so only <em>example</em> resulted in collapsing, but in real-life\r
1523 large projects, only small number of files change in one commit,\r
1524 and this <em>collapsing</em> tends to trivially merge most of the paths\r
1525 fairly quickly, leaving only a handful the real changes in non-zero\r
1526 stages.</p>\r
1527 <p>To look at only non-zero stages, use <tt>--unmerged</tt> flag:</p>\r
1528 <div class="listingblock">\r
1529 <div class="content">\r
1530 <pre><tt>$ git-ls-files --unmerged\r
1531 100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1       hello\r
1532 100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2       hello\r
1533 100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello</tt></pre>\r
1534 </div></div>\r
1535 <p>The next step of merging is to merge these three versions of the\r
1536 file, using 3-way merge.  This is done by giving\r
1537 <tt>git-merge-one-file</tt> command as one of the arguments to\r
1538 <tt>git-merge-index</tt> command:</p>\r
1539 <div class="listingblock">\r
1540 <div class="content">\r
1541 <pre><tt>$ git-merge-index git-merge-one-file hello\r
1542 Auto-merging hello.\r
1543 merge: warning: conflicts during merge\r
1544 ERROR: Merge conflict in hello.\r
1545 fatal: merge program failed</tt></pre>\r
1546 </div></div>\r
1547 <p><tt>git-merge-one-file</tt> script is called with parameters to\r
1548 describe those three versions, and is responsible to leave the\r
1549 merge results in the working tree.\r
1550 It is a fairly straightforward shell script, and\r
1551 eventually calls <tt>merge</tt> program from RCS suite to perform a\r
1552 file-level 3-way merge.  In this case, <tt>merge</tt> detects\r
1553 conflicts, and the merge result with conflict marks is left in\r
1554 the working tree..  This can be seen if you run <tt>ls-files\r
1555 &#8212;stage</tt> again at this point:</p>\r
1556 <div class="listingblock">\r
1557 <div class="content">\r
1558 <pre><tt>$ git-ls-files --stage\r
1559 100644 7f8b141b65fdcee47321e399a2598a235a032422 0       example\r
1560 100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1       hello\r
1561 100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2       hello\r
1562 100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello</tt></pre>\r
1563 </div></div>\r
1564 <p>This is the state of the index file and the working file after\r
1565 <tt>git merge</tt> returns control back to you, leaving the conflicting\r
1566 merge for you to resolve.  Notice that the path <tt>hello</tt> is still\r
1567 unmerged, and what you see with <tt>git diff</tt> at this point is\r
1568 differences since stage 2 (i.e. your version).</p>\r
1569 </div>\r
1570 <h2>Publishing your work</h2>\r
1571 <div class="sectionbody">\r
1572 <p>So we can use somebody else's work from a remote repository; but\r
1573 how can <strong>you</strong> prepare a repository to let other people pull from\r
1574 it?</p>\r
1575 <p>Your do your real work in your working tree that has your\r
1576 primary repository hanging under it as its <tt>.git</tt> subdirectory.\r
1577 You <strong>could</strong> make that repository accessible remotely and ask\r
1578 people to pull from it, but in practice that is not the way\r
1579 things are usually done. A recommended way is to have a public\r
1580 repository, make it reachable by other people, and when the\r
1581 changes you made in your primary working tree are in good shape,\r
1582 update the public repository from it. This is often called\r
1583 <em>pushing</em>.</p>\r
1584 <div class="admonitionblock">\r
1585 <table><tr>\r
1586 <td class="icon">\r
1587 <div class="title">Note</div>\r
1588 </td>\r
1589 <td class="content">This public repository could further be mirrored, and that is\r
1590 how git repositories at <tt>kernel.org</tt> are managed.</td>\r
1591 </tr></table>\r
1592 </div>\r
1593 <p>Publishing the changes from your local (private) repository to\r
1594 your remote (public) repository requires a write privilege on\r
1595 the remote machine. You need to have an SSH account there to\r
1596 run a single command, <tt>git-receive-pack</tt>.</p>\r
1597 <p>First, you need to create an empty repository on the remote\r
1598 machine that will house your public repository. This empty\r
1599 repository will be populated and be kept up-to-date by pushing\r
1600 into it later. Obviously, this repository creation needs to be\r
1601 done only once.</p>\r
1602 <div class="admonitionblock">\r
1603 <table><tr>\r
1604 <td class="icon">\r
1605 <div class="title">Note</div>\r
1606 </td>\r
1607 <td class="content"><tt>git push</tt> uses a pair of programs,\r
1608 <tt>git-send-pack</tt> on your local machine, and <tt>git-receive-pack</tt>\r
1609 on the remote machine. The communication between the two over\r
1610 the network internally uses an SSH connection.</td>\r
1611 </tr></table>\r
1612 </div>\r
1613 <p>Your private repository's git directory is usually <tt>.git</tt>, but\r
1614 your public repository is often named after the project name,\r
1615 i.e. <tt>&lt;project&gt;.git</tt>. Let's create such a public repository for\r
1616 project <tt>my-git</tt>. After logging into the remote machine, create\r
1617 an empty directory:</p>\r
1618 <div class="listingblock">\r
1619 <div class="content">\r
1620 <pre><tt>$ mkdir my-git.git</tt></pre>\r
1621 </div></div>\r
1622 <p>Then, make that directory into a git repository by running\r
1623 <tt>git init-db</tt>, but this time, since its name is not the usual\r
1624 <tt>.git</tt>, we do things slightly differently:</p>\r
1625 <div class="listingblock">\r
1626 <div class="content">\r
1627 <pre><tt>$ GIT_DIR=my-git.git git-init-db</tt></pre>\r
1628 </div></div>\r
1629 <p>Make sure this directory is available for others you want your\r
1630 changes to be pulled by via the transport of your choice. Also\r
1631 you need to make sure that you have the <tt>git-receive-pack</tt>\r
1632 program on the <tt>$PATH</tt>.</p>\r
1633 <div class="admonitionblock">\r
1634 <table><tr>\r
1635 <td class="icon">\r
1636 <div class="title">Note</div>\r
1637 </td>\r
1638 <td class="content">Many installations of sshd do not invoke your shell as the login\r
1639 shell when you directly run programs; what this means is that if\r
1640 your login shell is <tt>bash</tt>, only <tt>.bashrc</tt> is read and not\r
1641 <tt>.bash_profile</tt>. As a workaround, make sure <tt>.bashrc</tt> sets up\r
1642 <tt>$PATH</tt> so that you can run <tt>git-receive-pack</tt> program.</td>\r
1643 </tr></table>\r
1644 </div>\r
1645 <div class="admonitionblock">\r
1646 <table><tr>\r
1647 <td class="icon">\r
1648 <div class="title">Note</div>\r
1649 </td>\r
1650 <td class="content">If you plan to publish this repository to be accessed over http,\r
1651 you should do <tt>chmod +x my-git.git/hooks/post-update</tt> at this\r
1652 point.  This makes sure that every time you push into this\r
1653 repository, <tt>git-update-server-info</tt> is run.</td>\r
1654 </tr></table>\r
1655 </div>\r
1656 <p>Your "public repository" is now ready to accept your changes.\r
1657 Come back to the machine you have your private repository. From\r
1658 there, run this command:</p>\r
1659 <div class="listingblock">\r
1660 <div class="content">\r
1661 <pre><tt>$ git push &lt;public-host&gt;:/path/to/my-git.git master</tt></pre>\r
1662 </div></div>\r
1663 <p>This synchronizes your public repository to match the named\r
1664 branch head (i.e. <tt>master</tt> in this case) and objects reachable\r
1665 from them in your current repository.</p>\r
1666 <p>As a real example, this is how I update my public git\r
1667 repository. Kernel.org mirror network takes care of the\r
1668 propagation to other publicly visible machines:</p>\r
1669 <div class="listingblock">\r
1670 <div class="content">\r
1671 <pre><tt>$ git push master.kernel.org:/pub/scm/git/git.git/</tt></pre>\r
1672 </div></div>\r
1673 </div>\r
1674 <h2>Packing your repository</h2>\r
1675 <div class="sectionbody">\r
1676 <p>Earlier, we saw that one file under <tt>.git/objects/??/</tt> directory\r
1677 is stored for each git object you create. This representation\r
1678 is efficient to create atomically and safely, but\r
1679 not so convenient to transport over the network. Since git objects are\r
1680 immutable once they are created, there is a way to optimize the\r
1681 storage by "packing them together". The command</p>\r
1682 <div class="listingblock">\r
1683 <div class="content">\r
1684 <pre><tt>$ git repack</tt></pre>\r
1685 </div></div>\r
1686 <p>will do it for you. If you followed the tutorial examples, you\r
1687 would have accumulated about 17 objects in <tt>.git/objects/??/</tt>\r
1688 directories by now. <tt>git repack</tt> tells you how many objects it\r
1689 packed, and stores the packed file in <tt>.git/objects/pack</tt>\r
1690 directory.</p>\r
1691 <div class="admonitionblock">\r
1692 <table><tr>\r
1693 <td class="icon">\r
1694 <div class="title">Note</div>\r
1695 </td>\r
1696 <td class="content">You will see two files, <tt>pack-*.pack</tt> and <tt>pack-*.idx</tt>,\r
1697 in <tt>.git/objects/pack</tt> directory. They are closely related to\r
1698 each other, and if you ever copy them by hand to a different\r
1699 repository for whatever reason, you should make sure you copy\r
1700 them together. The former holds all the data from the objects\r
1701 in the pack, and the latter holds the index for random\r
1702 access.</td>\r
1703 </tr></table>\r
1704 </div>\r
1705 <p>If you are paranoid, running <tt>git-verify-pack</tt> command would\r
1706 detect if you have a corrupt pack, but do not worry too much.\r
1707 Our programs are always perfect ;-).</p>\r
1708 <p>Once you have packed objects, you do not need to leave the\r
1709 unpacked objects that are contained in the pack file anymore.</p>\r
1710 <div class="listingblock">\r
1711 <div class="content">\r
1712 <pre><tt>$ git prune-packed</tt></pre>\r
1713 </div></div>\r
1714 <p>would remove them for you.</p>\r
1715 <p>You can try running <tt>find .git/objects -type f</tt> before and after\r
1716 you run <tt>git prune-packed</tt> if you are curious.  Also <tt>git\r
1717 count-objects</tt> would tell you how many unpacked objects are in\r
1718 your repository and how much space they are consuming.</p>\r
1719 <div class="admonitionblock">\r
1720 <table><tr>\r
1721 <td class="icon">\r
1722 <div class="title">Note</div>\r
1723 </td>\r
1724 <td class="content"><tt>git pull</tt> is slightly cumbersome for HTTP transport, as a\r
1725 packed repository may contain relatively few objects in a\r
1726 relatively large pack. If you expect many HTTP pulls from your\r
1727 public repository you might want to repack &amp; prune often, or\r
1728 never.</td>\r
1729 </tr></table>\r
1730 </div>\r
1731 <p>If you run <tt>git repack</tt> again at this point, it will say\r
1732 "Nothing to pack". Once you continue your development and\r
1733 accumulate the changes, running <tt>git repack</tt> again will create a\r
1734 new pack, that contains objects created since you packed your\r
1735 repository the last time. We recommend that you pack your project\r
1736 soon after the initial import (unless you are starting your\r
1737 project from scratch), and then run <tt>git repack</tt> every once in a\r
1738 while, depending on how active your project is.</p>\r
1739 <p>When a repository is synchronized via <tt>git push</tt> and <tt>git pull</tt>\r
1740 objects packed in the source repository are usually stored\r
1741 unpacked in the destination, unless rsync transport is used.\r
1742 While this allows you to use different packing strategies on\r
1743 both ends, it also means you may need to repack both\r
1744 repositories every once in a while.</p>\r
1745 </div>\r
1746 <h2>Working with Others</h2>\r
1747 <div class="sectionbody">\r
1748 <p>Although git is a truly distributed system, it is often\r
1749 convenient to organize your project with an informal hierarchy\r
1750 of developers. Linux kernel development is run this way. There\r
1751 is a nice illustration (page 17, "Merges to Mainline") in Randy\r
1752 Dunlap's presentation (<tt>http://tinyurl.com/a2jdg</tt>).</p>\r
1753 <p>It should be stressed that this hierarchy is purely <strong>informal</strong>.\r
1754 There is nothing fundamental in git that enforces the "chain of\r
1755 patch flow" this hierarchy implies. You do not have to pull\r
1756 from only one remote repository.</p>\r
1757 <p>A recommended workflow for a "project lead" goes like this:</p>\r
1758 <ol>\r
1759 <li>\r
1760 <p>\r
1761 Prepare your primary repository on your local machine. Your\r
1762    work is done there.\r
1763 </p>\r
1764 </li>\r
1765 <li>\r
1766 <p>\r
1767 Prepare a public repository accessible to others.\r
1768 </p>\r
1769 <p>If other people are pulling from your repository over dumb\r
1770 transport protocols (HTTP), you need to keep this repository\r
1771 <em>dumb transport friendly</em>.  After <tt>git init-db</tt>,\r
1772 <tt>$GIT_DIR/hooks/post-update</tt> copied from the standard templates\r
1773 would contain a call to <tt>git-update-server-info</tt> but the\r
1774 <tt>post-update</tt> hook itself is disabled by default &#8212; enable it\r
1775 with <tt>chmod +x post-update</tt>.  This makes sure <tt>git-update-server-info</tt>\r
1776 keeps the necessary files up-to-date.</p>\r
1777 </li>\r
1778 <li>\r
1779 <p>\r
1780 Push into the public repository from your primary\r
1781    repository.\r
1782 </p>\r
1783 </li>\r
1784 <li>\r
1785 <p>\r
1786 <tt>git repack</tt> the public repository. This establishes a big\r
1787    pack that contains the initial set of objects as the\r
1788    baseline, and possibly <tt>git prune</tt> if the transport\r
1789    used for pulling from your repository supports packed\r
1790    repositories.\r
1791 </p>\r
1792 </li>\r
1793 <li>\r
1794 <p>\r
1795 Keep working in your primary repository. Your changes\r
1796    include modifications of your own, patches you receive via\r
1797    e-mails, and merges resulting from pulling the "public"\r
1798    repositories of your "subsystem maintainers".\r
1799 </p>\r
1800 <p>You can repack this private repository whenever you feel like.</p>\r
1801 </li>\r
1802 <li>\r
1803 <p>\r
1804 Push your changes to the public repository, and announce it\r
1805    to the public.\r
1806 </p>\r
1807 </li>\r
1808 <li>\r
1809 <p>\r
1810 Every once in a while, "git repack" the public repository.\r
1811    Go back to step 5. and continue working.\r
1812 </p>\r
1813 </li>\r
1814 </ol>\r
1815 <p>A recommended work cycle for a "subsystem maintainer" who works\r
1816 on that project and has an own "public repository" goes like this:</p>\r
1817 <ol>\r
1818 <li>\r
1819 <p>\r
1820 Prepare your work repository, by <tt>git clone</tt> the public\r
1821    repository of the "project lead". The URL used for the\r
1822    initial cloning is stored in <tt>.git/remotes/origin</tt>.\r
1823 </p>\r
1824 </li>\r
1825 <li>\r
1826 <p>\r
1827 Prepare a public repository accessible to others, just like\r
1828    the "project lead" person does.\r
1829 </p>\r
1830 </li>\r
1831 <li>\r
1832 <p>\r
1833 Copy over the packed files from "project lead" public\r
1834    repository to your public repository, unless the "project\r
1835    lead" repository lives on the same machine as yours.  In the\r
1836    latter case, you can use <tt>objects/info/alternates</tt> file to\r
1837    point at the repository you are borrowing from.\r
1838 </p>\r
1839 </li>\r
1840 <li>\r
1841 <p>\r
1842 Push into the public repository from your primary\r
1843    repository. Run <tt>git repack</tt>, and possibly <tt>git prune</tt> if the\r
1844    transport used for pulling from your repository supports\r
1845    packed repositories.\r
1846 </p>\r
1847 </li>\r
1848 <li>\r
1849 <p>\r
1850 Keep working in your primary repository. Your changes\r
1851    include modifications of your own, patches you receive via\r
1852    e-mails, and merges resulting from pulling the "public"\r
1853    repositories of your "project lead" and possibly your\r
1854    "sub-subsystem maintainers".\r
1855 </p>\r
1856 <p>You can repack this private repository whenever you feel\r
1857 like.</p>\r
1858 </li>\r
1859 <li>\r
1860 <p>\r
1861 Push your changes to your public repository, and ask your\r
1862    "project lead" and possibly your "sub-subsystem\r
1863    maintainers" to pull from it.\r
1864 </p>\r
1865 </li>\r
1866 <li>\r
1867 <p>\r
1868 Every once in a while, <tt>git repack</tt> the public repository.\r
1869    Go back to step 5. and continue working.\r
1870 </p>\r
1871 </li>\r
1872 </ol>\r
1873 <p>A recommended work cycle for an "individual developer" who does\r
1874 not have a "public" repository is somewhat different. It goes\r
1875 like this:</p>\r
1876 <ol>\r
1877 <li>\r
1878 <p>\r
1879 Prepare your work repository, by <tt>git clone</tt> the public\r
1880    repository of the "project lead" (or a "subsystem\r
1881    maintainer", if you work on a subsystem). The URL used for\r
1882    the initial cloning is stored in <tt>.git/remotes/origin</tt>.\r
1883 </p>\r
1884 </li>\r
1885 <li>\r
1886 <p>\r
1887 Do your work in your repository on <em>master</em> branch.\r
1888 </p>\r
1889 </li>\r
1890 <li>\r
1891 <p>\r
1892 Run <tt>git fetch origin</tt> from the public repository of your\r
1893    upstream every once in a while. This does only the first\r
1894    half of <tt>git pull</tt> but does not merge. The head of the\r
1895    public repository is stored in <tt>.git/refs/heads/origin</tt>.\r
1896 </p>\r
1897 </li>\r
1898 <li>\r
1899 <p>\r
1900 Use <tt>git cherry origin</tt> to see which ones of your patches\r
1901    were accepted, and/or use <tt>git rebase origin</tt> to port your\r
1902    unmerged changes forward to the updated upstream.\r
1903 </p>\r
1904 </li>\r
1905 <li>\r
1906 <p>\r
1907 Use <tt>git format-patch origin</tt> to prepare patches for e-mail\r
1908    submission to your upstream and send it out. Go back to\r
1909    step 2. and continue.\r
1910 </p>\r
1911 </li>\r
1912 </ol>\r
1913 </div>\r
1914 <h2>Working with Others, Shared Repository Style</h2>\r
1915 <div class="sectionbody">\r
1916 <p>If you are coming from CVS background, the style of cooperation\r
1917 suggested in the previous section may be new to you. You do not\r
1918 have to worry. git supports "shared public repository" style of\r
1919 cooperation you are probably more familiar with as well.</p>\r
1920 <p>See <a href="cvs-migration.txt">git for CVS users</a> for the details.</p>\r
1921 </div>\r
1922 <h2>Bundling your work together</h2>\r
1923 <div class="sectionbody">\r
1924 <p>It is likely that you will be working on more than one thing at\r
1925 a time.  It is easy to manage those more-or-less independent tasks\r
1926 using branches with git.</p>\r
1927 <p>We have already seen how branches work previously,\r
1928 with "fun and work" example using two branches.  The idea is the\r
1929 same if there are more than two branches.  Let's say you started\r
1930 out from "master" head, and have some new code in the "master"\r
1931 branch, and two independent fixes in the "commit-fix" and\r
1932 "diff-fix" branches:</p>\r
1933 <div class="listingblock">\r
1934 <div class="content">\r
1935 <pre><tt>$ git show-branch\r
1936 ! [commit-fix] Fix commit message normalization.\r
1937  ! [diff-fix] Fix rename detection.\r
1938   * [master] Release candidate #1\r
1939 ---\r
1940  +  [diff-fix] Fix rename detection.\r
1941  +  [diff-fix~1] Better common substring algorithm.\r
1942 +   [commit-fix] Fix commit message normalization.\r
1943   * [master] Release candidate #1\r
1944 ++* [diff-fix~2] Pretty-print messages.</tt></pre>\r
1945 </div></div>\r
1946 <p>Both fixes are tested well, and at this point, you want to merge\r
1947 in both of them.  You could merge in <em>diff-fix</em> first and then\r
1948 <em>commit-fix</em> next, like this:</p>\r
1949 <div class="listingblock">\r
1950 <div class="content">\r
1951 <pre><tt>$ git merge 'Merge fix in diff-fix' master diff-fix\r
1952 $ git merge 'Merge fix in commit-fix' master commit-fix</tt></pre>\r
1953 </div></div>\r
1954 <p>Which would result in:</p>\r
1955 <div class="listingblock">\r
1956 <div class="content">\r
1957 <pre><tt>$ git show-branch\r
1958 ! [commit-fix] Fix commit message normalization.\r
1959  ! [diff-fix] Fix rename detection.\r
1960   * [master] Merge fix in commit-fix\r
1961 ---\r
1962   - [master] Merge fix in commit-fix\r
1963 + * [commit-fix] Fix commit message normalization.\r
1964   - [master~1] Merge fix in diff-fix\r
1965  +* [diff-fix] Fix rename detection.\r
1966  +* [diff-fix~1] Better common substring algorithm.\r
1967   * [master~2] Release candidate #1\r
1968 ++* [master~3] Pretty-print messages.</tt></pre>\r
1969 </div></div>\r
1970 <p>However, there is no particular reason to merge in one branch\r
1971 first and the other next, when what you have are a set of truly\r
1972 independent changes (if the order mattered, then they are not\r
1973 independent by definition).  You could instead merge those two\r
1974 branches into the current branch at once.  First let's undo what\r
1975 we just did and start over.  We would want to get the master\r
1976 branch before these two merges by resetting it to <em>master~2</em>:</p>\r
1977 <div class="listingblock">\r
1978 <div class="content">\r
1979 <pre><tt>$ git reset --hard master~2</tt></pre>\r
1980 </div></div>\r
1981 <p>You can make sure <em>git show-branch</em> matches the state before\r
1982 those two <em>git merge</em> you just did.  Then, instead of running\r
1983 two <em>git merge</em> commands in a row, you would pull these two\r
1984 branch heads (this is known as <em>making an Octopus</em>):</p>\r
1985 <div class="listingblock">\r
1986 <div class="content">\r
1987 <pre><tt>$ git pull . commit-fix diff-fix\r
1988 $ git show-branch\r
1989 ! [commit-fix] Fix commit message normalization.\r
1990  ! [diff-fix] Fix rename detection.\r
1991   * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'\r
1992 ---\r
1993   - [master] Octopus merge of branches 'diff-fix' and 'commit-fix'\r
1994 + * [commit-fix] Fix commit message normalization.\r
1995  +* [diff-fix] Fix rename detection.\r
1996  +* [diff-fix~1] Better common substring algorithm.\r
1997   * [master~1] Release candidate #1\r
1998 ++* [master~2] Pretty-print messages.</tt></pre>\r
1999 </div></div>\r
2000 <p>Note that you should not do Octopus because you can.  An octopus\r
2001 is a valid thing to do and often makes it easier to view the\r
2002 commit history if you are pulling more than two independent\r
2003 changes at the same time.  However, if you have merge conflicts\r
2004 with any of the branches you are merging in and need to hand\r
2005 resolve, that is an indication that the development happened in\r
2006 those branches were not independent after all, and you should\r
2007 merge two at a time, documenting how you resolved the conflicts,\r
2008 and the reason why you preferred changes made in one side over\r
2009 the other.  Otherwise it would make the project history harder\r
2010 to follow, not easier.</p>\r
2011 </div>\r
2012 <div id="footer">\r
2013 <div id="footer-text">\r
2014 Last updated 07-Feb-2006 08:04:34 UTC\r
2015 </div>\r
2016 </div>\r
2017 </body>\r
2018 </html>\r