Sub topic 5
Reading objects with git cat-file
This chapter assumes you know basic Git use, are comfortable in a Unix-style shell, and have seen git hash-object -w write a blob. Here you learn the reverse: reading any object back out of the object database.
What cat-file does
git cat-file takes an object name and prints information about that object. The name can be a full hash, a unique abbreviation of at least four characters, or a revision expression such as HEAD^{tree}. The command works on every object type: blob, tree, commit, and annotated tag.
Inspecting a blob
Create a repository and store one blob. Then query it with each option.
git init cat-file-demo
cd cat-file-demo
id=$(echo 'hello' | git hash-object -w --stdin)
git cat-file -t "$id"
git cat-file -s "$id"
git cat-file -p "$id"The output is blob, then 6, then hello. The size is 6 because the content is the five letters plus the newline that echo adds. The size counts the content only. It does not include the header Git stores internally.
Choosing between -p and a type name
-p looks at the object's type and formats the content for reading. Passing the type name instead, as in git cat-file blob "$id", prints the raw content. For a blob the two outputs are identical. For a tree they differ: -p prints readable text, while the raw form is binary data. Use the type name when you pipe a blob into another program, and use -p when you read the output yourself.
Inspecting a tree and a commit
Add the blob to the index under a name, write a tree, and wrap it in a commit.
git update-index --add --cacheinfo 100644 "$id" hello.txt
tree=$(git write-tree)
git cat-file -p "$tree"
commit=$(echo 'first commit' | git -c user.name=Demo -c [email protected] commit-tree "$tree")
git cat-file -p "$commit"The tree prints one line per entry, with the mode, the type, the hash, and the name. Here that is 100644 blob, the hash of the blob, then hello.txt. The commit prints a tree line, author and committer lines with timestamps, a blank line, and the message. A commit that has parents would also show parent lines.
Testing existence and scripting
-e prints nothing. It exits with status 0 if the object exists and with a nonzero status if it does not. This makes it useful in scripts.
git cat-file -e "$commit" && echo "commit exists"
git cat-file -e 0000000000000000000000000000000000000001 || echo "no such object"To query many objects at once, use --batch-check. It reads one object name per line from standard input and prints <hash> <type> <size> for each. This is much faster than starting a separate process for every object.
printf '%s\n' "$id" "$tree" "$commit" | git cat-file --batch-checkCommon errors
fatal: Not a valid object name: the hash is mistyped, the abbreviation is too short, or the object was never written. Check the name withgit cat-file -e.error: short object ID ... is ambiguous: more than one object starts with those characters. Use more characters.- Running a command outside the repository, which gives
fatal: not a git repository. - Garbled terminal output from
git cat-file tree "$tree". Use-pfor trees.
Exercise
You run git cat-file -s on a blob created from the text abc written with echo. What size does it print, and why?
Show answer
It prints 4. The content is three letters plus the newline that echo appends, and the size counts only the content bytes.
Exercise
Which command tells a script whether an object exists without printing anything, and how does it report the answer?
Show answer
git cat-file -e <name>. It reports through its exit status: 0 if the object exists and nonzero if it does not.