1GITTUTORIAL-2(7)                  Git Manual                  GITTUTORIAL-2(7)
2
3
4

NAME

6       gittutorial-2 - A tutorial introduction to git: part two
7

SYNOPSIS

9       git *
10

DESCRIPTION

12       You should work through gittutorial(7) before reading this tutorial.
13
14       The goal of this tutorial is to introduce two fundamental pieces of
15       git’s architecture—the object database and the index file—and to
16       provide the reader with everything necessary to understand the rest of
17       the git documentation.
18

THE GIT OBJECT DATABASE

20       Let’s start a new project and create a small amount of history:
21
22           $ mkdir test-project
23           $ cd test-project
24           $ git init
25           Initialized empty Git repository in .git/
26           $ echo ´hello world´ > file.txt
27           $ git add .
28           $ git commit -a -m "initial commit"
29           [master (root-commit) 54196cc] initial commit
30            1 files changed, 1 insertions(+), 0 deletions(-)
31            create mode 100644 file.txt
32           $ echo ´hello world!´ >file.txt
33           $ git commit -a -m "add emphasis"
34           [master c4d59f3] add emphasis
35            1 files changed, 1 insertions(+), 1 deletions(-)
36
37
38       What are the 7 digits of hex that git responded to the commit with?
39
40       We saw in part one of the tutorial that commits have names like this.
41       It turns out that every object in the git history is stored under a
42       40-digit hex name. That name is the SHA1 hash of the object’s contents;
43       among other things, this ensures that git will never store the same
44       data twice (since identical data is given an identical SHA1 name), and
45       that the contents of a git object will never change (since that would
46       change the object’s name as well). The 7 char hex strings here are
47       simply the abbreviation of such 40 character long strings.
48       Abbreviations can be used everywhere where the 40 character strings can
49       be used, so long as they are unambiguous.
50
51       It is expected that the content of the commit object you created while
52       following the example above generates a different SHA1 hash than the
53       one shown above because the commit object records the time when it was
54       created and the name of the person performing the commit.
55
56       We can ask git about this particular object with the cat-file command.
57       Don’t copy the 40 hex digits from this example but use those from your
58       own version. Note that you can shorten it to only a few characters to
59       save yourself typing all 40 hex digits:
60
61           $ git cat-file -t 54196cc2
62           commit
63           $ git cat-file commit 54196cc2
64           tree 92b8b694ffb1675e5975148e1121810081dbdffe
65           author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
66           committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
67
68           initial commit
69
70
71       A tree can refer to one or more "blob" objects, each corresponding to a
72       file. In addition, a tree can also refer to other tree objects, thus
73       creating a directory hierarchy. You can examine the contents of any
74       tree using ls-tree (remember that a long enough initial portion of the
75       SHA1 will also work):
76
77           $ git ls-tree 92b8b694
78           100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad    file.txt
79
80
81       Thus we see that this tree has one file in it. The SHA1 hash is a
82       reference to that file’s data:
83
84           $ git cat-file -t 3b18e512
85           blob
86
87
88       A "blob" is just file data, which we can also examine with cat-file:
89
90           $ git cat-file blob 3b18e512
91           hello world
92
93
94       Note that this is the old file data; so the object that git named in
95       its response to the initial tree was a tree with a snapshot of the
96       directory state that was recorded by the first commit.
97
98       All of these objects are stored under their SHA1 names inside the git
99       directory:
100
101           $ find .git/objects/
102           .git/objects/
103           .git/objects/pack
104           .git/objects/info
105           .git/objects/3b
106           .git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad
107           .git/objects/92
108           .git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe
109           .git/objects/54
110           .git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7
111           .git/objects/a0
112           .git/objects/a0/423896973644771497bdc03eb99d5281615b51
113           .git/objects/d0
114           .git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59
115           .git/objects/c4
116           .git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241
117
118
119       and the contents of these files is just the compressed data plus a
120       header identifying their length and their type. The type is either a
121       blob, a tree, a commit, or a tag.
122
123       The simplest commit to find is the HEAD commit, which we can find from
124       .git/HEAD:
125
126           $ cat .git/HEAD
127           ref: refs/heads/master
128
129
130       As you can see, this tells us which branch we’re currently on, and it
131       tells us this by naming a file under the .git directory, which itself
132       contains a SHA1 name referring to a commit object, which we can examine
133       with cat-file:
134
135           $ cat .git/refs/heads/master
136           c4d59f390b9cfd4318117afde11d601c1085f241
137           $ git cat-file -t c4d59f39
138           commit
139           $ git cat-file commit c4d59f39
140           tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
141           parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
142           author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500
143           committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500
144
145           add emphasis
146
147
148       The "tree" object here refers to the new state of the tree:
149
150           $ git ls-tree d0492b36
151           100644 blob a0423896973644771497bdc03eb99d5281615b51    file.txt
152           $ git cat-file blob a0423896
153           hello world!
154
155
156       and the "parent" object refers to the previous commit:
157
158           $ git cat-file commit 54196cc2
159           tree 92b8b694ffb1675e5975148e1121810081dbdffe
160           author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
161           committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
162
163           initial commit
164
165
166       The tree object is the tree we examined first, and this commit is
167       unusual in that it lacks any parent.
168
169       Most commits have only one parent, but it is also common for a commit
170       to have multiple parents. In that case the commit represents a merge,
171       with the parent references pointing to the heads of the merged
172       branches.
173
174       Besides blobs, trees, and commits, the only remaining type of object is
175       a "tag", which we won’t discuss here; refer to git-tag(1) for details.
176
177       So now we know how git uses the object database to represent a
178       project’s history:
179
180       ·   "commit" objects refer to "tree" objects representing the snapshot
181           of a directory tree at a particular point in the history, and refer
182           to "parent" commits to show how they’re connected into the project
183           history.
184
185       ·   "tree" objects represent the state of a single directory,
186           associating directory names to "blob" objects containing file data
187           and "tree" objects containing subdirectory information.
188
189       ·   "blob" objects contain file data without any other structure.
190
191       ·   References to commit objects at the head of each branch are stored
192           in files under .git/refs/heads/.
193
194       ·   The name of the current branch is stored in .git/HEAD.
195
196       Note, by the way, that lots of commands take a tree as an argument. But
197       as we can see above, a tree can be referred to in many different ways—
198       by the SHA1 name for that tree, by the name of a commit that refers to
199       the tree, by the name of a branch whose head refers to that tree,
200       etc.--and most such commands can accept any of these names.
201
202       In command synopses, the word "tree-ish" is sometimes used to designate
203       such an argument.
204

