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

THE INDEX FILE

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

WHAT NEXT?

352       At this point you should know everything necessary to read the man
353       pages for any of the git commands; one good place to start would be
354       with the commands mentioned in giteveryday(7). You should be able to
355       find any unknown jargon in gitglossary(7).
356
357       The Git User’s Manual[1] provides a more comprehensive introduction to
358       Git.
359
360       gitcvs-migration(7) explains how to import a CVS repository into Git,
361       and shows how to use Git in a CVS-like way.
362
363       For some interesting examples of Git use, see the howtos[2].
364
365       For Git developers, gitcore-tutorial(7) goes into detail on the
366       lower-level Git mechanisms involved in, for example, creating a new
367       commit.
368

SEE ALSO

370       gittutorial(7), gitcvs-migration(7), gitcore-tutorial(7),
371       gitglossary(7), git-help(1), giteveryday(7), The Git User’s Manual[1]
372

GIT

374       Part of the git(1) suite
375

NOTES

377        1. Git User’s Manual
378           file:///usr/share/doc/git/user-manual.html
379
380        2. howtos
381           file:///usr/share/doc/git/howto-index.html
382
383
384
385Git 2.30.2                        2021-03-08                  GITTUTORIAL-2(7)
Impressum