1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8" />
6 <meta name=
"generator" content=
"AsciiDoc 8.2.5" />
7 <style type=
"text/css">
9 p
, li
, dt
, dd
, div
, pre
, h1
, h2
, h3
, h4
, h5
, h6
{
11 border: 1px solid red;
16 margin: 1em 5% 1em 5%;
21 text-decoration: underline
;
39 h1
, h2
, h3
, h4
, h5
, h6
{
41 font-family: sans-serif
;
48 border-bottom: 2px solid silver
;
66 border: 1px solid silver
;
81 font-family: sans-serif
;
88 font-family: sans-serif
;
92 font-family: sans-serif
;
94 border-top: 2px solid silver
;
100 padding-bottom: 0.5em;
104 padding-bottom: 0.5em;
108 div
.tableblock
, div
.imageblock
, div
.exampleblock
, div
.verseblock
,
109 div
.quoteblock
, div
.literalblock
, div
.listingblock
, div
.sidebarblock
,
110 div
.admonitionblock
{
113 margin-bottom: 1.5em;
115 div
.admonitionblock
{
117 margin-bottom: 2.5em;
120 div
.content
{ /* Block element content. */
124 /* Block element titles. */
125 div
.title
, caption
.title
{
126 font-family: sans-serif
;
130 margin-bottom: 0.5em;
136 td div
.title:first-child
{
139 div
.content div
.title:first-child
{
142 div
.content
+ div
.title
{
146 div
.sidebarblock
> div
.content
{
148 border: 1px solid silver
;
155 div
.listingblock
> div
.content
{
156 border: 1px solid silver
;
161 div
.quoteblock
> div
.content
{
168 div
.verseblock
+ div
.attribution
{
172 div
.admonitionblock
.icon
{
176 text-decoration: underline
;
178 padding-right: 0.5em;
180 div
.admonitionblock td
.content
{
182 border-left: 2px solid silver
;
185 div
.exampleblock
> div
.content
{
186 border-left: 2px solid silver
;
190 div
.verseblock div
.content
{
194 div
.imageblock div
.content
{ padding-left: 0; }
195 div
.imageblock img
{ border: 1px solid silver
; }
196 span
.image img
{ border-style: none
; }
200 margin-bottom: 0.8em;
212 list-style-position: outside
;
215 list-style-type: lower-alpha
;
218 div
.tableblock
> table
{
219 border: 3px solid
#527bbd;
222 font-family: sans-serif
;
231 margin-bottom: 0.8em;
239 padding-right: 0.8em;
246 div#footer-badges
{ display: none
; }
251 font-family: sans-serif
;
255 margin-bottom: 0.1em;
258 div
.toclevel1
, div
.toclevel2
, div
.toclevel3
, div
.toclevel4
{
274 include1::./stylesheets
/xhtml11-manpage
.css
[]
275 /* Workarounds for IE6's broken and incomplete CSS2. */
277 div
.sidebar-content
{
279 border: 1px solid silver
;
282 div
.sidebar-title
, div
.image-title
{
283 font-family: sans-serif
;
286 margin-bottom: 0.5em;
289 div
.listingblock div
.content
{
290 border: 1px solid silver
;
295 div
.quoteblock-content
{
299 div
.exampleblock-content
{
300 border-left: 2px solid silver
;
304 /* IE6 sets dynamically generated links as visited. */
305 div#toc
a:visited
{ color: blue
; }
307 <title>gittutorial-
2(
7)
</title>
312 gittutorial-
2(
7) Manual Page
315 <div class=
"sectionbody">
317 A tutorial introduction to git: part two
322 <div class=
"sectionbody">
323 <div class=
"para"><p>git *
</p></div>
325 <h2 id=
"_description">DESCRIPTION
</h2>
326 <div class=
"sectionbody">
327 <div class=
"para"><p>You should work through
<a href=
"gittutorial.html">gittutorial(
7)
</a> before reading this tutorial.
</p></div>
328 <div class=
"para"><p>The goal of this tutorial is to introduce two fundamental pieces of
329 git's architecture--the object database and the index file--and to
330 provide the reader with everything necessary to understand the rest
331 of the git documentation.
</p></div>
333 <h2 id=
"_the_git_object_database">The git object database
</h2>
334 <div class=
"sectionbody">
335 <div class=
"para"><p>Let's start a new project and create a small amount of history:
</p></div>
336 <div class=
"listingblock">
337 <div class=
"content">
338 <pre><tt>$ mkdir test-project
341 Initialized empty Git repository in .git/
342 $ echo 'hello world'
> file.txt
344 $ git commit -a -m
"initial commit"
345 [master (root-commit)
54196cc] initial commit
346 1 files changed,
1 insertions(+),
0 deletions(-)
347 create mode
100644 file.txt
348 $ echo 'hello world!'
>file.txt
349 $ git commit -a -m
"add emphasis"
350 [master c4d59f3] add emphasis
351 1 files changed,
1 insertions(+),
1 deletions(-)
</tt></pre>
353 <div class=
"para"><p>What are the
7 digits of hex that git responded to the commit with?
</p></div>
354 <div class=
"para"><p>We saw in part one of the tutorial that commits have names like this.
355 It turns out that every object in the git history is stored under
356 a
40-digit hex name. That name is the SHA1 hash of the object's
357 contents; among other things, this ensures that git will never store
358 the same data twice (since identical data is given an identical SHA1
359 name), and that the contents of a git object will never change (since
360 that would change the object's name as well). The
7 char hex strings
361 here are simply the abbreviation of such
40 character long strings.
362 Abbreviations can be used everywhere where the
40 character strings
363 can be used, so long as they are unambiguous.
</p></div>
364 <div class=
"para"><p>It is expected that the content of the commit object you created while
365 following the example above generates a different SHA1 hash than
366 the one shown above because the commit object records the time when
367 it was created and the name of the person performing the commit.
</p></div>
368 <div class=
"para"><p>We can ask git about this particular object with the
<tt>cat-file
</tt>
369 command. Don't copy the
40 hex digits from this example but use those
370 from your own version. Note that you can shorten it to only a few
371 characters to save yourself typing all
40 hex digits:
</p></div>
372 <div class=
"listingblock">
373 <div class=
"content">
374 <pre><tt>$ git cat-file -t
54196cc2
376 $ git cat-file commit
54196cc2
377 tree
92b8b694ffb1675e5975148e1121810081dbdffe
378 author J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143414668 -
0500
379 committer J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143414668 -
0500
381 initial commit
</tt></pre>
383 <div class=
"para"><p>A tree can refer to one or more
"blob" objects, each corresponding to
384 a file. In addition, a tree can also refer to other tree objects,
385 thus creating a directory hierarchy. You can examine the contents of
386 any tree using ls-tree (remember that a long enough initial portion
387 of the SHA1 will also work):
</p></div>
388 <div class=
"listingblock">
389 <div class=
"content">
390 <pre><tt>$ git ls-tree
92b8b694
391 100644 blob
3b18e512dba79e4c8300dd08aeb37f8e728b8dad file.txt
</tt></pre>
393 <div class=
"para"><p>Thus we see that this tree has one file in it. The SHA1 hash is a
394 reference to that file's data:
</p></div>
395 <div class=
"listingblock">
396 <div class=
"content">
397 <pre><tt>$ git cat-file -t
3b18e512
400 <div class=
"para"><p>A
"blob" is just file data, which we can also examine with cat-file:
</p></div>
401 <div class=
"listingblock">
402 <div class=
"content">
403 <pre><tt>$ git cat-file blob
3b18e512
404 hello world
</tt></pre>
406 <div class=
"para"><p>Note that this is the old file data; so the object that git named in
407 its response to the initial tree was a tree with a snapshot of the
408 directory state that was recorded by the first commit.
</p></div>
409 <div class=
"para"><p>All of these objects are stored under their SHA1 names inside the git
411 <div class=
"listingblock">
412 <div class=
"content">
413 <pre><tt>$ find .git/objects/
418 .git/objects/
3b/
18e512dba79e4c8300dd08aeb37f8e728b8dad
420 .git/objects/
92/b8b694ffb1675e5975148e1121810081dbdffe
422 .git/objects/
54/
196cc2703dc165cbd373a65a4dcf22d50ae7f7
424 .git/objects/a0/
423896973644771497bdc03eb99d5281615b51
426 .git/objects/d0/
492b368b66bdabf2ac1fd8c92b39d3db916e59
428 .git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241
</tt></pre>
430 <div class=
"para"><p>and the contents of these files is just the compressed data plus a
431 header identifying their length and their type. The type is either a
432 blob, a tree, a commit, or a tag.
</p></div>
433 <div class=
"para"><p>The simplest commit to find is the HEAD commit, which we can find
434 from .git/HEAD:
</p></div>
435 <div class=
"listingblock">
436 <div class=
"content">
437 <pre><tt>$ cat .git/HEAD
438 ref: refs/heads/master
</tt></pre>
440 <div class=
"para"><p>As you can see, this tells us which branch we're currently on, and it
441 tells us this by naming a file under the .git directory, which itself
442 contains a SHA1 name referring to a commit object, which we can
443 examine with cat-file:
</p></div>
444 <div class=
"listingblock">
445 <div class=
"content">
446 <pre><tt>$ cat .git/refs/heads/master
447 c4d59f390b9cfd4318117afde11d601c1085f241
448 $ git cat-file -t c4d59f39
450 $ git cat-file commit c4d59f39
451 tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
452 parent
54196cc2703dc165cbd373a65a4dcf22d50ae7f7
453 author J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143418702 -
0500
454 committer J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143418702 -
0500
456 add emphasis
</tt></pre>
458 <div class=
"para"><p>The
"tree" object here refers to the new state of the tree:
</p></div>
459 <div class=
"listingblock">
460 <div class=
"content">
461 <pre><tt>$ git ls-tree d0492b36
462 100644 blob a0423896973644771497bdc03eb99d5281615b51 file.txt
463 $ git cat-file blob a0423896
464 hello world!
</tt></pre>
466 <div class=
"para"><p>and the
"parent" object refers to the previous commit:
</p></div>
467 <div class=
"listingblock">
468 <div class=
"content">
469 <pre><tt>$ git cat-file commit
54196cc2
470 tree
92b8b694ffb1675e5975148e1121810081dbdffe
471 author J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143414668 -
0500
472 committer J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143414668 -
0500
474 initial commit
</tt></pre>
476 <div class=
"para"><p>The tree object is the tree we examined first, and this commit is
477 unusual in that it lacks any parent.
</p></div>
478 <div class=
"para"><p>Most commits have only one parent, but it is also common for a commit
479 to have multiple parents. In that case the commit represents a
480 merge, with the parent references pointing to the heads of the merged
482 <div class=
"para"><p>Besides blobs, trees, and commits, the only remaining type of object
483 is a
"tag", which we won't discuss here; refer to
<a href=
"git-tag.html">git-tag(
1)
</a>
484 for details.
</p></div>
485 <div class=
"para"><p>So now we know how git uses the object database to represent a
486 project's history:
</p></div>
487 <div class=
"ilist"><ul>
490 "commit" objects refer to
"tree" objects representing the
491 snapshot of a directory tree at a particular point in the
492 history, and refer to
"parent" commits to show how they're
493 connected into the project history.
498 "tree" objects represent the state of a single directory,
499 associating directory names to
"blob" objects containing file
500 data and
"tree" objects containing subdirectory information.
505 "blob" objects contain file data without any other structure.
510 References to commit objects at the head of each branch are
511 stored in files under .git/refs/heads/.
516 The name of the current branch is stored in .git/HEAD.
520 <div class=
"para"><p>Note, by the way, that lots of commands take a tree as an argument.
521 But as we can see above, a tree can be referred to in many different
522 ways--by the SHA1 name for that tree, by the name of a commit that
523 refers to the tree, by the name of a branch whose head refers to that
524 tree, etc.--and most such commands can accept any of these names.
</p></div>
525 <div class=
"para"><p>In command synopses, the word
"tree-ish" is sometimes used to
526 designate such an argument.
</p></div>
528 <h2 id=
"_the_index_file">The index file
</h2>
529 <div class=
"sectionbody">
530 <div class=
"para"><p>The primary tool we've been using to create commits is
<tt>git-commit
531 -a
</tt>, which creates a commit including every change you've made to
532 your working tree. But what if you want to commit changes only to
533 certain files? Or only certain changes to certain files?
</p></div>
534 <div class=
"para"><p>If we look at the way commits are created under the cover, we'll see
535 that there are more flexible ways creating commits.
</p></div>
536 <div class=
"para"><p>Continuing with our test-project, let's modify file.txt again:
</p></div>
537 <div class=
"listingblock">
538 <div class=
"content">
539 <pre><tt>$ echo
"hello world, again" >>file.txt
</tt></pre>
541 <div class=
"para"><p>but this time instead of immediately making the commit, let's take an
542 intermediate step, and ask for diffs along the way to keep track of
543 what's happening:
</p></div>
544 <div class=
"listingblock">
545 <div class=
"content">
553 $ git diff
</tt></pre>
555 <div class=
"para"><p>The last diff is empty, but no new commits have been made, and the
556 head still doesn't contain the new line:
</p></div>
557 <div class=
"listingblock">
558 <div class=
"content">
559 <pre><tt>$ git diff HEAD
560 diff --git a/file.txt b/file.txt
561 index a042389.
.513feba
100644
566 +hello world, again
</tt></pre>
568 <div class=
"para"><p>So
<em>git diff
</em> is comparing against something other than the head.
569 The thing that it's comparing against is actually the index file,
570 which is stored in .git/index in a binary format, but whose contents
571 we can examine with ls-files:
</p></div>
572 <div class=
"listingblock">
573 <div class=
"content">
574 <pre><tt>$ git ls-files --stage
575 100644 513feba2e53ebbd2532419ded848ba19de88ba00
0 file.txt
576 $ git cat-file -t
513feba2
578 $ git cat-file blob
513feba2
580 hello world, again
</tt></pre>
582 <div class=
"para"><p>So what our
<em>git add
</em> did was store a new blob and then put
583 a reference to it in the index file. If we modify the file again,
584 we'll see that the new modifications are reflected in the
<em>git diff
</em>
586 <div class=
"listingblock">
587 <div class=
"content">
588 <pre><tt>$ echo 'again?'
>>file.txt
590 index
513feba..ba3da7b
100644
598 <div class=
"para"><p>With the right arguments,
<em>git diff
</em> can also show us the difference
599 between the working directory and the last commit, or between the
600 index and the last commit:
</p></div>
601 <div class=
"listingblock">
602 <div class=
"content">
603 <pre><tt>$ git diff HEAD
604 diff --git a/file.txt b/file.txt
605 index a042389..ba3da7b
100644
613 diff --git a/file.txt b/file.txt
614 index a042389.
.513feba
100644
619 +hello world, again
</tt></pre>
621 <div class=
"para"><p>At any time, we can create a new commit using
<em>git commit
</em> (without
622 the
"-a" option), and verify that the state committed only includes the
623 changes stored in the index file, not the additional change that is
624 still only in our working tree:
</p></div>
625 <div class=
"listingblock">
626 <div class=
"content">
627 <pre><tt>$ git commit -m
"repeat"
629 diff --git a/file.txt b/file.txt
630 index
513feba..ba3da7b
100644
638 <div class=
"para"><p>So by default
<em>git commit
</em> uses the index to create the commit, not
639 the working tree; the
"-a" option to commit tells it to first update
640 the index with all changes in the working tree.
</p></div>
641 <div class=
"para"><p>Finally, it's worth looking at the effect of
<em>git add
</em> on the index
643 <div class=
"listingblock">
644 <div class=
"content">
645 <pre><tt>$ echo
"goodbye, world" >closing.txt
646 $ git add closing.txt
</tt></pre>
648 <div class=
"para"><p>The effect of the
<em>git add
</em> was to add one entry to the index file:
</p></div>
649 <div class=
"listingblock">
650 <div class=
"content">
651 <pre><tt>$ git ls-files --stage
652 100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138
0 closing.txt
653 100644 513feba2e53ebbd2532419ded848ba19de88ba00
0 file.txt
</tt></pre>
655 <div class=
"para"><p>And, as you can see with cat-file, this new entry refers to the
656 current contents of the file:
</p></div>
657 <div class=
"listingblock">
658 <div class=
"content">
659 <pre><tt>$ git cat-file blob
8b9743b2
660 goodbye, world
</tt></pre>
662 <div class=
"para"><p>The
"status" command is a useful way to get a quick summary of the
664 <div class=
"listingblock">
665 <div class=
"content">
666 <pre><tt>$ git status
668 # Changes to be committed:
669 # (use
"git reset HEAD <file>..." to unstage)
671 # new file: closing.txt
673 # Changed but not updated:
674 # (use
"git add <file>..." to update what will be committed)
679 <div class=
"para"><p>Since the current state of closing.txt is cached in the index file,
680 it is listed as
"Changes to be committed". Since file.txt has
681 changes in the working directory that aren't reflected in the index,
682 it is marked
"changed but not updated". At this point, running
"git
683 commit" would create a commit that added closing.txt (with its new
684 contents), but that didn't modify file.txt.
</p></div>
685 <div class=
"para"><p>Also, note that a bare
<tt>git diff
</tt> shows the changes to file.txt, but
686 not the addition of closing.txt, because the version of closing.txt
687 in the index file is identical to the one in the working directory.
</p></div>
688 <div class=
"para"><p>In addition to being the staging area for new commits, the index file
689 is also populated from the object database when checking out a
690 branch, and is used to hold the trees involved in a merge operation.
691 See
<a href=
"gitcore-tutorial.html">gitcore-tutorial(
7)
</a> and the relevant man
692 pages for details.
</p></div>
694 <h2 id=
"_what_next">What next?
</h2>
695 <div class=
"sectionbody">
696 <div class=
"para"><p>At this point you should know everything necessary to read the man
697 pages for any of the git commands; one good place to start would be
698 with the commands mentioned in
<a href=
"everyday.html">Everyday git
</a>. You
699 should be able to find any unknown jargon in
<a href=
"gitglossary.html">gitglossary(
7)
</a>.
</p></div>
700 <div class=
"para"><p>The
<a href=
"user-manual.html">Git User's Manual
</a> provides a more
701 comprehensive introduction to git.
</p></div>
702 <div class=
"para"><p><a href=
"gitcvs-migration.html">gitcvs-migration(
7)
</a> explains how to
703 import a CVS repository into git, and shows how to use git in a
704 CVS-like way.
</p></div>
705 <div class=
"para"><p>For some interesting examples of git use, see the
706 <a href=
"howto-index.html">howtos
</a>.
</p></div>
707 <div class=
"para"><p>For git developers,
<a href=
"gitcore-tutorial.html">gitcore-tutorial(
7)
</a> goes
708 into detail on the lower-level git mechanisms involved in, for
709 example, creating a new commit.
</p></div>
711 <h2 id=
"_see_also">SEE ALSO
</h2>
712 <div class=
"sectionbody">
713 <div class=
"para"><p><a href=
"gittutorial.html">gittutorial(
7)
</a>,
714 <a href=
"gitcvs-migration.html">gitcvs-migration(
7)
</a>,
715 <a href=
"gitcore-tutorial.html">gitcore-tutorial(
7)
</a>,
716 <a href=
"gitglossary.html">gitglossary(
7)
</a>,
717 <a href=
"git-help.html">git-help(
1)
</a>,
718 <a href=
"everyday.html">Everyday git
</a>,
719 <a href=
"user-manual.html">The Git User's Manual
</a></p></div>
721 <h2 id=
"_git">GIT
</h2>
722 <div class=
"sectionbody">
723 <div class=
"para"><p>Part of the
<a href=
"git.html">git(
1)
</a> suite.
</p></div>
726 <div id=
"footer-text">
727 Last updated
2010-
04-
24 02:
56:
14 UTC