Sub topic 5
Reading objects with git cat-file
This chapter assumes you can run basic Git commands and use a Unix-style shell. It also assumes you know that a hash function turns any input into a fixed-length fingerprint. You should have seen git hash-object -w write a blob. Here you learn the matching read command, git cat-file.
What git cat-file does
git cat-file takes the name of an object and prints something about it. The name can be a full hash, an abbreviated hash, or any revision expression Git understands, such as HEAD or main~2. The command has three families of behavior:
- Metadata: the type of the object (
-t), its size (-s), or whether it exists (-e). - Content: a readable rendering (
-p) or the raw bytes (a type name such asblob). - Batch: a long-running mode that reads many names from standard input (
--batch,--batch-check).
Setting up a repository to read
Create a small repository with porcelain commands. The rest of the chapter reads what these commands stored.
git init cat-file-demo
cd cat-file-demo
git config user.name "Demo User"
git config user.email "[email protected]"
echo 'hello' > hello.txt
mkdir docs
echo 'notes' > docs/notes.txt
git add .
git commit -m 'first commit'The commit contains three objects besides itself: the blob for hello.txt, the blob for docs/notes.txt, and two trees (the root and docs). That makes five objects in total.
Asking for type and size
git cat-file -t HEAD
git cat-file -t 'HEAD^{tree}'
git cat-file -t HEAD:hello.txt
git cat-file -s HEAD:hello.txtThe first three commands print commit, tree and blob. The last prints 6, because the file holds hello plus a newline.
The size is the length of the object's content in bytes. It does not include the header Git writes in front of the content before hashing.
The revision syntax does much of the work here:
| Expression | Meaning |
|---|---|
| `HEAD` | The commit that HEAD resolves to |
| `HEAD^{tree}` | The root tree of that commit |
| `HEAD:docs` | The tree stored at path `docs` in that commit |
| `HEAD:docs/notes.txt` | The blob at that path |
| `:hello.txt` | The blob staged in the index for that path |
| `HEAD~1` | The first parent of the commit |
Quote expressions containing ^{} so the shell does not treat the braces specially.
Pretty-printing with -p
The -p option figures out the type and prints a readable form.
git cat-file -p HEAD:hello.txt
git cat-file -p 'HEAD^{tree}'
git cat-file -p HEADFor the blob, -p prints the file content. For the tree, it prints one line per entry: mode, type, hash, a tab, and the name.
040000 tree <hash> docs
100644 blob ce013625030ba8dba906f756967f9e9ca394464a hello.txtFor the commit, it prints header lines, a blank line, and the message:
tree <hash>
author Demo User <[email protected]> <timestamp> <timezone>
committer Demo User <[email protected]> <timestamp> <timezone>
first commitA commit with a parent also has a parent <hash> line after tree. A merge commit has several.
The output of -p is for people. For a tree, the real stored form is binary, so scripts should not parse it. Use git ls-tree for tree entries, which has a stable text format.
Raw output with a type name
Instead of an option, you can pass a type name followed by the object name:
git cat-file blob HEAD:hello.txt
git cat-file commit HEAD
git cat-file tree 'HEAD^{tree}' | od -c | headThe first two outputs match -p, because blobs and commits are plain text. The third differs from -p. A tree entry is stored as an ASCII mode, a space, the file name, a NUL byte, and then the hash as 20 raw bytes, not 40 hex characters. The od -c command shows the NUL bytes and the unreadable hash bytes.
Passing the type also acts as an assertion. If the object is not of that type, Git exits with an error. This makes the form useful in scripts that must not read the wrong kind of object. There is one convenience: Git will peel a commit or tag down to the requested type where that makes sense.
git cat-file tree HEADThis prints the root tree, because a commit points at exactly one tree. In contrast, git cat-file blob HEAD fails, since a commit does not resolve to a single blob.
Testing existence with -e
The -e option prints nothing and reports through its exit status.
git cat-file -e HEAD:hello.txt && echo "exists"
git cat-file -e 0000000000000000000000000000000000000001 || echo "missing"The second line uses a syntactically valid 40-digit hash that no object has. Git exits non-zero without printing anything, so the shell prints missing.
A name that cannot be resolved at all, such as HEAD:no-such-file, also exits non-zero but prints a fatal: message. In an if statement both cases look the same, because both are failures.
Reading many objects with --batch
Starting a process per object is slow when you need thousands of lookups. The batch modes read names from standard input, one per line, and answer on standard output.
printf '%s\n' HEAD 'HEAD^{tree}' HEAD:hello.txt | git cat-file --batch-checkEach answer has the form <hash> <type> <size>:
<commit-hash> commit 218
<tree-hash> tree 64
ce013625030ba8dba906f756967f9e9ca394464a blob 6The commit and tree sizes depend on your author name and timestamp, so yours will differ. Only the blob line is the same on every machine.
--batch prints that same header line followed by the content and a trailing newline:
echo HEAD:hello.txt | git cat-file --batchce013625030ba8dba906f756967f9e9ca394464a blob 6
hello
The blank line is the delimiter after the content hello\n. A reader program uses the size in the header to know exactly how many bytes to consume, so it works for binary content too. If a name does not resolve, the output is <name> missing.
You can control the header with a format string:
printf '%s\n' HEAD:hello.txt HEAD:docs/notes.txt \
| git cat-file --batch-check='%(objecttype) %(objectsize) %(rest)'%(rest) echoes any text that followed the name on the input line. That lets you attach your own label to each query.
To list every object in the database, loose or packed, add --batch-all-objects:
git cat-file --batch-all-objects --batch-checkThis does not follow refs. It also lists unreachable objects, which makes it a useful companion to git fsck in a later chapter.
Choosing the right form
Common errors
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `fatal: Not a valid object name <name>` | The name does not resolve, for example a typo in a path or a hash that is too short | Check with `git rev-parse <name>` and `git ls-tree` |
| `fatal: ambiguous argument` or an ambiguity warning for a short hash | Several objects share that prefix | Use more hex digits |
| `fatal: git cat-file: could not get object info` | The hash is well formed but no such object exists in this repository | Check that you are in the right repository |
| `-p` output looks different from the file on disk | Git stores bytes without any end-of-line or filter conversion, while your working tree file may have been converted | Compare with `git cat-file blob` for the exact stored bytes |
| `git cat-file -p` on a tree shows no hashes you can paste | You passed a tree name but wanted a blob | Name the entry, for example `HEAD:hello.txt` |
| Script breaks on tree output | The script parsed `-p` text or raw tree bytes | Use `git ls-tree` |
Exercise
In the demo repository, which two commands print the stored type and size of the blob for docs/notes.txt in the latest commit, and what size do you expect?
Show answer
Run git cat-file -t HEAD:docs/notes.txt (prints blob) and git cat-file -s HEAD:docs/notes.txt. The file holds notes plus a newline, which is 6 bytes, so the size is 6.
Exercise
You have a list of 5,000 commit hashes in hashes.txt, one per line, and you want to know which ones are missing from the repository. Which form do you use, and how do you spot the missing ones?
Show answer
Pipe the file into one process: git cat-file --batch-check < hashes.txt. Each line of output is <hash> <type> <size> for objects that exist and <hash> missing for those that do not, so grep missing shows the absent ones. A single process avoids starting 5,000 separate commands.