You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/10-git-internals/sections/objects.asc
+18-15Lines changed: 18 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,11 @@ Git is a content-addressable filesystem.
5
5
Great.
6
6
What does that mean?
7
7
It means that at the core of Git is a simple key-value data store.
8
-
You can insert any kind of content into it, and it will give you back a key that you can use to retrieve the content again at any time.
9
-
To demonstrate, you can use the plumbing command `hash-object`, which takes some data, stores it in your `.git/objects` directory (the _object database_), and gives you back the key the data is stored as.
8
+
What this means it that you can insert any kind of content into a Git repository, for which Git will hand you back a unique key you can use later to retrieve that content.
10
9
11
-
First, you initialize a new Git repository and verify that there is nothing in the `objects` directory:
10
+
As a demonstration, let's look at the plumbing command `git hash-object`, which takes some data, stores it in your `.git/objects` directory (the _object database_), and gives you back the unique key that now refers to that data object.
11
+
12
+
First, you initialize a new Git repository and verify that there is (predictably) nothing in the `objects` directory:
12
13
13
14
[source,console]
14
15
----
@@ -23,18 +24,20 @@ $ find .git/objects -type f
23
24
----
24
25
25
26
Git has initialized the `objects` directory and created `pack` and `info` subdirectories in it, but there are no regular files.
26
-
Now, store some text in your Git database:
27
+
Now, let's use `git hash-object` to create a new data object and manually store it in your new Git database:
The `-w` tells `hash-object` to store the object; otherwise, the command simply tells you what the key would be.
35
-
`--stdin` tells the command to read the content from stdin; if you don't specify this, `hash-object` expects a file path at the end.
36
-
The output from the command is a 40-character checksum hash.
37
-
This is the SHA-1 hash – a checksum of the content you're storing plus a header, which you'll learn about in a bit.
35
+
In its simplest form, `git hash-object` would take the content you handed to it and merely return the unique key that _would_ be used to store it in your Git database.
36
+
The `-w` option then tells the command to not simply return the key, but to write that object to the database.
37
+
Finally, the `--stdin` option tells `git hash-object` to get the content to be processed from stdin; otherwise, the command would expect a filename argument at the end of the command containing the content to be used.
38
+
39
+
The output from the above command is a 40-character checksum hash.
40
+
This is the SHA-1 hash -- a checksum of the content you're storing plus a header, which you'll learn about in a bit.
Now you can revert the file back to the first version
94
+
At this point, you can delete your local copy of that `test.txt` file, then use Git to retrieve, from the object database, either the first version you saved:
92
95
93
96
[source,console]
94
97
----
@@ -106,7 +109,7 @@ $ cat test.txt
106
109
version 2
107
110
----
108
111
109
-
But remembering the SHA-1 key for each version of your file isn't practical; plus, you aren't storing the filename in your system – just the content.
112
+
But remembering the SHA-1 key for each version of your file isn't practical; plus, you aren't storing the filename in your system -- just the content.
110
113
This object type is called a _blob_.
111
114
You can have Git tell you the object type of any object in Git, given its SHA-1 key, with `cat-file -t`:
0 commit comments