linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Tools/cat-file

kb/tools ── Tools ── intermediate

git cat-file

A plumbing command for reading objects in `.git/objects/` by their SHA. The main flags are: `-t` (type), `-s` (size), `-p` (content in human-readable form), `-e` (check whether the object exists).

view as markdownaka: git-cat-file

git cat-file is the primary tool for inspecting the contents of Git objects from the inside. Use it in scripts, during debugging, and when learning how Git stores data.

Basic flags

bash
git cat-file -t <sha>   # type: blob | tree | commit | tag
git cat-file -s <sha>   # content size in bytes
git cat-file -p <sha>   # content (pretty-print)
git cat-file -e <sha>   # existence check via exit code

Pretty-print adapts its formatting to the object type:

  • blob: raw bytes as-is.
  • tree: a table of "mode type sha name".
  • commit: text representation with tree, parent, author, committer, message.
  • tag: annotated tag with all fields.

Examples

What object is under this SHA?

bash
git cat-file -t 8d0e41
# blob

Show the content of the latest commit:

bash
git cat-file -p HEAD
# tree 7e3f9a2b1c4d5e6f...
# parent a1b2c3d4...
# author ...
# committer ...
#
# Commit message

Look inside the root tree of that commit:

bash
git cat-file -p HEAD^{tree}
# 100644 blob 5f7e9c12...    README.md
# 100644 blob 8a3f2e91...    index.html

Check whether an object exists without any output:

bash
if git cat-file -e <sha> 2>/dev/null; then
  echo "object is present"
fi

Batch mode

For scripts that read many objects at once:

bash
echo HEAD | git cat-file --batch
# <sha> commit 245
# tree ...
# parent ...
# ...

--batch reads SHAs from stdin one per line and outputs the metadata plus content for each. On large repositories this is tens of times faster than calling cat-file -p for each SHA separately.

Pitfalls

  • Pretty-print for a blob outputs raw bytes as-is. If the blob is binary (an image, an archive), this will corrupt your terminal. Check the type with -t first.
  • An object may be in a packfile, and cat-file will find it regardless. This is the plumbing level; packfiles are transparent.
  • -p HEAD shows the commit object, not the tree. To see the directory contents, use HEAD^{tree} or git ls-tree HEAD.

§ команды

bash
git cat-file -t <sha>

Get the type of an object

bash
git cat-file -p HEAD

Print a commit with its metadata

bash
git cat-file -p HEAD^{tree}

Print the root tree of the commit

bash
git cat-file -e <sha>

Check whether an object exists (exit code)

§ см. также

  • rev-parsegit rev-parseA plumbing command that translates human-readable ref names (HEAD, main~2, v1.0, :/typo) into full SHAs. Used by all porcelain commands under the hood, and convenient in scripts.
  • blobBlobA Git object that stores the content of a single file. Just bytes, no name, no permissions, no date. The filename lives in the `tree`, not in the blob.
  • treeTreeA Git object that holds the listing of one directory: entries of the form `(mode, type, SHA, name)`. It references other tree objects recursively for subdirectories.
  • commitCommitA Git object: a snapshot of the entire project (via a tree) plus metadata including author, committer, date, parents, and message. The SHA of a commit includes the parent's SHA, which makes history cryptographically linked.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies