Sub topic 4
Looking inside with git cat-file
This chapter assumes you can use git init, add, commit, and log, and that you are comfortable in a Unix-style shell. Earlier chapters showed that Git stores data as objects named by the hash of their content. Here you learn the tool for reading those objects: git cat-file.
What cat-file does
git cat-file takes an object name and reports something about that object. It is a plumbing command. Git provides it for scripts and for inspection, so its output is stable and plain.
The three options you will use most:
| Option | Reports |
|---|---|
| `-t` | The object's type: `blob`, `tree`, `commit`, or `tag` |
| `-s` | The size of the object's content in bytes |
| `-p` | The content, pretty-printed according to the type |
A first inspection
Create a blob and ask about it:
git init inspect-demo && cd inspect-demo
blob=$(echo 'hello' | git hash-object -w --stdin)
git cat-file -t "$blob"
git cat-file -s "$blob"
git cat-file -p "$blob"The output is blob, then 6, then hello. The size is 6 because echo adds a newline after the five letters. This is the same number Git writes in the object header.
Names you can pass
The argument does not have to be a full 40-character ID. Git accepts anything it can resolve to an object:
- A unique abbreviation of an ID, such as the first seven characters.
- A branch name, a tag name, or
HEAD. - A revision expression, such as
HEAD~1orHEAD^{tree}.
git cat-file -t "${blob:0:7}"
git cat-file -e "$blob" && echo "object exists"The -e option prints nothing. It only sets the exit status to 0 when the object exists, which makes it useful in scripts.
Once the repository has a commit, you can follow a branch name down to the objects behind it. Committing needs user.name and user.email to be set.
git commit --allow-empty -m 'first commit'
git cat-file -t HEAD
git cat-file -p HEAD
git cat-file -t 'HEAD^{tree}'
git cat-file -p 'HEAD^{tree}'HEAD resolves to a commit, and HEAD^{tree} resolves to the tree that commit points to. The tree is empty here because the commit was empty. Later chapters explain what each type's output means. For now, notice that -p formats each type differently, while -t and -s work on all of them.
Common errors
fatal: Not a valid object name: Git could not resolve what you typed. Check for typos. Also check that you passed an object name and not a file path.git cat-file -p greeting.txtfails unless a ref has that name. UseHEAD:greeting.txtto name the blob at that path in the last commit.error: short object ID ... is ambiguous: Your abbreviation matches more than one object. Add characters until it is unique.- Using
-pon the wrong thing: Giving a tag name to-pprints the tag object, not the commit it points to, if the tag is annotated. Use-tfirst to see what you have. - Missing identity on commit: If
git commitstops with "Author identity unknown", setuser.nameanduser.emailand retry.
Exercise
You run git cat-file -s "$blob" on a blob created with printf 'hi' | git hash-object -w --stdin. What size does it print, and why is it different from the 6 you saw for echo 'hello'?
Show answer
It prints 2. The printf command adds no trailing newline, so the content is the two bytes h and i. The earlier blob was five letters plus the newline that echo appends, which is 6 bytes.
Exercise
Which command shows only the type of the tree that the current commit points to?
Show answer
git cat-file -t 'HEAD^{tree}', which prints tree.