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