THE INDEX FILE

206       The primary tool we’ve been using to create commits is git-commit -a,
207       which creates a commit including every change you’ve made to your
208       working tree. But what if you want to commit changes only to certain
209       files? Or only certain changes to certain files?
210
211       If we look at the way commits are created under the cover, we’ll see
212       that there are more flexible ways creating commits.
213
214       Continuing with our test-project, let’s modify file.txt again:
215
216           $ echo "hello world, again" >>file.txt
217
218
219       but this time instead of immediately making the commit, let’s take an
220       intermediate step, and ask for diffs along the way to keep track of
221       what’s happening:
222
223           $ git diff
224           --- a/file.txt
225           +++ b/file.txt
226           @@ -1 +1,2 @@
227            hello world!
228           +hello world, again
229           $ git add file.txt
230           $ git diff
231
232
233       The last diff is empty, but no new commits have been made, and the head
234       still doesn’t contain the new line:
235
236           $ git diff HEAD
237           diff --git a/file.txt b/file.txt
238           index a042389..513feba 100644
239           --- a/file.txt
240           +++ b/file.txt
241           @@ -1 +1,2 @@
242            hello world!
243           +hello world, again
244
245
246       So git diff is comparing against something other than the head. The
247       thing that it’s comparing against is actually the index file, which is
248       stored in .git/index in a binary format, but whose contents we can
249       examine with ls-files:
250
251           $ git ls-files --stage
252           100644 513feba2e53ebbd2532419ded848ba19de88ba00 0       file.txt
253           $ git cat-file -t 513feba2
254           blob
255           $ git cat-file blob 513feba2
256           hello world!
257           hello world, again
258
259
260       So what our git add did was store a new blob and then put a reference
261       to it in the index file. If we modify the file again, we’ll see that
262       the new modifications are reflected in the git diff output:
263
264           $ echo ´again?´ >>file.txt
265           $ git diff
266           index 513feba..ba3da7b 100644
267           --- a/file.txt
268           +++ b/file.txt
269           @@ -1,2 +1,3 @@
270            hello world!
271            hello world, again
272           +again?
273
274
275       With the right arguments, git diff can also show us the difference
276       between the working directory and the last commit, or between the index
277       and the last commit:
278
279           $ git diff HEAD
280           diff --git a/file.txt b/file.txt
281           index a042389..ba3da7b 100644
282           --- a/file.txt
283           +++ b/file.txt
284           @@ -1 +1,3 @@
285            hello world!
286           +hello world, again
287           +again?
288           $ git diff --cached
289           diff --git a/file.txt b/file.txt
290           index a042389..513feba 100644
291           --- a/file.txt
292           +++ b/file.txt
293           @@ -1 +1,2 @@
294            hello world!
295           +hello world, again
296
297
298       At any time, we can create a new commit using git commit (without the
299       "-a" option), and verify that the state committed only includes the
300       changes stored in the index file, not the additional change that is
301       still only in our working tree:
302
303           $ git commit -m "repeat"
304           $ git diff HEAD
305           diff --git a/file.txt b/file.txt
306           index 513feba..ba3da7b 100644
307           --- a/file.txt
308           +++ b/file.txt
309           @@ -1,2 +1,3 @@
310            hello world!
311            hello world, again
312           +again?
313
314
315       So by default git commit uses the index to create the commit, not the
316       working tree; the "-a" option to commit tells it to first update the
317       index with all changes in the working tree.
318
319       Finally, it’s worth looking at the effect of git add on the index file:
320
321           $ echo "goodbye, world" >closing.txt
322           $ git add closing.txt
323
324
325       The effect of the git add was to add one entry to the index file:
326
327           $ git ls-files --stage
328           100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0       closing.txt
329           100644 513feba2e53ebbd2532419ded848ba19de88ba00 0       file.txt
330
331
332       And, as you can see with cat-file, this new entry refers to the current
333       contents of the file:
334
335           $ git cat-file blob 8b9743b2
336           goodbye, world
337
338
339       The "status" command is a useful way to get a quick summary of the
340       situation:
341
342           $ git status
343           # On branch master
344           # Changes to be committed:
345           #   (use "git reset HEAD <file>..." to unstage)
346           #
347           #       new file: closing.txt
348           #
349           # Changed but not updated:
350           #   (use "git add <file>..." to update what will be committed)
351           #
352           #       modified: file.txt
353           #
354
355
356       Since the current state of closing.txt is cached in the index file, it
357       is listed as "Changes to be committed". Since file.txt has changes in
358       the working directory that aren’t reflected in the index, it is marked
359       "changed but not updated". At this point, running "git commit" would
360       create a commit that added closing.txt (with its new contents), but
361       that didn’t modify file.txt.
362
363       Also, note that a bare git diff shows the changes to file.txt, but not
364       the addition of closing.txt, because the version of closing.txt in the
365       index file is identical to the one in the working directory.
366
367       In addition to being the staging area for new commits, the index file
368       is also populated from the object database when checking out a branch,
369       and is used to hold the trees involved in a merge operation. See
370       gitcore-tutorial(7) and the relevant man pages for details.
371

