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.4.5" />
7 <title>gittutorial-
2(
7)
</title>
8 <style type=
"text/css">
10 p
, li
, dt
, dd
, div
, pre
, h1
, h2
, h3
, h4
, h5
, h6
{
12 border: 1px solid red;
17 margin: 1em 5% 1em 5%;
22 text-decoration: underline
;
42 h1
, h2
, h3
, h4
, h5
, h6
{
44 font-family: sans-serif
;
51 border-bottom: 2px solid silver
;
69 border: 1px solid silver
;
88 font-family: sans-serif
;
94 span#revnumber
, span#revdate
, span#revremark
{
95 font-family: sans-serif
;
99 font-family: sans-serif
;
101 border-top: 2px solid silver
;
107 padding-bottom: 0.5em;
111 padding-bottom: 0.5em;
116 margin-bottom: 1.5em;
118 div
.tableblock
, div
.imageblock
, div
.exampleblock
, div
.verseblock
,
119 div
.quoteblock
, div
.literalblock
, div
.listingblock
, div
.sidebarblock
,
120 div
.admonitionblock
{
122 margin-bottom: 1.5em;
124 div
.admonitionblock
{
126 margin-bottom: 2.5em;
129 div
.content
{ /* Block element content. */
133 /* Block element titles. */
134 div
.title
, caption
.title
{
136 font-family: sans-serif
;
140 margin-bottom: 0.5em;
146 td div
.title:first-child
{
149 div
.content div
.title:first-child
{
152 div
.content
+ div
.title
{
156 div
.sidebarblock
> div
.content
{
158 border: 1px solid silver
;
162 div
.listingblock
> div
.content
{
163 border: 1px solid silver
;
172 div
.quoteblock
> div
.attribution
{
181 div
.verseblock
> div
.content
{
184 div
.verseblock
> div
.attribution
{
188 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
189 div
.verseblock
+ div
.attribution
{
193 div
.admonitionblock
.icon
{
197 text-decoration: underline
;
199 padding-right: 0.5em;
201 div
.admonitionblock td
.content
{
203 border-left: 2px solid silver
;
206 div
.exampleblock
> div
.content
{
207 border-left: 2px solid silver
;
211 div
.imageblock div
.content
{ padding-left: 0; }
212 span
.image img
{ border-style: none
; }
213 a
.image:visited
{ color: white
; }
217 margin-bottom: 0.8em;
230 list-style-position: outside
;
233 list-style-type: decimal
;
236 list-style-type: lower-alpha
;
239 list-style-type: upper-alpha
;
242 list-style-type: lower-roman
;
245 list-style-type: upper-roman
;
248 div
.compact ul
, div
.compact ol
,
249 div
.compact p
, div
.compact p
,
250 div
.compact div
, div
.compact div
{
252 margin-bottom: 0.1em;
255 div
.tableblock
> table
{
256 border: 3px solid
#527bbd;
259 font-family: sans-serif
;
271 /* Because the table frame attribute is overriden by CSS in most browsers. */
272 div
.tableblock
> table
[frame
="void"] {
275 div
.tableblock
> table
[frame
="hsides"] {
276 border-left-style: none
;
277 border-right-style: none
;
279 div
.tableblock
> table
[frame
="vsides"] {
280 border-top-style: none
;
281 border-bottom-style: none
;
287 margin-bottom: 0.8em;
290 padding-bottom: 15px;
292 dt
.hdlist1
.strong
, td
.hdlist1
.strong
{
298 padding-right: 0.8em;
304 div
.hdlist
.compact tr
{
314 div#footer-badges
{ display: none
; }
319 font-family: sans-serif
;
323 margin-bottom: 0.1em;
326 div
.toclevel1
, div
.toclevel2
, div
.toclevel3
, div
.toclevel4
{
342 /* Overrides for manpage documents */
345 padding-bottom: 0.5em;
346 border-top: 2px solid silver
;
347 border-bottom: 2px solid silver
;
357 div#toc
{ display: none
; }
360 /* Workarounds for IE6's broken and incomplete CSS2. */
362 div
.sidebar-content
{
364 border: 1px solid silver
;
367 div
.sidebar-title
, div
.image-title
{
369 font-family: sans-serif
;
372 margin-bottom: 0.5em;
375 div
.listingblock div
.content
{
376 border: 1px solid silver
;
381 div
.quoteblock-attribution
{
386 div
.verseblock-content
{
389 div
.verseblock-attribution
{
394 div
.exampleblock-content
{
395 border-left: 2px solid silver
;
399 /* IE6 sets dynamically generated links as visited. */
400 div#toc
a:visited
{ color: blue
; }
406 gittutorial-
2(
7) Manual Page
409 <div class=
"sectionbody">
411 A tutorial introduction to git: part two
415 <h2 id=
"_synopsis">SYNOPSIS
</h2>
416 <div class=
"sectionbody">
417 <div class=
"verseblock">
418 <div class=
"verseblock-content">git *
</div>
419 <div class=
"verseblock-attribution">
422 <h2 id=
"_description">DESCRIPTION
</h2>
423 <div class=
"sectionbody">
424 <div class=
"paragraph"><p>You should work through
<a href=
"gittutorial.html">gittutorial(
7)
</a> before reading this tutorial.
</p></div>
425 <div class=
"paragraph"><p>The goal of this tutorial is to introduce two fundamental pieces of
426 git
’s architecture
—the object database and the index file
—and to
427 provide the reader with everything necessary to understand the rest
428 of the git documentation.
</p></div>
430 <h2 id=
"_the_git_object_database">The git object database
</h2>
431 <div class=
"sectionbody">
432 <div class=
"paragraph"><p>Let
’s start a new project and create a small amount of history:
</p></div>
433 <div class=
"listingblock">
434 <div class=
"content">
435 <pre><tt>$ mkdir test-project
438 Initialized empty Git repository in .git/
439 $ echo 'hello world'
> file.txt
441 $ git commit -a -m
"initial commit"
442 [master (root-commit)
54196cc] initial commit
443 1 files changed,
1 insertions(+),
0 deletions(-)
444 create mode
100644 file.txt
445 $ echo 'hello world!'
>file.txt
446 $ git commit -a -m
"add emphasis"
447 [master c4d59f3] add emphasis
448 1 files changed,
1 insertions(+),
1 deletions(-)
</tt></pre>
450 <div class=
"paragraph"><p>What are the
7 digits of hex that git responded to the commit with?
</p></div>
451 <div class=
"paragraph"><p>We saw in part one of the tutorial that commits have names like this.
452 It turns out that every object in the git history is stored under
453 a
40-digit hex name. That name is the SHA1 hash of the object
’s
454 contents; among other things, this ensures that git will never store
455 the same data twice (since identical data is given an identical SHA1
456 name), and that the contents of a git object will never change (since
457 that would change the object
’s name as well). The
7 char hex strings
458 here are simply the abbreviation of such
40 character long strings.
459 Abbreviations can be used everywhere where the
40 character strings
460 can be used, so long as they are unambiguous.
</p></div>
461 <div class=
"paragraph"><p>It is expected that the content of the commit object you created while
462 following the example above generates a different SHA1 hash than
463 the one shown above because the commit object records the time when
464 it was created and the name of the person performing the commit.
</p></div>
465 <div class=
"paragraph"><p>We can ask git about this particular object with the
<tt>cat-file
</tt>
466 command. Don
’t copy the
40 hex digits from this example but use those
467 from your own version. Note that you can shorten it to only a few
468 characters to save yourself typing all
40 hex digits:
</p></div>
469 <div class=
"listingblock">
470 <div class=
"content">
471 <pre><tt>$ git cat-file -t
54196cc2
473 $ git cat-file commit
54196cc2
474 tree
92b8b694ffb1675e5975148e1121810081dbdffe
475 author J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143414668 -
0500
476 committer J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143414668 -
0500
478 initial commit
</tt></pre>
480 <div class=
"paragraph"><p>A tree can refer to one or more
"blob" objects, each corresponding to
481 a file. In addition, a tree can also refer to other tree objects,
482 thus creating a directory hierarchy. You can examine the contents of
483 any tree using ls-tree (remember that a long enough initial portion
484 of the SHA1 will also work):
</p></div>
485 <div class=
"listingblock">
486 <div class=
"content">
487 <pre><tt>$ git ls-tree
92b8b694
488 100644 blob
3b18e512dba79e4c8300dd08aeb37f8e728b8dad file.txt
</tt></pre>
490 <div class=
"paragraph"><p>Thus we see that this tree has one file in it. The SHA1 hash is a
491 reference to that file
’s data:
</p></div>
492 <div class=
"listingblock">
493 <div class=
"content">
494 <pre><tt>$ git cat-file -t
3b18e512
497 <div class=
"paragraph"><p>A
"blob" is just file data, which we can also examine with cat-file:
</p></div>
498 <div class=
"listingblock">
499 <div class=
"content">
500 <pre><tt>$ git cat-file blob
3b18e512
501 hello world
</tt></pre>
503 <div class=
"paragraph"><p>Note that this is the old file data; so the object that git named in
504 its response to the initial tree was a tree with a snapshot of the
505 directory state that was recorded by the first commit.
</p></div>
506 <div class=
"paragraph"><p>All of these objects are stored under their SHA1 names inside the git
508 <div class=
"listingblock">
509 <div class=
"content">
510 <pre><tt>$ find .git/objects/
515 .git/objects/
3b/
18e512dba79e4c8300dd08aeb37f8e728b8dad
517 .git/objects/
92/b8b694ffb1675e5975148e1121810081dbdffe
519 .git/objects/
54/
196cc2703dc165cbd373a65a4dcf22d50ae7f7
521 .git/objects/a0/
423896973644771497bdc03eb99d5281615b51
523 .git/objects/d0/
492b368b66bdabf2ac1fd8c92b39d3db916e59
525 .git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241
</tt></pre>
527 <div class=
"paragraph"><p>and the contents of these files is just the compressed data plus a
528 header identifying their length and their type. The type is either a
529 blob, a tree, a commit, or a tag.
</p></div>
530 <div class=
"paragraph"><p>The simplest commit to find is the HEAD commit, which we can find
531 from .git/HEAD:
</p></div>
532 <div class=
"listingblock">
533 <div class=
"content">
534 <pre><tt>$ cat .git/HEAD
535 ref: refs/heads/master
</tt></pre>
537 <div class=
"paragraph"><p>As you can see, this tells us which branch we
’re currently on, and it
538 tells us this by naming a file under the .git directory, which itself
539 contains a SHA1 name referring to a commit object, which we can
540 examine with cat-file:
</p></div>
541 <div class=
"listingblock">
542 <div class=
"content">
543 <pre><tt>$ cat .git/refs/heads/master
544 c4d59f390b9cfd4318117afde11d601c1085f241
545 $ git cat-file -t c4d59f39
547 $ git cat-file commit c4d59f39
548 tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
549 parent
54196cc2703dc165cbd373a65a4dcf22d50ae7f7
550 author J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143418702 -
0500
551 committer J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143418702 -
0500
553 add emphasis
</tt></pre>
555 <div class=
"paragraph"><p>The
"tree" object here refers to the new state of the tree:
</p></div>
556 <div class=
"listingblock">
557 <div class=
"content">
558 <pre><tt>$ git ls-tree d0492b36
559 100644 blob a0423896973644771497bdc03eb99d5281615b51 file.txt
560 $ git cat-file blob a0423896
561 hello world!
</tt></pre>
563 <div class=
"paragraph"><p>and the
"parent" object refers to the previous commit:
</p></div>
564 <div class=
"listingblock">
565 <div class=
"content">
566 <pre><tt>$ git cat-file commit
54196cc2
567 tree
92b8b694ffb1675e5975148e1121810081dbdffe
568 author J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143414668 -
0500
569 committer J. Bruce Fields
<bfields@puzzle.fieldses.org
> 1143414668 -
0500
571 initial commit
</tt></pre>
573 <div class=
"paragraph"><p>The tree object is the tree we examined first, and this commit is
574 unusual in that it lacks any parent.
</p></div>
575 <div class=
"paragraph"><p>Most commits have only one parent, but it is also common for a commit
576 to have multiple parents. In that case the commit represents a
577 merge, with the parent references pointing to the heads of the merged
579 <div class=
"paragraph"><p>Besides blobs, trees, and commits, the only remaining type of object
580 is a
"tag", which we won
’t discuss here; refer to
<a href=
"git-tag.html">git-tag(
1)
</a>
581 for details.
</p></div>
582 <div class=
"paragraph"><p>So now we know how git uses the object database to represent a
583 project
’s history:
</p></div>
584 <div class=
"ulist"><ul>
587 "commit" objects refer to
"tree" objects representing the
588 snapshot of a directory tree at a particular point in the
589 history, and refer to
"parent" commits to show how they
’re
590 connected into the project history.
595 "tree" objects represent the state of a single directory,
596 associating directory names to
"blob" objects containing file
597 data and
"tree" objects containing subdirectory information.
602 "blob" objects contain file data without any other structure.
607 References to commit objects at the head of each branch are
608 stored in files under .git/refs/heads/.
613 The name of the current branch is stored in .git/HEAD.
617 <div class=
"paragraph"><p>Note, by the way, that lots of commands take a tree as an argument.
618 But as we can see above, a tree can be referred to in many different
619 ways
—by the SHA1 name for that tree, by the name of a commit that
620 refers to the tree, by the name of a branch whose head refers to that
621 tree, etc.--and most such commands can accept any of these names.
</p></div>
622 <div class=
"paragraph"><p>In command synopses, the word
"tree-ish" is sometimes used to
623 designate such an argument.
</p></div>
625 <h2 id=
"_the_index_file">The index file
</h2>
626 <div class=
"sectionbody">
627 <div class=
"paragraph"><p>The primary tool we
’ve been using to create commits is
<tt>git-commit
628 -a
</tt>, which creates a commit including every change you
’ve made to
629 your working tree. But what if you want to commit changes only to
630 certain files? Or only certain changes to certain files?
</p></div>
631 <div class=
"paragraph"><p>If we look at the way commits are created under the cover, we
’ll see
632 that there are more flexible ways creating commits.
</p></div>
633 <div class=
"paragraph"><p>Continuing with our test-project, let
’s modify file.txt again:
</p></div>
634 <div class=
"listingblock">
635 <div class=
"content">
636 <pre><tt>$ echo
"hello world, again" >>file.txt
</tt></pre>
638 <div class=
"paragraph"><p>but this time instead of immediately making the commit, let
’s take an
639 intermediate step, and ask for diffs along the way to keep track of
640 what
’s happening:
</p></div>
641 <div class=
"listingblock">
642 <div class=
"content">
650 $ git diff
</tt></pre>
652 <div class=
"paragraph"><p>The last diff is empty, but no new commits have been made, and the
653 head still doesn
’t contain the new line:
</p></div>
654 <div class=
"listingblock">
655 <div class=
"content">
656 <pre><tt>$ git diff HEAD
657 diff --git a/file.txt b/file.txt
658 index a042389.
.513feba
100644
663 +hello world, again
</tt></pre>
665 <div class=
"paragraph"><p>So
<em>git diff
</em> is comparing against something other than the head.
666 The thing that it
’s comparing against is actually the index file,
667 which is stored in .git/index in a binary format, but whose contents
668 we can examine with ls-files:
</p></div>
669 <div class=
"listingblock">
670 <div class=
"content">
671 <pre><tt>$ git ls-files --stage
672 100644 513feba2e53ebbd2532419ded848ba19de88ba00
0 file.txt
673 $ git cat-file -t
513feba2
675 $ git cat-file blob
513feba2
677 hello world, again
</tt></pre>
679 <div class=
"paragraph"><p>So what our
<em>git add
</em> did was store a new blob and then put
680 a reference to it in the index file. If we modify the file again,
681 we
’ll see that the new modifications are reflected in the
<em>git diff
</em>
683 <div class=
"listingblock">
684 <div class=
"content">
685 <pre><tt>$ echo 'again?'
>>file.txt
687 index
513feba..ba3da7b
100644
695 <div class=
"paragraph"><p>With the right arguments,
<em>git diff
</em> can also show us the difference
696 between the working directory and the last commit, or between the
697 index and the last commit:
</p></div>
698 <div class=
"listingblock">
699 <div class=
"content">
700 <pre><tt>$ git diff HEAD
701 diff --git a/file.txt b/file.txt
702 index a042389..ba3da7b
100644
710 diff --git a/file.txt b/file.txt
711 index a042389.
.513feba
100644
716 +hello world, again
</tt></pre>
718 <div class=
"paragraph"><p>At any time, we can create a new commit using
<em>git commit
</em> (without
719 the
"-a" option), and verify that the state committed only includes the
720 changes stored in the index file, not the additional change that is
721 still only in our working tree:
</p></div>
722 <div class=
"listingblock">
723 <div class=
"content">
724 <pre><tt>$ git commit -m
"repeat"
726 diff --git a/file.txt b/file.txt
727 index
513feba..ba3da7b
100644
735 <div class=
"paragraph"><p>So by default
<em>git commit
</em> uses the index to create the commit, not
736 the working tree; the
"-a" option to commit tells it to first update
737 the index with all changes in the working tree.
</p></div>
738 <div class=
"paragraph"><p>Finally, it
’s worth looking at the effect of
<em>git add
</em> on the index
740 <div class=
"listingblock">
741 <div class=
"content">
742 <pre><tt>$ echo
"goodbye, world" >closing.txt
743 $ git add closing.txt
</tt></pre>
745 <div class=
"paragraph"><p>The effect of the
<em>git add
</em> was to add one entry to the index file:
</p></div>
746 <div class=
"listingblock">
747 <div class=
"content">
748 <pre><tt>$ git ls-files --stage
749 100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138
0 closing.txt
750 100644 513feba2e53ebbd2532419ded848ba19de88ba00
0 file.txt
</tt></pre>
752 <div class=
"paragraph"><p>And, as you can see with cat-file, this new entry refers to the
753 current contents of the file:
</p></div>
754 <div class=
"listingblock">
755 <div class=
"content">
756 <pre><tt>$ git cat-file blob
8b9743b2
757 goodbye, world
</tt></pre>
759 <div class=
"paragraph"><p>The
"status" command is a useful way to get a quick summary of the
761 <div class=
"listingblock">
762 <div class=
"content">
763 <pre><tt>$ git status
765 # Changes to be committed:
766 # (use
"git reset HEAD <file>..." to unstage)
768 # new file: closing.txt
770 # Changes not staged for commit:
771 # (use
"git add <file>..." to update what will be committed)
776 <div class=
"paragraph"><p>Since the current state of closing.txt is cached in the index file,
777 it is listed as
"Changes to be committed". Since file.txt has
778 changes in the working directory that aren
’t reflected in the index,
779 it is marked
"changed but not updated". At this point, running
"git
780 commit" would create a commit that added closing.txt (with its new
781 contents), but that didn
’t modify file.txt.
</p></div>
782 <div class=
"paragraph"><p>Also, note that a bare
<tt>git diff
</tt> shows the changes to file.txt, but
783 not the addition of closing.txt, because the version of closing.txt
784 in the index file is identical to the one in the working directory.
</p></div>
785 <div class=
"paragraph"><p>In addition to being the staging area for new commits, the index file
786 is also populated from the object database when checking out a
787 branch, and is used to hold the trees involved in a merge operation.
788 See
<a href=
"gitcore-tutorial.html">gitcore-tutorial(
7)
</a> and the relevant man
789 pages for details.
</p></div>
791 <h2 id=
"_what_next">What next?
</h2>
792 <div class=
"sectionbody">
793 <div class=
"paragraph"><p>At this point you should know everything necessary to read the man
794 pages for any of the git commands; one good place to start would be
795 with the commands mentioned in
<a href=
"everyday.html">Everyday git
</a>. You
796 should be able to find any unknown jargon in
<a href=
"gitglossary.html">gitglossary(
7)
</a>.
</p></div>
797 <div class=
"paragraph"><p>The
<a href=
"user-manual.html">Git User
’s Manual
</a> provides a more
798 comprehensive introduction to git.
</p></div>
799 <div class=
"paragraph"><p><a href=
"gitcvs-migration.html">gitcvs-migration(
7)
</a> explains how to
800 import a CVS repository into git, and shows how to use git in a
801 CVS-like way.
</p></div>
802 <div class=
"paragraph"><p>For some interesting examples of git use, see the
803 <a href=
"howto-index.html">howtos
</a>.
</p></div>
804 <div class=
"paragraph"><p>For git developers,
<a href=
"gitcore-tutorial.html">gitcore-tutorial(
7)
</a> goes
805 into detail on the lower-level git mechanisms involved in, for
806 example, creating a new commit.
</p></div>
808 <h2 id=
"_see_also">SEE ALSO
</h2>
809 <div class=
"sectionbody">
810 <div class=
"paragraph"><p><a href=
"gittutorial.html">gittutorial(
7)
</a>,
811 <a href=
"gitcvs-migration.html">gitcvs-migration(
7)
</a>,
812 <a href=
"gitcore-tutorial.html">gitcore-tutorial(
7)
</a>,
813 <a href=
"gitglossary.html">gitglossary(
7)
</a>,
814 <a href=
"git-help.html">git-help(
1)
</a>,
815 <a href=
"everyday.html">Everyday git
</a>,
816 <a href=
"user-manual.html">The Git User
’s Manual
</a></p></div>
818 <h2 id=
"_git">GIT
</h2>
819 <div class=
"sectionbody">
820 <div class=
"paragraph"><p>Part of the
<a href=
"git.html">git(
1)
</a> suite.
</p></div>
823 <div id=
"footer-text">
824 Last updated
2011-
07-
23 00:
49:
30 UTC