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