How to View Commits and Files Inside Git with git cat-file
Running git cat-file -p <object> shows the contents of a commit or file that Git stores in the repository, exactly as Git keeps them. The command is useful when you are learning how Git works, when you want to check what a commit records, or when a script needs to check whether a commit or file exists. I used git cat-file to open each object one by one and confirm what was added to .git/objects before and after git add and git commit.
The test environment was Ubuntu 24.04, Git 2.43.0, and Python 3.11. I set the author name and email to example values (Neko <neko@example.com>). To keep the output the same every time, I fixed the commit timestamp with environment variables. I saved the output to files, so there are no colors and no pager was used. I did not test on Windows or macOS, and I did not look at the contents of pack files.
- 1. Git Stores Four Kinds of Objects
- 2. Before add, There Are No Objects
- 3. After add, the File Contents Are Stored as a Blob
- 4. After commit, a Tree and a Commit Are Added
- 5. In the Second Commit, Unchanged Files Keep the Same Blob
- 6. Only an Annotated Tag Becomes a tag Object
- 7. Specifying a Type Follows the Object to the Target
- 8. Checking Whether an Object Exists in a Script
- 9. Looking Does Not Change the Repository
Git Stores Four Kinds of Objects
Git stores file contents and commits as "objects" in .git/objects. There are four kinds of objects.
- blob: the contents of a file. It does not have a file name.
- tree: a folder. It is a list of the file names inside it, and the hash of the blob or tree for each one.
- commit: a commit. It holds the top-level tree, the parent commit, the author, and the message.
- tag: an annotated tag. It holds the object it points to (usually a commit), the person who created the tag, and the message.
git cat-file shows the type (-t), the size (-s), or the contents (-p) of an object that you specify by hash or branch name. The full list of options is in the official git cat-file reference.
Before add, There Are No Objects
I created an empty repository named neko-cafe and put two files in it, menu.txt and docs/hours.md. I had not run git add yet. When I searched for files in .git/objects in this state, nothing was found.
$ find .git/objects -type f
$ git cat-file -t HEAD
fatal: Not a valid object name HEAD
There is no commit yet, so HEAD points to nothing, and the command fails.
After add, the File Contents Are Stored as a Blob
When I ran git add on menu.txt alone, one file was added to .git/objects.
$ git add menu.txt
$ find .git/objects -type f
.git/objects/80/86847e639a5eb690e946ee413e5fd421d8e72e
$ git ls-files --stage
100644 8086847e639a5eb690e946ee413e5fd421d8e72e 0 menu.txt
The hash of this object is 8086847e…, made by joining the folder name 80 and the file name 86847e…. In the output of git ls-files --stage, the menu.txt in the stage points to this hash. The hash calculated from the file with git hash-object was the same.
$ git hash-object menu.txt
8086847e639a5eb690e946ee413e5fd421d8e72e
I passed this hash to git cat-file. You can shorten a hash to its first few characters, as long as the result is still unique.
$ git cat-file -t 8086847
blob
$ git cat-file -s 8086847
34
$ wc -c < menu.txt
34
$ git cat-file -p 8086847
コーヒー 400円
紅茶 380円
-t showed that the type is blob, and -s showed that the size is 34 bytes. This matches the byte count from wc -c. -p printed the contents. A blob does not include a file name, so only the contents are shown.
The Object File Cannot Be Read As-Is
The files in .git/objects are compressed with zlib. When I decompressed one with Python's zlib, the data began with blob 34 and a \x00 (null byte) separator. After that came the contents of menu.txt in UTF-8.
$ python3 -c "import sys,zlib; print(repr(zlib.decompress(open(sys.argv[1],\"rb\").read())))" .git/objects/80/86847*
b'blob 34\x00\xe3\x82\xb3\xe3\x83\xbc\xe3\x83\x92\xe3\x83\xbc 400\xe5\x86\x86\n\xe7\xb4\x85\xe8\x8c\xb6 380\xe5\x86\x86\n'
The section "Git Objects" in Pro Git also says that Git adds a header to the contents made of the type, a space, the byte count, and a null byte, and then compresses the result with zlib before saving it. git cat-file decompresses the file, reads the header, and shows the type, size, and contents.
After commit, a Tree and a Commit Are Added
I also added docs/hours.md and made the first commit. There were now five objects.
$ git add docs/hours.md && git commit -q -m "メニューを作る"
$ find .git/objects -type f | sort
.git/objects/39/f565ae51efe85a8898139ee9f812348ab885bb
.git/objects/58/40ace4bc2ea9ceecd385357bf5e9c66b8d3017
.git/objects/66/cc3aefc48c1477bbbcca2bab158e08f1327226
.git/objects/80/86847e639a5eb690e946ee413e5fd421d8e72e
.git/objects/96/3fb323c5704e50a3f6b47647a015cbef8477d4
The hashes alone do not tell you what each object is, so I used --batch-check with --batch-all-objects to list the type and size of every object in the repository.
$ git cat-file --batch-check --batch-all-objects
39f565ae51efe85a8898139ee9f812348ab885bb tree 67
5840ace4bc2ea9ceecd385357bf5e9c66b8d3017 tree 36
66cc3aefc48c1477bbbcca2bab158e08f1327226 commit 168
8086847e639a5eb690e946ee413e5fd421d8e72e blob 34
963fb323c5704e50a3f6b47647a015cbef8477d4 blob 39
There are two blobs (the contents of menu.txt and hours.md), two trees (the top-level folder and docs), and one commit.
Viewing the Contents of a Commit
When I opened HEAD with -p, the information recorded in the commit was shown as is.
$ git cat-file -t HEAD
commit
$ git cat-file -p HEAD
tree 39f565ae51efe85a8898139ee9f812348ab885bb
author Neko <neko@example.com> 1794445200 +0900
committer Neko <neko@example.com> 1794445200 +0900
メニューを作る
Unlike git show, which displays a commit, this output contains no diff. A commit holds only the hash of the top-level tree, the author and the committer with their dates, and the message. 1794445200 is the number of seconds since 00:00 on January 1, 1970 (UTC). When I converted it with date, it was 2026-11-12 10:00:00 +0900. Because this is the first commit, there is no parent line that points to an earlier commit.
Following the Tree to the File Contents
You can specify the tree that a commit points to with HEAD^{tree}. ^{tree} is the notation that means "the tree of that commit."
$ git cat-file -p "HEAD^{tree}"
040000 tree 5840ace4bc2ea9ceecd385357bf5e9c66b8d3017 docs
100644 blob 8086847e639a5eb690e946ee413e5fd421d8e72e menu.txt
$ git cat-file -p HEAD:docs
100644 blob 963fb323c5704e50a3f6b47647a015cbef8477d4 hours.md
$ git cat-file -p HEAD:docs/hours.md
# Business hours
10時から18時まで
Each line of a tree has the mode, the type, the hash, and the name, in that order. 100644 is a regular file, and 040000 is a folder. You can see that file names are stored in the tree, not in the blob. Writing commit:path, as in HEAD:docs, lets you point directly to the tree or blob at that path. This notation is described in the official gitrevisions reference.
In the Second Commit, Unchanged Files Keep the Same Blob
I changed the price of coffee in menu.txt to 450 yen and made a second commit.
$ git commit -q -am "コーヒーを値上げする"
$ git cat-file -p HEAD
tree e2695d6afed2266a3d9c2db34268c8eafcd1fd00
parent 66cc3aefc48c1477bbbcca2bab158e08f1327226
author Neko <neko@example.com> 1794447000 +0900
committer Neko <neko@example.com> 1794447000 +0900
コーヒーを値上げする
This time there is a parent line, which points to the first commit 66cc3ae…. The history is made by following these parent lines in order.
When I compared the trees of the two commits, the blob for menu.txt had changed, but the docs tree had the same hash as before.
$ git cat-file -p "HEAD^{tree}"
040000 tree 5840ace4bc2ea9ceecd385357bf5e9c66b8d3017 docs
100644 blob 4a12d56a4c2851f524deb815f704dfbbc946c1ad menu.txt
$ git cat-file -p "HEAD~1^{tree}"
040000 tree 5840ace4bc2ea9ceecd385357bf5e9c66b8d3017 docs
100644 blob 8086847e639a5eb690e946ee413e5fd421d8e72e menu.txt
The list of all objects also grew from five to eight. Only three were added: the new blob for menu.txt (4a12d56…), the new top-level tree (e2695d6…), and the new commit (eb8b624…). The blob for hours.md and the docs tree did not change, so they were not created again, and the existing ones are reused.
$ git cat-file --batch-check --batch-all-objects
39f565ae51efe85a8898139ee9f812348ab885bb tree 67
4a12d56a4c2851f524deb815f704dfbbc946c1ad blob 34
5840ace4bc2ea9ceecd385357bf5e9c66b8d3017 tree 36
66cc3aefc48c1477bbbcca2bab158e08f1327226 commit 168
8086847e639a5eb690e946ee413e5fd421d8e72e blob 34
963fb323c5704e50a3f6b47647a015cbef8477d4 blob 39
e2695d6afed2266a3d9c2db34268c8eafcd1fd00 tree 67
eb8b624a7c18bdf7e6402388a4c353fcc89f289c commit 225
Only an Annotated Tag Becomes a tag Object
I created a lightweight tag v1.0-light and an annotated tag v1.0 (with -a) on the first commit, and compared their types.
$ git tag v1.0-light HEAD~1
$ git tag -a v1.0 -m "最初の版" HEAD~1
$ git cat-file -t v1.0-light
commit
$ git cat-file -t v1.0
tag
$ git cat-file -p v1.0
object 66cc3aefc48c1477bbbcca2bab158e08f1327226
type commit
tag v1.0
tagger Neko <neko@example.com> 1794447000 +0900
最初の版
A lightweight tag is just a name that points directly to a commit, so its type is shown as commit. An annotated tag creates a tag object, which contains the commit it points to (object), the person who created the tag (tagger), and the message.
Specifying a Type Follows the Object to the Target
If you write a type such as commit or blob in place of -p, the contents are shown as that type. According to the official reference, you can also specify types that can be reached easily, such as going from a tag to a commit, or from a commit to a tree. v1.0 is a tag object, but when I specified commit, the contents of the commit it leads to were shown.
$ git cat-file commit v1.0
tree 39f565ae51efe85a8898139ee9f812348ab885bb
author Neko <neko@example.com> 1794445200 +0900
committer Neko <neko@example.com> 1794445200 +0900
メニューを作る
$ git cat-file blob HEAD:menu.txt
コーヒー 450円
紅茶 380円
You cannot reach a blob from a commit, so specifying blob for HEAD gave an error.
$ git cat-file blob HEAD
fatal: git cat-file HEAD: bad file
-p formats the output to be readable for the type, but when you specify a type, the decompressed contents are shown as they are. The difference is clear when you compare trees. When I used od -c to show the bytes one at a time, there was a null byte (\0) after each file name, and the hash was stored as 20 bytes of binary data, not as text. The folder mode was also stored as 40000, without the leading zero. -p shows it as 040000, padded to six digits.
$ git cat-file tree HEAD | od -c | head -5
0000000 4 0 0 0 0 d o c s \0 X @ 254 344 274
0000020 . 251 316 354 323 205 5 { 365 351 306 k 215 0 027 1
0000040 0 0 6 4 4 m e n u . t x t \0 J
0000060 022 325 j L ( Q 365 $ 336 270 025 367 004 337 273 311
0000100 F 301 255
If you want to read the contents of a tree, use -p.
Checking Whether an Object Exists in a Script
-e prints nothing. If the object exists, the command exits with status 0. You can use it in a shell script to check whether a commit or file exists.
$ git cat-file -e HEAD~1:menu.txt; echo "終了ステータス: $?"
終了ステータス: 0
$ git cat-file -e HEAD:summer.txt; echo "終了ステータス: $?"
fatal: path 'summer.txt' does not exist in 'HEAD'
終了ステータス: 128
$ git cat-file -e 0000000000000000000000000000000000000001; echo "終了ステータス: $?"
終了ステータス: 1
When I specified a path to a file that does not exist, an error message was printed and the command exited with 128. When I used a correctly formed hash for an object that does not exist, nothing was printed and the command exited with 1. Both are non-zero, so you can write if git cat-file -e …; then to continue only when the object exists.
If you need to check many objects, pass them to --batch-check on standard input, one per line, and it prints the type and size of each one together. For objects that do not exist, it printed missing.
$ printf "HEAD\nHEAD:menu.txt\nv1.0\nHEAD:summer.txt\n" | git cat-file --batch-check
eb8b624a7c18bdf7e6402388a4c353fcc89f289c commit 225
4a12d56a4c2851f524deb815f704dfbbc946c1ad blob 34
786e64494a09e3878357612254a6a00bcde799c0 tag 131
HEAD:summer.txt missing
With --batch, the contents are printed after the information on the first line.
$ printf "HEAD~1:menu.txt\n" | git cat-file --batch
8086847e639a5eb690e946ee413e5fd421d8e72e blob 34
コーヒー 400円
紅茶 380円
Looking Does Not Change the Repository
I opened many objects with git cat-file above. When I checked the state at the end, the working tree had no changes and there were still two commits. git cat-file only reads objects. It does not change files or branches.
$ git status
On branch main
nothing to commit, working tree clean
$ git log --oneline
eb8b624 コーヒーを値上げする
66cc3ae メニューを作る
If you only want to see a file from an earlier commit, git show <commit>:<path> also prints the same contents.
$ git show HEAD~1:menu.txt
コーヒー 400円
紅茶 380円
When you want to check parts that do not appear in the usual output of git log or git show, such as the hash of the tree a commit points to, or the modes and file names in a tree, open the object with git cat-file -p to see it in the form Git stores.





