Sub topic 4
Writing objects with git hash-object
What you need before starting
This chapter assumes you can use Git day to day (init, add, commit, log) and are comfortable in a Unix-style shell. It also assumes you know what a hash function does and have seen the .git directory layout and the idea of a content-addressable object store from earlier chapters.
Use the plumbing-demo repository from earlier. If you don't have it, create it:
git init plumbing-demo
cd plumbing-demoWhat the command does
git hash-object takes content, computes the object ID Git would give it, and optionally stores it in the object database. It is the plumbing command underneath git add. When you stage a file, Git runs the same steps for each file's contents.
The command follows these steps:
- Read the input bytes.
- Prepend a header made of the object type, a space, the content size in bytes, and a NUL byte (
blob 6\0). - Hash the header plus the content. The result is the object ID.
- With
-w, compress the header and content with zlib and write them to.git/objects/<first 2 hex digits>/<remaining digits>.
Hashing without writing
Without -w, the command only computes the ID and prints it. The object database is not touched.
echo 'hello' | git hash-object --stdin
find .git/objects -type fThe first command prints ce013625030ba8dba906f756967f9e9ca394464a. The find command prints nothing, because no object file exists yet. This mode is useful when you want to know whether Git already has some content. Without -w, the command can even run outside a repository.
Writing with -w
Add -w to store the object:
id=$(echo 'hello' | git hash-object -w --stdin)
echo "$id"
git cat-file -t "$id"
git cat-file -s "$id"
git cat-file -p "$id"
find .git/objects -type fThe ID is the same as before. cat-file reports type blob, size 6, and the content hello. The size is 6 because echo appends a newline to the five letters. The find command now lists .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a. The first two hex digits become a directory name, which keeps any one directory from growing huge.
Checking the hash yourself
The ID is not magic. You can reproduce it with a general-purpose hash tool, because the header is part of the hashed data:
printf 'blob 6\000hello\n' | sha1sumOn macOS, use shasum instead of sha1sum. The output starts with ce013625030ba8dba906f756967f9e9ca394464a, matching Git. This shows the ID depends only on the type, the size and the bytes. It does not depend on the file name, the time or the machine.
You can also look inside the stored file. Loose objects are zlib-compressed, so cat shows binary noise. Decompress the file instead:
python3 -c "import sys, zlib; print(zlib.decompress(open(sys.argv[1], 'rb').read()))" .git/objects/ce/013625030ba8dba906f756967f9e9ca394464aPython prints b'blob 6\x00hello\n'. This is the exact byte sequence that was hashed.
Same content, same object
Because the ID comes only from the content, identical content always gives the same object, whatever the file is called.
echo 'same text' > one.txt
cp one.txt two.txt
git hash-object -w one.txt two.txt
find .git/objects -type f | wc -lThe command prints the same ID twice, and only one new object file appears. Writing an object that already exists is harmless. Git sees the file is there and does nothing. This is why a repository with many copies of the same file stores that content once.
A single changed byte gives a completely different ID. Compare the hash of echo 'hello' (with a newline) to printf 'hello' (without one) and you will get two unrelated IDs.
Choosing the input
git hash-object accepts input in three ways.
| Form | Reads from | Typical use |
|---|---|---|
| `git hash-object file` | One or more named files | Hash a file on disk |
| `git hash-object --stdin` | Standard input | Hash the output of another command |
| `git hash-object --stdin-paths` | File names, one per line, on standard input | Hash many files in a single run |
The --stdin-paths form saves starting a new process per file:
printf 'one.txt\ntwo.txt\n' | git hash-object -w --stdin-pathsIt prints one ID per line, in input order. File names containing newlines cannot be passed this way.
Filters and --path
When you hash a file by name, Git does not always hash the raw bytes on disk. It applies the same conversions that git add would: line-ending conversion from core.autocrlf or the text attribute, and any clean filters set in .gitattributes. This keeps hash-object consistent with what staging stores.
Two options control this:
--no-filtershashes the raw bytes and skips all conversion.--path=<name>is for--stdin. Standard input has no file name, so filter rules cannot be matched.--pathtells Git which name to use when it looks up attributes.
echo 'data' | git hash-object --stdin --path=notes.txt
echo 'data' | git hash-object --stdin --no-filtersIf a hash from hash-object file differs from the hash of the same bytes sent through --stdin, a filter is the usual cause.
Object types and validation
The default type is blob. The -t option sets another type: blob, tree, commit or tag.
echo 'not a real commit' | git hash-object -t commit --stdinThis fails. When Git is asked to write a typed object, it checks that the content is well formed for that type. A commit needs a tree line, author and committer lines, and a message, and this text has none of them. The check keeps you from storing objects that break later commands.
The --literally option skips the check and also allows arbitrary type names. It exists for testing how Git handles damaged objects, and for little else. Objects written this way can make git fsck report errors, so do not use it in a repository you care about.
In normal work you rarely call hash-object -t tree or -t commit. The commands git write-tree and git commit-tree build those objects for you, and later chapters use them.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The ID is printed but `.git/objects` is empty | `-w` was left out | Run the command again with `-w` |
| `cat-file` says the object does not exist after hashing | The ID was computed but never written | Hash again with `-w` |
| The hash differs from a colleague's for the "same" file | Line endings or a clean filter changed the bytes | Compare with `--no-filters` and check `.gitattributes` and `core.autocrlf` |
| The hash differs between `echo` and `printf` | `echo` added a trailing newline | Use `printf` when you need exact bytes |
| `hash-object -t commit` or `-t tree` is rejected | The content is not a valid object of that type | Build it with `commit-tree` or `write-tree`, or use `--literally` for experiments |
| `--stdin-paths` fails on a name | The name contains a newline | Hash such files individually |
Where this leads
You can now put raw content into the database and get back an ID. A blob stores only content. It has no name, no directory and no history. The next chapters read these objects back with git cat-file and then connect blobs to names using trees.
Exercise
You run echo 'abc' | git hash-object --stdin and get an ID, but git cat-file -p <that ID> reports that the object does not exist. Why, and what single change fixes it? Also, if you then copy the content into three differently named files and hash each with -w, how many new object files appear?
Show answer
Without -w, hash-object only computes the ID and never writes the object, so the database has nothing to read. Adding -w stores it. The three files would add only one object file in total (the first write creates it, and the others find it already present), because the ID depends only on the type, size and bytes, not the file name.
Exercise
A file hashes to one ID with git hash-object notes.txt, but the same bytes piped with git hash-object --stdin give a different ID. Name one likely cause and one way to confirm it.
Show answer
A filter is likely being applied to the named file, such as line-ending conversion from core.autocrlf or the text attribute, or a clean filter from .gitattributes. Confirm by running git hash-object --no-filters notes.txt and comparing it with the --stdin result. If they now match, a filter changed the bytes. You can also pass --path=notes.txt to --stdin so the same attribute rules apply.