WHAT NEXT?

373       At this point you should know everything necessary to read the man
374       pages for any of the git commands; one good place to start would be
375       with the commands mentioned in Everyday git[1]. You should be able to
376       find any unknown jargon in gitglossary(7).
377
378       The Git User’s Manual[2] provides a more comprehensive introduction to
379       git.
380
381       gitcvs-migration(7) explains how to import a CVS repository into git,
382       and shows how to use git in a CVS-like way.
383
384       For some interesting examples of git use, see the howtos[3].
385
386       For git developers, gitcore-tutorial(7) goes into detail on the
387       lower-level git mechanisms involved in, for example, creating a new
388       commit.
389

SEE ALSO

391       gittutorial(7), gitcvs-migration(7), gitcore-tutorial(7),
392       gitglossary(7), git-help(1), Everyday git[1], The Git User’s Manual[2]
393

GIT

395       Part of the git(1) suite.
396

NOTES

398        1. Everyday git
399           file:///usr/share/doc/git-1.7.1/everyday.html
400
401        2. Git User’s Manual
402           file:///usr/share/doc/git-1.7.1/user-manual.html
403
404        3. howtos
405           file:///usr/share/doc/git-1.7.1/howto-index.html
406
407
408
409Git 1.7.1                         08/16/2017                  GITTUTORIAL-2(7)
Impressum