blob: 9e92ce6fef6ee08f5b4d55234cd9f49b2a5c4e1e [file] [log] [blame]
Junio C Hamano6f8a7902006-05-22 01:10:131<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6<meta name="generator" content="AsciiDoc 7.0.2" />
7<style type="text/css">
8/* Debug borders */
9p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
10/*
11 border: 1px solid red;
12*/
13}
14
15body {
16 margin: 1em 5% 1em 5%;
17}
18
19a { color: blue; }
20a:visited { color: fuchsia; }
21
22em {
23 font-style: italic;
24}
25
26strong {
27 font-weight: bold;
28}
29
30tt {
31 color: navy;
32}
33
34h1, h2, h3, h4, h5, h6 {
35 color: #527bbd;
36 font-family: sans-serif;
37 margin-top: 1.2em;
38 margin-bottom: 0.5em;
39 line-height: 1.3;
40}
41
42h1 {
43 border-bottom: 2px solid silver;
44}
45h2 {
46 border-bottom: 2px solid silver;
47 padding-top: 0.5em;
48}
49
50div.sectionbody {
51 font-family: serif;
52 margin-left: 0;
53}
54
55hr {
56 border: 1px solid silver;
57}
58
59p {
60 margin-top: 0.5em;
61 margin-bottom: 0.5em;
62}
63
64pre {
65 padding: 0;
66 margin: 0;
67}
68
69span#author {
70 color: #527bbd;
71 font-family: sans-serif;
72 font-weight: bold;
73 font-size: 1.2em;
74}
75span#email {
76}
77span#revision {
78 font-family: sans-serif;
79}
80
81div#footer {
82 font-family: sans-serif;
83 font-size: small;
84 border-top: 2px solid silver;
85 padding-top: 0.5em;
86 margin-top: 4.0em;
87}
88div#footer-text {
89 float: left;
90 padding-bottom: 0.5em;
91}
92div#footer-badges {
93 float: right;
94 padding-bottom: 0.5em;
95}
96
97div#preamble,
98div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
99div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
100div.admonitionblock {
101 margin-right: 10%;
102 margin-top: 1.5em;
103 margin-bottom: 1.5em;
104}
105div.admonitionblock {
106 margin-top: 2.5em;
107 margin-bottom: 2.5em;
108}
109
110div.content { /* Block element content. */
111 padding: 0;
112}
113
114/* Block element titles. */
115div.title, caption.title {
116 font-family: sans-serif;
117 font-weight: bold;
118 text-align: left;
119 margin-top: 1.0em;
120 margin-bottom: 0.5em;
121}
122div.title + * {
123 margin-top: 0;
124}
125
126td div.title:first-child {
127 margin-top: 0.0em;
128}
129div.content div.title:first-child {
130 margin-top: 0.0em;
131}
132div.content + div.title {
133 margin-top: 0.0em;
134}
135
136div.sidebarblock > div.content {
137 background: #ffffee;
138 border: 1px solid silver;
139 padding: 0.5em;
140}
141
142div.listingblock > div.content {
143 border: 1px solid silver;
144 background: #f4f4f4;
145 padding: 0.5em;
146}
147
148div.quoteblock > div.content {
149 padding-left: 2.0em;
150}
151div.quoteblock .attribution {
152 text-align: right;
153}
154
155div.admonitionblock .icon {
156 vertical-align: top;
157 font-size: 1.1em;
158 font-weight: bold;
159 text-decoration: underline;
160 color: #527bbd;
161 padding-right: 0.5em;
162}
163div.admonitionblock td.content {
164 padding-left: 0.5em;
165 border-left: 2px solid silver;
166}
167
168div.exampleblock > div.content {
169 border-left: 2px solid silver;
170 padding: 0.5em;
171}
172
173div.verseblock div.content {
174 white-space: pre;
175}
176
177div.imageblock div.content { padding-left: 0; }
178div.imageblock img { border: 1px solid silver; }
179span.image img { border-style: none; }
180
181dl {
182 margin-top: 0.8em;
183 margin-bottom: 0.8em;
184}
185dt {
186 margin-top: 0.5em;
187 margin-bottom: 0;
188 font-style: italic;
189}
190dd > *:first-child {
191 margin-top: 0;
192}
193
194ul, ol {
195 list-style-position: outside;
196}
197ol.olist2 {
198 list-style-type: lower-alpha;
199}
200
201div.tableblock > table {
202 border-color: #527bbd;
203 border-width: 3px;
204}
205thead {
206 font-family: sans-serif;
207 font-weight: bold;
208}
209tfoot {
210 font-weight: bold;
211}
212
213div.hlist {
214 margin-top: 0.8em;
215 margin-bottom: 0.8em;
216}
217td.hlist1 {
218 vertical-align: top;
219 font-style: italic;
220 padding-right: 0.8em;
221}
222td.hlist2 {
223 vertical-align: top;
224}
225
226@media print {
227 div#footer-badges { display: none; }
228}
229/* Workarounds for IE6's broken and incomplete CSS2. */
230
231div.sidebar-content {
232 background: #ffffee;
233 border: 1px solid silver;
234 padding: 0.5em;
235}
236div.sidebar-title, div.image-title {
237 font-family: sans-serif;
238 font-weight: bold;
239 margin-top: 0.0em;
240 margin-bottom: 0.5em;
241}
242
243div.listingblock div.content {
244 border: 1px solid silver;
245 background: #f4f4f4;
246 padding: 0.5em;
247}
248
249div.quoteblock-content {
250 padding-left: 2.0em;
251}
252
253div.exampleblock-content {
254 border-left: 2px solid silver;
255 padding-left: 0.5em;
256}
257</style>
258<title>A tutorial introduction to git: part two</title>
259</head>
260<body>
261<div id="header">
262<h1>A tutorial introduction to git: part two</h1>
263</div>
264<div id="preamble">
265<div class="sectionbody">
266<p>You should work through <a href="tutorial.html">A tutorial introduction to
267git</a> before reading this tutorial.</p>
268<p>The goal of this tutorial is to introduce two fundamental pieces of
269git's architecture&#8212;the object database and the index file&#8212;and to
270provide the reader with everything necessary to understand the rest
271of the git documentation.</p>
272</div>
273</div>
274<h2>The git object database</h2>
275<div class="sectionbody">
276<p>Let's start a new project and create a small amount of history:</p>
277<div class="listingblock">
278<div class="content">
279<pre><tt>$ mkdir test-project
280$ cd test-project
Junio C Hamanofc4d38c2007-01-08 06:53:32281$ git init
Junio C Hamano7d23f5e2006-12-16 07:44:04282Initialized empty Git repository in .git/
Junio C Hamano6f8a7902006-05-22 01:10:13283$ echo 'hello world' &gt; file.txt
284$ git add .
285$ git commit -a -m "initial commit"
Junio C Hamano7d23f5e2006-12-16 07:44:04286Created initial commit 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
Junio C Hamanoe7935c42006-12-13 21:32:17287 create mode 100644 file.txt
Junio C Hamano6f8a7902006-05-22 01:10:13288$ echo 'hello world!' &gt;file.txt
Junio C Hamano7d23f5e2006-12-16 07:44:04289$ git commit -a -m "add emphasis"
290Created commit c4d59f390b9cfd4318117afde11d601c1085f241</tt></pre>
Junio C Hamano6f8a7902006-05-22 01:10:13291</div></div>
Junio C Hamano7d23f5e2006-12-16 07:44:04292<p>What are the 40 digits of hex that git responded to the commit with?</p>
Junio C Hamano6f8a7902006-05-22 01:10:13293<p>We saw in part one of the tutorial that commits have names like this.
294It turns out that every object in the git history is stored under
295such a 40-digit hex name. That name is the SHA1 hash of the object's
296contents; among other things, this ensures that git will never store
297the same data twice (since identical data is given an identical SHA1
298name), and that the contents of a git object will never change (since
299that would change the object's name as well).</p>
Junio C Hamano7d23f5e2006-12-16 07:44:04300<p>It is expected that the content of the commit object you created while
301following the example above generates a different SHA1 hash than
302the one shown above because the commit object records the time when
303it was created and the name of the person performing the commit.</p>
Junio C Hamano6f8a7902006-05-22 01:10:13304<p>We can ask git about this particular object with the cat-file
Junio C Hamano7d23f5e2006-12-16 07:44:04305command. Don't copy the 40 hex digits from this example but use those
306from your own version. Note that you can shorten it to only a few
307characters to save yourself typing all 40 hex digits:</p>
Junio C Hamano6f8a7902006-05-22 01:10:13308<div class="listingblock">
309<div class="content">
Junio C Hamano7d23f5e2006-12-16 07:44:04310<pre><tt>$ git-cat-file -t 54196cc2
311commit
312$ git-cat-file commit 54196cc2
313tree 92b8b694ffb1675e5975148e1121810081dbdffe
314author J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
315committer J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
316
317initial commit</tt></pre>
Junio C Hamano6f8a7902006-05-22 01:10:13318</div></div>
319<p>A tree can refer to one or more "blob" objects, each corresponding to
320a file. In addition, a tree can also refer to other tree objects,
Junio C Hamano341071d2006-06-04 07:24:48321thus creating a directory hierarchy. You can examine the contents of
Junio C Hamano6f8a7902006-05-22 01:10:13322any tree using ls-tree (remember that a long enough initial portion
323of the SHA1 will also work):</p>
324<div class="listingblock">
325<div class="content">
326<pre><tt>$ git ls-tree 92b8b694
327100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad file.txt</tt></pre>
328</div></div>
329<p>Thus we see that this tree has one file in it. The SHA1 hash is a
330reference to that file's data:</p>
331<div class="listingblock">
332<div class="content">
333<pre><tt>$ git cat-file -t 3b18e512
334blob</tt></pre>
335</div></div>
336<p>A "blob" is just file data, which we can also examine with cat-file:</p>
337<div class="listingblock">
338<div class="content">
339<pre><tt>$ git cat-file blob 3b18e512
340hello world</tt></pre>
341</div></div>
342<p>Note that this is the old file data; so the object that git named in
343its response to the initial tree was a tree with a snapshot of the
344directory state that was recorded by the first commit.</p>
345<p>All of these objects are stored under their SHA1 names inside the git
346directory:</p>
347<div class="listingblock">
348<div class="content">
349<pre><tt>$ find .git/objects/
350.git/objects/
351.git/objects/pack
352.git/objects/info
353.git/objects/3b
354.git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad
355.git/objects/92
356.git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe
357.git/objects/54
358.git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7
359.git/objects/a0
360.git/objects/a0/423896973644771497bdc03eb99d5281615b51
361.git/objects/d0
362.git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59
363.git/objects/c4
364.git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241</tt></pre>
365</div></div>
366<p>and the contents of these files is just the compressed data plus a
367header identifying their length and their type. The type is either a
Junio C Hamano7d23f5e2006-12-16 07:44:04368blob, a tree, a commit, or a tag.</p>
Junio C Hamano6f8a7902006-05-22 01:10:13369<p>The simplest commit to find is the HEAD commit, which we can find
370from .git/HEAD:</p>
371<div class="listingblock">
372<div class="content">
373<pre><tt>$ cat .git/HEAD
374ref: refs/heads/master</tt></pre>
375</div></div>
376<p>As you can see, this tells us which branch we're currently on, and it
377tells us this by naming a file under the .git directory, which itself
378contains a SHA1 name referring to a commit object, which we can
379examine with cat-file:</p>
380<div class="listingblock">
381<div class="content">
382<pre><tt>$ cat .git/refs/heads/master
383c4d59f390b9cfd4318117afde11d601c1085f241
384$ git cat-file -t c4d59f39
385commit
386$ git cat-file commit c4d59f39
387tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
388parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
389author J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143418702 -0500
390committer J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143418702 -0500
391
392add emphasis</tt></pre>
393</div></div>
394<p>The "tree" object here refers to the new state of the tree:</p>
395<div class="listingblock">
396<div class="content">
397<pre><tt>$ git ls-tree d0492b36
398100644 blob a0423896973644771497bdc03eb99d5281615b51 file.txt
Junio C Hamano7da87bb2006-06-06 01:23:49399$ git cat-file blob a0423896
Junio C Hamano6f8a7902006-05-22 01:10:13400hello world!</tt></pre>
401</div></div>
402<p>and the "parent" object refers to the previous commit:</p>
403<div class="listingblock">
404<div class="content">
405<pre><tt>$ git-cat-file commit 54196cc2
406tree 92b8b694ffb1675e5975148e1121810081dbdffe
407author J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
408committer J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
409
410initial commit</tt></pre>
411</div></div>
412<p>The tree object is the tree we examined first, and this commit is
413unusual in that it lacks any parent.</p>
414<p>Most commits have only one parent, but it is also common for a commit
415to have multiple parents. In that case the commit represents a
416merge, with the parent references pointing to the heads of the merged
417branches.</p>
418<p>Besides blobs, trees, and commits, the only remaining type of object
419is a "tag", which we won't discuss here; refer to <a href="git-tag.html">git-tag(1)</a>
420for details.</p>
421<p>So now we know how git uses the object database to represent a
422project's history:</p>
423<ul>
424<li>
425<p>
426"commit" objects refer to "tree" objects representing the
427 snapshot of a directory tree at a particular point in the
428 history, and refer to "parent" commits to show how they're
429 connected into the project history.
430</p>
431</li>
432<li>
433<p>
434"tree" objects represent the state of a single directory,
435 associating directory names to "blob" objects containing file
436 data and "tree" objects containing subdirectory information.
437</p>
438</li>
439<li>
440<p>
441"blob" objects contain file data without any other structure.
442</p>
443</li>
444<li>
445<p>
446References to commit objects at the head of each branch are
447 stored in files under .git/refs/heads/.
448</p>
449</li>
450<li>
451<p>
452The name of the current branch is stored in .git/HEAD.
453</p>
454</li>
455</ul>
456<p>Note, by the way, that lots of commands take a tree as an argument.
457But as we can see above, a tree can be referred to in many different
458ways&#8212;by the SHA1 name for that tree, by the name of a commit that
459refers to the tree, by the name of a branch whose head refers to that
460tree, etc.&#8212;and most such commands can accept any of these names.</p>
461<p>In command synopses, the word "tree-ish" is sometimes used to
462designate such an argument.</p>
463</div>
464<h2>The index file</h2>
465<div class="sectionbody">
466<p>The primary tool we've been using to create commits is "git commit
467-a", which creates a commit including every change you've made to
468your working tree. But what if you want to commit changes only to
469certain files? Or only certain changes to certain files?</p>
470<p>If we look at the way commits are created under the cover, we'll see
471that there are more flexible ways creating commits.</p>
472<p>Continuing with our test-project, let's modify file.txt again:</p>
473<div class="listingblock">
474<div class="content">
475<pre><tt>$ echo "hello world, again" &gt;&gt;file.txt</tt></pre>
476</div></div>
477<p>but this time instead of immediately making the commit, let's take an
478intermediate step, and ask for diffs along the way to keep track of
479what's happening:</p>
480<div class="listingblock">
481<div class="content">
482<pre><tt>$ git diff
483--- a/file.txt
484+++ b/file.txt
485@@ -1 +1,2 @@
486 hello world!
487+hello world, again
Junio C Hamano89d4e0f2007-02-18 00:34:59488$ git add file.txt
Junio C Hamano6f8a7902006-05-22 01:10:13489$ git diff</tt></pre>
490</div></div>
491<p>The last diff is empty, but no new commits have been made, and the
492head still doesn't contain the new line:</p>
493<div class="listingblock">
494<div class="content">
495<pre><tt>$ git-diff HEAD
496diff --git a/file.txt b/file.txt
497index a042389..513feba 100644
498--- a/file.txt
499+++ b/file.txt
500@@ -1 +1,2 @@
501 hello world!
502+hello world, again</tt></pre>
503</div></div>
504<p>So "git diff" is comparing against something other than the head.
505The thing that it's comparing against is actually the index file,
506which is stored in .git/index in a binary format, but whose contents
507we can examine with ls-files:</p>
508<div class="listingblock">
509<div class="content">
510<pre><tt>$ git ls-files --stage
511100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt
512$ git cat-file -t 513feba2
513blob
514$ git cat-file blob 513feba2
Junio C Hamano8fd52302006-08-10 00:18:08515hello world!
Junio C Hamano6f8a7902006-05-22 01:10:13516hello world, again</tt></pre>
517</div></div>
Junio C Hamano89d4e0f2007-02-18 00:34:59518<p>So what our "git add" did was store a new blob and then put
Junio C Hamano6f8a7902006-05-22 01:10:13519a reference to it in the index file. If we modify the file again,
520we'll see that the new modifications are reflected in the "git-diff"
521output:</p>
522<div class="listingblock">
523<div class="content">
524<pre><tt>$ echo 'again?' &gt;&gt;file.txt
525$ git diff
526index 513feba..ba3da7b 100644
527--- a/file.txt
528+++ b/file.txt
529@@ -1,2 +1,3 @@
530 hello world!
531 hello world, again
532+again?</tt></pre>
533</div></div>
534<p>With the right arguments, git diff can also show us the difference
535between the working directory and the last commit, or between the
536index and the last commit:</p>
537<div class="listingblock">
538<div class="content">
539<pre><tt>$ git diff HEAD
540diff --git a/file.txt b/file.txt
541index a042389..ba3da7b 100644
542--- a/file.txt
543+++ b/file.txt
544@@ -1 +1,3 @@
545 hello world!
546+hello world, again
547+again?
548$ git diff --cached
549diff --git a/file.txt b/file.txt
550index a042389..513feba 100644
551--- a/file.txt
552+++ b/file.txt
553@@ -1 +1,2 @@
554 hello world!
555+hello world, again</tt></pre>
556</div></div>
557<p>At any time, we can create a new commit using "git commit" (without
558the -a option), and verify that the state committed only includes the
559changes stored in the index file, not the additional change that is
560still only in our working tree:</p>
561<div class="listingblock">
562<div class="content">
563<pre><tt>$ git commit -m "repeat"
564$ git diff HEAD
565diff --git a/file.txt b/file.txt
566index 513feba..ba3da7b 100644
567--- a/file.txt
568+++ b/file.txt
569@@ -1,2 +1,3 @@
570 hello world!
571 hello world, again
572+again?</tt></pre>
573</div></div>
574<p>So by default "git commit" uses the index to create the commit, not
575the working tree; the -a option to commit tells it to first update
576the index with all changes in the working tree.</p>
577<p>Finally, it's worth looking at the effect of "git add" on the index
578file:</p>
579<div class="listingblock">
580<div class="content">
581<pre><tt>$ echo "goodbye, world" &gt;closing.txt
582$ git add closing.txt</tt></pre>
583</div></div>
584<p>The effect of the "git add" was to add one entry to the index file:</p>
585<div class="listingblock">
586<div class="content">
587<pre><tt>$ git ls-files --stage
588100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0 closing.txt
589100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt</tt></pre>
590</div></div>
591<p>And, as you can see with cat-file, this new entry refers to the
592current contents of the file:</p>
593<div class="listingblock">
594<div class="content">
Junio C Hamanof65d9282007-01-22 09:00:13595<pre><tt>$ git cat-file blob 8b9743b2
596goodbye, world</tt></pre>
Junio C Hamano6f8a7902006-05-22 01:10:13597</div></div>
598<p>The "status" command is a useful way to get a quick summary of the
599situation:</p>
600<div class="listingblock">
601<div class="content">
602<pre><tt>$ git status
Junio C Hamano8a66a652007-02-03 08:07:35603# On branch master
604# Changes to be committed:
605# (use "git reset HEAD &lt;file&gt;..." to unstage)
Junio C Hamano6f8a7902006-05-22 01:10:13606#
607# new file: closing.txt
608#
Junio C Hamano8a66a652007-02-03 08:07:35609# Changed but not updated:
610# (use "git add &lt;file&gt;..." to update what will be committed)
Junio C Hamano6f8a7902006-05-22 01:10:13611#
612# modified: file.txt
613#</tt></pre>
614</div></div>
615<p>Since the current state of closing.txt is cached in the index file,
Junio C Hamano8a66a652007-02-03 08:07:35616it is listed as "Changes to be committed". Since file.txt has
Junio C Hamano6f8a7902006-05-22 01:10:13617changes in the working directory that aren't reflected in the index,
Junio C Hamano8a66a652007-02-03 08:07:35618it is marked "changed but not updated". At this point, running "git
Junio C Hamano6f8a7902006-05-22 01:10:13619commit" would create a commit that added closing.txt (with its new
620contents), but that didn't modify file.txt.</p>
621<p>Also, note that a bare "git diff" shows the changes to file.txt, but
622not the addition of closing.txt, because the version of closing.txt
623in the index file is identical to the one in the working directory.</p>
624<p>In addition to being the staging area for new commits, the index file
625is also populated from the object database when checking out a
626branch, and is used to hold the trees involved in a merge operation.
Junio C Hamanofd73d892006-09-14 07:38:22627See the <a href="core-tutorial.html">core tutorial</a> and the relevant man
Junio C Hamano6f8a7902006-05-22 01:10:13628pages for details.</p>
629</div>
630<h2>What next?</h2>
631<div class="sectionbody">
632<p>At this point you should know everything necessary to read the man
633pages for any of the git commands; one good place to start would be
Junio C Hamano59929ee2006-05-22 22:34:54634with the commands mentioned in <a href="everyday.html">Everyday git</a>. You
Junio C Hamano6f8a7902006-05-22 01:10:13635should be able to find any unknown jargon in the
Junio C Hamanobb8fb052006-05-30 07:21:12636<a href="glossary.html">Glossary</a>.</p>
Junio C Hamanoed7f4f62007-05-20 09:09:09637<p>The <a href="user-manual.html">Git User's Manual</a> provides a more
638comprehensive introduction to git.</p>
Junio C Hamano6f8a7902006-05-22 01:10:13639<p>The <a href="cvs-migration.html">CVS migration</a> document explains how to
640import a CVS repository into git, and shows how to use git in a
641CVS-like way.</p>
642<p>For some interesting examples of git use, see the
643<a href="howto-index.html">howtos</a>.</p>
644<p>For git developers, the <a href="core-tutorial.html">Core tutorial</a> goes
645into detail on the lower-level git mechanisms involved in, for
646example, creating a new commit.</p>
647</div>
648<div id="footer">
649<div id="footer-text">
Junio C Hamano35738e82008-01-07 07:55:46650Last updated 07-Jan-2008 07:51:03 UTC
Junio C Hamano6f8a7902006-05-22 01:10:13651</div>
652</div>
653</body>
654</html>