1 <?xml version="1.0" encoding="UTF-8"?>
\r
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
\r
4 <article lang="en" id="gittutorial-2(7)">
\r
6 <title>gittutorial-2(7)</title>
\r
8 <primary>gittutorial-2(7)</primary>
\r
11 <simplesect id="_name">
\r
13 <simpara>gittutorial-2 - A tutorial introduction to git: part two</simpara>
\r
15 <simplesect id="_synopsis">
\r
16 <title>SYNOPSIS</title>
\r
17 <simpara>git *</simpara>
\r
19 <simplesect id="_description">
\r
20 <title>DESCRIPTION</title>
\r
21 <simpara>You should work through <xref linkend="gittutorial(7)"/> before reading this tutorial.</simpara>
\r
22 <simpara>The goal of this tutorial is to introduce two fundamental pieces of
\r
23 git’s architecture—the object database and the index file—and to
\r
24 provide the reader with everything necessary to understand the rest
\r
25 of the git documentation.</simpara>
\r
27 <simplesect id="_the_git_object_database">
\r
28 <title>The git object database</title>
\r
29 <simpara>Let’s start a new project and create a small amount of history:</simpara>
\r
30 <literallayout>$ mkdir test-project
\r
33 Initialized empty Git repository in .git/
\r
34 $ echo 'hello world' > file.txt
\r
36 $ git commit -a -m "initial commit"
\r
37 [master (root-commit) 54196cc] initial commit
\r
38 1 files changed, 1 insertions(+), 0 deletions(-)
\r
39 create mode 100644 file.txt
\r
40 $ echo 'hello world!' >file.txt
\r
41 $ git commit -a -m "add emphasis"
\r
42 [master c4d59f3] add emphasis
\r
43 1 files changed, 1 insertions(+), 1 deletions(-)</literallayout>
\r
44 <simpara>What are the 7 digits of hex that git responded to the commit with?</simpara>
\r
45 <simpara>We saw in part one of the tutorial that commits have names like this.
\r
46 It turns out that every object in the git history is stored under
\r
47 a 40-digit hex name. That name is the SHA1 hash of the object’s
\r
48 contents; among other things, this ensures that git will never store
\r
49 the same data twice (since identical data is given an identical SHA1
\r
50 name), and that the contents of a git object will never change (since
\r
51 that would change the object’s name as well). The 7 char hex strings
\r
52 here are simply the abbreviation of such 40 character long strings.
\r
53 Abbreviations can be used everywhere where the 40 character strings
\r
54 can be used, so long as they are unambiguous.</simpara>
\r
55 <simpara>It is expected that the content of the commit object you created while
\r
56 following the example above generates a different SHA1 hash than
\r
57 the one shown above because the commit object records the time when
\r
58 it was created and the name of the person performing the commit.</simpara>
\r
59 <simpara>We can ask git about this particular object with the <literal>cat-file</literal>
\r
60 command. Don’t copy the 40 hex digits from this example but use those
\r
61 from your own version. Note that you can shorten it to only a few
\r
62 characters to save yourself typing all 40 hex digits:</simpara>
\r
63 <literallayout>$ git cat-file -t 54196cc2
\r
65 $ git cat-file commit 54196cc2
\r
66 tree 92b8b694ffb1675e5975148e1121810081dbdffe
\r
67 author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
\r
68 committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
\r
70 initial commit</literallayout>
\r
71 <simpara>A tree can refer to one or more "blob" objects, each corresponding to
\r
72 a file. In addition, a tree can also refer to other tree objects,
\r
73 thus creating a directory hierarchy. You can examine the contents of
\r
74 any tree using ls-tree (remember that a long enough initial portion
\r
75 of the SHA1 will also work):</simpara>
\r
76 <literallayout>$ git ls-tree 92b8b694
\r
77 100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad file.txt</literallayout>
\r
78 <simpara>Thus we see that this tree has one file in it. The SHA1 hash is a
\r
79 reference to that file’s data:</simpara>
\r
80 <literallayout>$ git cat-file -t 3b18e512
\r
81 blob</literallayout>
\r
82 <simpara>A "blob" is just file data, which we can also examine with cat-file:</simpara>
\r
83 <literallayout>$ git cat-file blob 3b18e512
\r
84 hello world</literallayout>
\r
85 <simpara>Note that this is the old file data; so the object that git named in
\r
86 its response to the initial tree was a tree with a snapshot of the
\r
87 directory state that was recorded by the first commit.</simpara>
\r
88 <simpara>All of these objects are stored under their SHA1 names inside the git
\r
89 directory:</simpara>
\r
90 <literallayout>$ find .git/objects/
\r
95 .git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad
\r
97 .git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe
\r
99 .git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7
\r
101 .git/objects/a0/423896973644771497bdc03eb99d5281615b51
\r
103 .git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59
\r
105 .git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241</literallayout>
\r
106 <simpara>and the contents of these files is just the compressed data plus a
\r
107 header identifying their length and their type. The type is either a
\r
108 blob, a tree, a commit, or a tag.</simpara>
\r
109 <simpara>The simplest commit to find is the HEAD commit, which we can find
\r
110 from .git/HEAD:</simpara>
\r
111 <literallayout>$ cat .git/HEAD
\r
112 ref: refs/heads/master</literallayout>
\r
113 <simpara>As you can see, this tells us which branch we’re currently on, and it
\r
114 tells us this by naming a file under the .git directory, which itself
\r
115 contains a SHA1 name referring to a commit object, which we can
\r
116 examine with cat-file:</simpara>
\r
117 <literallayout>$ cat .git/refs/heads/master
\r
118 c4d59f390b9cfd4318117afde11d601c1085f241
\r
119 $ git cat-file -t c4d59f39
\r
121 $ git cat-file commit c4d59f39
\r
122 tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
\r
123 parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
\r
124 author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500
\r
125 committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500
\r
127 add emphasis</literallayout>
\r
128 <simpara>The "tree" object here refers to the new state of the tree:</simpara>
\r
129 <literallayout>$ git ls-tree d0492b36
\r
130 100644 blob a0423896973644771497bdc03eb99d5281615b51 file.txt
\r
131 $ git cat-file blob a0423896
\r
132 hello world!</literallayout>
\r
133 <simpara>and the "parent" object refers to the previous commit:</simpara>
\r
134 <literallayout>$ git cat-file commit 54196cc2
\r
135 tree 92b8b694ffb1675e5975148e1121810081dbdffe
\r
136 author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
\r
137 committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
\r
139 initial commit</literallayout>
\r
140 <simpara>The tree object is the tree we examined first, and this commit is
\r
141 unusual in that it lacks any parent.</simpara>
\r
142 <simpara>Most commits have only one parent, but it is also common for a commit
\r
143 to have multiple parents. In that case the commit represents a
\r
144 merge, with the parent references pointing to the heads of the merged
\r
145 branches.</simpara>
\r
146 <simpara>Besides blobs, trees, and commits, the only remaining type of object
\r
147 is a "tag", which we won’t discuss here; refer to <xref linkend="git-tag(1)"/>
\r
148 for details.</simpara>
\r
149 <simpara>So now we know how git uses the object database to represent a
\r
150 project’s history:</simpara>
\r
154 "commit" objects refer to "tree" objects representing the
\r
155 snapshot of a directory tree at a particular point in the
\r
156 history, and refer to "parent" commits to show how they’re
\r
157 connected into the project history.
\r
162 "tree" objects represent the state of a single directory,
\r
163 associating directory names to "blob" objects containing file
\r
164 data and "tree" objects containing subdirectory information.
\r
169 "blob" objects contain file data without any other structure.
\r
174 References to commit objects at the head of each branch are
\r
175 stored in files under .git/refs/heads/.
\r
180 The name of the current branch is stored in .git/HEAD.
\r
184 <simpara>Note, by the way, that lots of commands take a tree as an argument.
\r
185 But as we can see above, a tree can be referred to in many different
\r
186 ways—by the SHA1 name for that tree, by the name of a commit that
\r
187 refers to the tree, by the name of a branch whose head refers to that
\r
188 tree, etc.--and most such commands can accept any of these names.</simpara>
\r
189 <simpara>In command synopses, the word "tree-ish" is sometimes used to
\r
190 designate such an argument.</simpara>
\r
192 <simplesect id="_the_index_file">
\r
193 <title>The index file</title>
\r
194 <simpara>The primary tool we’ve been using to create commits is <literal>git-commit
\r
195 -a</literal>, which creates a commit including every change you’ve made to
\r
196 your working tree. But what if you want to commit changes only to
\r
197 certain files? Or only certain changes to certain files?</simpara>
\r
198 <simpara>If we look at the way commits are created under the cover, we’ll see
\r
199 that there are more flexible ways creating commits.</simpara>
\r
200 <simpara>Continuing with our test-project, let’s modify file.txt again:</simpara>
\r
201 <literallayout>$ echo "hello world, again" >>file.txt</literallayout>
\r
202 <simpara>but this time instead of immediately making the commit, let’s take an
\r
203 intermediate step, and ask for diffs along the way to keep track of
\r
204 what’s happening:</simpara>
\r
205 <literallayout>$ git diff
\r
210 +hello world, again
\r
212 $ git diff</literallayout>
\r
213 <simpara>The last diff is empty, but no new commits have been made, and the
\r
214 head still doesn’t contain the new line:</simpara>
\r
215 <literallayout>$ git diff HEAD
\r
216 diff --git a/file.txt b/file.txt
\r
217 index a042389..513feba 100644
\r
222 +hello world, again</literallayout>
\r
223 <simpara>So <emphasis>git-diff</emphasis> is comparing against something other than the head.
\r
224 The thing that it’s comparing against is actually the index file,
\r
225 which is stored in .git/index in a binary format, but whose contents
\r
226 we can examine with ls-files:</simpara>
\r
227 <literallayout>$ git ls-files --stage
\r
228 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt
\r
229 $ git cat-file -t 513feba2
\r
231 $ git cat-file blob 513feba2
\r
233 hello world, again</literallayout>
\r
234 <simpara>So what our <emphasis>git-add</emphasis> did was store a new blob and then put
\r
235 a reference to it in the index file. If we modify the file again,
\r
236 we’ll see that the new modifications are reflected in the <emphasis>git-diff</emphasis>
\r
238 <literallayout>$ echo 'again?' >>file.txt
\r
240 index 513feba..ba3da7b 100644
\r
246 +again?</literallayout>
\r
247 <simpara>With the right arguments, <emphasis>git-diff</emphasis> can also show us the difference
\r
248 between the working directory and the last commit, or between the
\r
249 index and the last commit:</simpara>
\r
250 <literallayout>$ git diff HEAD
\r
251 diff --git a/file.txt b/file.txt
\r
252 index a042389..ba3da7b 100644
\r
257 +hello world, again
\r
259 $ git diff --cached
\r
260 diff --git a/file.txt b/file.txt
\r
261 index a042389..513feba 100644
\r
266 +hello world, again</literallayout>
\r
267 <simpara>At any time, we can create a new commit using <emphasis>git-commit</emphasis> (without
\r
268 the "-a" option), and verify that the state committed only includes the
\r
269 changes stored in the index file, not the additional change that is
\r
270 still only in our working tree:</simpara>
\r
271 <literallayout>$ git commit -m "repeat"
\r
273 diff --git a/file.txt b/file.txt
\r
274 index 513feba..ba3da7b 100644
\r
280 +again?</literallayout>
\r
281 <simpara>So by default <emphasis>git-commit</emphasis> uses the index to create the commit, not
\r
282 the working tree; the "-a" option to commit tells it to first update
\r
283 the index with all changes in the working tree.</simpara>
\r
284 <simpara>Finally, it’s worth looking at the effect of <emphasis>git-add</emphasis> on the index
\r
286 <literallayout>$ echo "goodbye, world" >closing.txt
\r
287 $ git add closing.txt</literallayout>
\r
288 <simpara>The effect of the <emphasis>git-add</emphasis> was to add one entry to the index file:</simpara>
\r
289 <literallayout>$ git ls-files --stage
\r
290 100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0 closing.txt
\r
291 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt</literallayout>
\r
292 <simpara>And, as you can see with cat-file, this new entry refers to the
\r
293 current contents of the file:</simpara>
\r
294 <literallayout>$ git cat-file blob 8b9743b2
\r
295 goodbye, world</literallayout>
\r
296 <simpara>The "status" command is a useful way to get a quick summary of the
\r
297 situation:</simpara>
\r
298 <literallayout>$ git status
\r
300 # Changes to be committed:
\r
301 # (use "git reset HEAD <file>..." to unstage)
\r
303 # new file: closing.txt
\r
305 # Changed but not updated:
\r
306 # (use "git add <file>..." to update what will be committed)
\r
308 # modified: file.txt
\r
310 <simpara>Since the current state of closing.txt is cached in the index file,
\r
311 it is listed as "Changes to be committed". Since file.txt has
\r
312 changes in the working directory that aren’t reflected in the index,
\r
313 it is marked "changed but not updated". At this point, running "git
\r
314 commit" would create a commit that added closing.txt (with its new
\r
315 contents), but that didn’t modify file.txt.</simpara>
\r
316 <simpara>Also, note that a bare <literal>git diff</literal> shows the changes to file.txt, but
\r
317 not the addition of closing.txt, because the version of closing.txt
\r
318 in the index file is identical to the one in the working directory.</simpara>
\r
319 <simpara>In addition to being the staging area for new commits, the index file
\r
320 is also populated from the object database when checking out a
\r
321 branch, and is used to hold the trees involved in a merge operation.
\r
322 See <xref linkend="gitcore-tutorial(7)"/> and the relevant man
\r
323 pages for details.</simpara>
\r
325 <simplesect id="_what_next">
\r
326 <title>What next?</title>
\r
327 <simpara>At this point you should know everything necessary to read the man
\r
328 pages for any of the git commands; one good place to start would be
\r
329 with the commands mentioned in <ulink url="everyday.html">Everyday git</ulink>. You
\r
330 should be able to find any unknown jargon in <xref linkend="gitglossary(7)"/>.</simpara>
\r
331 <simpara>The <ulink url="user-manual.html">Git User’s Manual</ulink> provides a more
\r
332 comprehensive introduction to git.</simpara>
\r
333 <simpara><xref linkend="gitcvs-migration(7)"/> explains how to
\r
334 import a CVS repository into git, and shows how to use git in a
\r
335 CVS-like way.</simpara>
\r
336 <simpara>For some interesting examples of git use, see the
\r
337 <ulink url="howto-index.html">howtos</ulink>.</simpara>
\r
338 <simpara>For git developers, <xref linkend="gitcore-tutorial(7)"/> goes
\r
339 into detail on the lower-level git mechanisms involved in, for
\r
340 example, creating a new commit.</simpara>
\r
342 <simplesect id="_see_also">
\r
343 <title>SEE ALSO</title>
\r
344 <simpara><xref linkend="gittutorial(7)"/>,
\r
345 <xref linkend="gitcvs-migration(7)"/>,
\r
346 <xref linkend="gitcore-tutorial(7)"/>,
\r
347 <xref linkend="gitglossary(7)"/>,
\r
348 <xref linkend="git-help(1)"/>,
\r
349 <ulink url="everyday.html">Everyday git</ulink>,
\r
350 <ulink url="user-manual.html">The Git User’s Manual</ulink></simpara>
\r
352 <simplesect id="_git">
\r
354 <simpara>Part of the <xref linkend="git(1)"/> suite.</simpara>
\r