Skip to content

Commit 509a047

Browse files
committed
Merge branch 'dg/user-manual-hash-example'
User manual (the original one) update. * dg/user-manual-hash-example: Documentation/user-manual.txt: example for generating object hashes
2 parents 81ba11b + 28636d7 commit 509a047

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

Documentation/user-manual.txt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,7 +4093,38 @@ that not only specifies their type, but also provides size information
40934093
about the data in the object. It's worth noting that the SHA-1 hash
40944094
that is used to name the object is the hash of the original data
40954095
plus this header, so `sha1sum` 'file' does not match the object name
4096-
for 'file'.
4096+
for 'file' (the earliest versions of Git hashed slightly differently
4097+
but the conclusion is still the same).
4098+
4099+
The following is a short example that demonstrates how these hashes
4100+
can be generated manually:
4101+
4102+
Let's assume a small text file with some simple content:
4103+
4104+
-------------------------------------------------
4105+
$ echo "Hello world" >hello.txt
4106+
-------------------------------------------------
4107+
4108+
We can now manually generate the hash Git would use for this file:
4109+
4110+
- The object we want the hash for is of type "blob" and its size is
4111+
12 bytes.
4112+
4113+
- Prepend the object header to the file content and feed this to
4114+
`sha1sum`:
4115+
4116+
-------------------------------------------------
4117+
$ { printf "blob 12\0"; cat hello.txt; } | sha1sum
4118+
802992c4220de19a90767f3000a79a31b98d0df7 -
4119+
-------------------------------------------------
4120+
4121+
This manually constructed hash can be verified using `git hash-object`
4122+
which of course hides the addition of the header:
4123+
4124+
-------------------------------------------------
4125+
$ git hash-object hello.txt
4126+
802992c4220de19a90767f3000a79a31b98d0df7
4127+
-------------------------------------------------
40974128

40984129
As a result, the general consistency of an object can always be tested
40994130
independently of the contents or the type of the object: all objects can
@@ -4123,7 +4154,8 @@ $ git switch --detach e83c5163
41234154
----------------------------------------------------
41244155

41254156
The initial revision lays the foundation for almost everything Git has
4126-
today, but is small enough to read in one sitting.
4157+
today (even though details may differ in a few places), but is small
4158+
enough to read in one sitting.
41274159

41284160
Note that terminology has changed since that revision. For example, the
41294161
README in that revision uses the word "changeset" to describe what we

0 commit comments

Comments
 (0)