]>
Commit | Line | Data |
---|---|---|
b27a23e3 CC |
1 | gittutorial-2(7) |
2 | ================ | |
e31952da | 3 | |
b27a23e3 CC |
4 | NAME |
5 | ---- | |
6 | gittutorial-2 - A tutorial introduction to git: part two | |
7 | ||
8 | SYNOPSIS | |
9 | -------- | |
10 | git * | |
11 | ||
12 | DESCRIPTION | |
13 | ----------- | |
14 | ||
6998e4db | 15 | You should work through linkgit:gittutorial[7] before reading this tutorial. |
e31952da BF |
16 | |
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. | |
21 | ||
22 | The git object database | |
23 | ----------------------- | |
24 | ||
25 | Let's start a new project and create a small amount of history: | |
26 | ||
27 | ------------------------------------------------ | |
28 | $ mkdir test-project | |
29 | $ cd test-project | |
515377ea | 30 | $ git init |
ef0a89a6 | 31 | Initialized empty Git repository in .git/ |
e31952da BF |
32 | $ echo 'hello world' > file.txt |
33 | $ git add . | |
34 | $ git commit -a -m "initial commit" | |
27a58359 | 35 | [master (root-commit) 54196cc] initial commit |
b724fd09 | 36 | 1 files changed, 1 insertions(+), 0 deletions(-) |
61f5cb7f | 37 | create mode 100644 file.txt |
e31952da BF |
38 | $ echo 'hello world!' >file.txt |
39 | $ git commit -a -m "add emphasis" | |
27a58359 | 40 | [master c4d59f3] add emphasis |
b724fd09 | 41 | 1 files changed, 1 insertions(+), 1 deletions(-) |
e31952da BF |
42 | ------------------------------------------------ |
43 | ||
72c69ebc | 44 | What are the 7 digits of hex that git responded to the commit with? |
e31952da BF |
45 | |
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 | |
72c69ebc | 48 | a 40-digit hex name. That name is the SHA1 hash of the object's |
e31952da BF |
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 | |
72c69ebc AE |
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. | |
e31952da | 56 | |
ebd124c6 NP |
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. | |
61 | ||
483bc4f0 | 62 | We can ask git about this particular object with the `cat-file` |
ebd124c6 NP |
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: | |
e31952da BF |
66 | |
67 | ------------------------------------------------ | |
d54467b8 | 68 | $ git cat-file -t 54196cc2 |
ebd124c6 | 69 | commit |
d54467b8 | 70 | $ git cat-file commit 54196cc2 |
ebd124c6 NP |
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 | |
74 | ||
75 | initial commit | |
e31952da BF |
76 | ------------------------------------------------ |
77 | ||
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, | |
abda1ef5 | 80 | thus creating a directory hierarchy. You can examine the contents of |
e31952da BF |
81 | any tree using ls-tree (remember that a long enough initial portion |
82 | of the SHA1 will also work): | |
83 | ||
84 | ------------------------------------------------ | |
85 | $ git ls-tree 92b8b694 | |
86 | 100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad file.txt | |
87 | ------------------------------------------------ | |
88 | ||
89 | Thus we see that this tree has one file in it. The SHA1 hash is a | |
90 | reference to that file's data: | |
91 | ||
92 | ------------------------------------------------ | |
93 | $ git cat-file -t 3b18e512 | |
94 | blob | |
95 | ------------------------------------------------ | |
96 | ||
97 | A "blob" is just file data, which we can also examine with cat-file: | |
98 | ||
99 | ------------------------------------------------ | |
100 | $ git cat-file blob 3b18e512 | |
101 | hello world | |
102 | ------------------------------------------------ | |
103 | ||
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. | |
107 | ||
108 | All of these objects are stored under their SHA1 names inside the git | |
109 | directory: | |
110 | ||
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 | ------------------------------------------------ | |
129 | ||
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 | |
ebd124c6 | 132 | blob, a tree, a commit, or a tag. |
e31952da BF |
133 | |
134 | The simplest commit to find is the HEAD commit, which we can find | |
135 | from .git/HEAD: | |
136 | ||
137 | ------------------------------------------------ | |
138 | $ cat .git/HEAD | |
139 | ref: refs/heads/master | |
140 | ------------------------------------------------ | |
141 | ||
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: | |
146 | ||
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 | |
157 | ||
158 | add emphasis | |
159 | ------------------------------------------------ | |
160 | ||
161 | The "tree" object here refers to the new state of the tree: | |
162 | ||
163 | ------------------------------------------------ | |
164 | $ git ls-tree d0492b36 | |
165 | 100644 blob a0423896973644771497bdc03eb99d5281615b51 file.txt | |
2befe6fe | 166 | $ git cat-file blob a0423896 |
e31952da BF |
167 | hello world! |
168 | ------------------------------------------------ | |
169 | ||
170 | and the "parent" object refers to the previous commit: | |
171 | ||
172 | ------------------------------------------------ | |
d54467b8 | 173 | $ git cat-file commit 54196cc2 |
e31952da BF |
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 | |
177 | ||
178 | initial commit | |
179 | ------------------------------------------------ | |
180 | ||
181 | The tree object is the tree we examined first, and this commit is | |
182 | unusual in that it lacks any parent. | |
183 | ||
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. | |
188 | ||
189 | Besides blobs, trees, and commits, the only remaining type of object | |
5162e697 | 190 | is a "tag", which we won't discuss here; refer to linkgit:git-tag[1] |
e31952da BF |
191 | for details. |
192 | ||
193 | So now we know how git uses the object database to represent a | |
194 | project's history: | |
195 | ||
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. | |
207 | ||
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. | |
213 | ||
214 | In command synopses, the word "tree-ish" is sometimes used to | |
215 | designate such an argument. | |
216 | ||
217 | The index file | |
218 | -------------- | |
219 | ||
483bc4f0 JN |
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 | |
e31952da BF |
222 | your working tree. But what if you want to commit changes only to |
223 | certain files? Or only certain changes to certain files? | |
224 | ||
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. | |
227 | ||
228 | Continuing with our test-project, let's modify file.txt again: | |
229 | ||
230 | ------------------------------------------------ | |
231 | $ echo "hello world, again" >>file.txt | |
232 | ------------------------------------------------ | |
233 | ||
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: | |
237 | ||
238 | ------------------------------------------------ | |
239 | $ git diff | |
240 | --- a/file.txt | |
241 | +++ b/file.txt | |
242 | @@ -1 +1,2 @@ | |
243 | hello world! | |
d5e3d60c | 244 | +hello world, again |
d7f078b8 | 245 | $ git add file.txt |
e31952da BF |
246 | $ git diff |
247 | ------------------------------------------------ | |
248 | ||
249 | The last diff is empty, but no new commits have been made, and the | |
250 | head still doesn't contain the new line: | |
251 | ||
252 | ------------------------------------------------ | |
d54467b8 | 253 | $ git diff HEAD |
e31952da BF |
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! | |
d5e3d60c | 260 | +hello world, again |
e31952da BF |
261 | ------------------------------------------------ |
262 | ||
0b444cdb | 263 | So 'git diff' is comparing against something other than the head. |
e31952da BF |
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: | |
267 | ||
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 | |
1d17c25c | 274 | hello world! |
e31952da BF |
275 | hello world, again |
276 | ------------------------------------------------ | |
277 | ||
0b444cdb | 278 | So what our 'git add' did was store a new blob and then put |
e31952da | 279 | a reference to it in the index file. If we modify the file again, |
0b444cdb | 280 | we'll see that the new modifications are reflected in the 'git diff' |
e31952da BF |
281 | output: |
282 | ||
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 | ------------------------------------------------ | |
294 | ||
0b444cdb | 295 | With the right arguments, 'git diff' can also show us the difference |
e31952da BF |
296 | between the working directory and the last commit, or between the |
297 | index and the last commit: | |
298 | ||
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 | ------------------------------------------------ | |
318 | ||
0b444cdb | 319 | At any time, we can create a new commit using 'git commit' (without |
483bc4f0 | 320 | the "-a" option), and verify that the state committed only includes the |
e31952da BF |
321 | changes stored in the index file, not the additional change that is |
322 | still only in our working tree: | |
323 | ||
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 | ------------------------------------------------ | |
336 | ||
0b444cdb | 337 | So by default 'git commit' uses the index to create the commit, not |
483bc4f0 | 338 | the working tree; the "-a" option to commit tells it to first update |
e31952da BF |
339 | the index with all changes in the working tree. |
340 | ||
0b444cdb | 341 | Finally, it's worth looking at the effect of 'git add' on the index |
e31952da BF |
342 | file: |
343 | ||
344 | ------------------------------------------------ | |
345 | $ echo "goodbye, world" >closing.txt | |
346 | $ git add closing.txt | |
347 | ------------------------------------------------ | |
348 | ||
0b444cdb | 349 | The effect of the 'git add' was to add one entry to the index file: |
e31952da BF |
350 | |
351 | ------------------------------------------------ | |
352 | $ git ls-files --stage | |
353 | 100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0 closing.txt | |
354 | 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt | |
355 | ------------------------------------------------ | |
356 | ||
357 | And, as you can see with cat-file, this new entry refers to the | |
358 | current contents of the file: | |
359 | ||
360 | ------------------------------------------------ | |
1bb91460 JH |
361 | $ git cat-file blob 8b9743b2 |
362 | goodbye, world | |
e31952da BF |
363 | ------------------------------------------------ |
364 | ||
365 | The "status" command is a useful way to get a quick summary of the | |
366 | situation: | |
367 | ||
368 | ------------------------------------------------ | |
369 | $ git status | |
bf3478de JH |
370 | # On branch master |
371 | # Changes to be committed: | |
372 | # (use "git reset HEAD <file>..." to unstage) | |
e31952da BF |
373 | # |
374 | # new file: closing.txt | |
375 | # | |
bf3478de JH |
376 | # Changed but not updated: |
377 | # (use "git add <file>..." to update what will be committed) | |
e31952da BF |
378 | # |
379 | # modified: file.txt | |
380 | # | |
381 | ------------------------------------------------ | |
382 | ||
383 | Since the current state of closing.txt is cached in the index file, | |
bf3478de | 384 | it is listed as "Changes to be committed". Since file.txt has |
e31952da | 385 | changes in the working directory that aren't reflected in the index, |
bf3478de | 386 | it is marked "changed but not updated". At this point, running "git |
e31952da BF |
387 | commit" would create a commit that added closing.txt (with its new |
388 | contents), but that didn't modify file.txt. | |
389 | ||
483bc4f0 | 390 | Also, note that a bare `git diff` shows the changes to file.txt, but |
e31952da BF |
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. | |
393 | ||
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. | |
6998e4db | 397 | See linkgit:gitcore-tutorial[7] and the relevant man |
e31952da BF |
398 | pages for details. |
399 | ||
400 | What next? | |
401 | ---------- | |
402 | ||
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 | |
884e3134 | 405 | with the commands mentioned in link:everyday.html[Everyday git]. You |
6998e4db | 406 | should be able to find any unknown jargon in linkgit:gitglossary[7]. |
e31952da | 407 | |
cd50aba9 BF |
408 | The link:user-manual.html[Git User's Manual] provides a more |
409 | comprehensive introduction to git. | |
410 | ||
6998e4db | 411 | linkgit:gitcvs-migration[7] explains how to |
e31952da BF |
412 | import a CVS repository into git, and shows how to use git in a |
413 | CVS-like way. | |
414 | ||
415 | For some interesting examples of git use, see the | |
416 | link:howto-index.html[howtos]. | |
417 | ||
6998e4db | 418 | For git developers, linkgit:gitcore-tutorial[7] goes |
e31952da BF |
419 | into detail on the lower-level git mechanisms involved in, for |
420 | example, creating a new commit. | |
b27a23e3 CC |
421 | |
422 | SEE ALSO | |
423 | -------- | |
424 | linkgit:gittutorial[7], | |
425 | linkgit:gitcvs-migration[7], | |
497c8331 CC |
426 | linkgit:gitcore-tutorial[7], |
427 | linkgit:gitglossary[7], | |
6e702c24 | 428 | linkgit:git-help[1], |
b27a23e3 CC |
429 | link:everyday.html[Everyday git], |
430 | link:user-manual.html[The Git User's Manual] | |
431 | ||
432 | GIT | |
433 | --- | |
9e1f0a85 | 434 | Part of the linkgit:git[1] suite. |