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