0
0
Gitdevops~5 mins

git cat-file to inspect objects - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to look inside the hidden parts of your Git project to see exactly what is stored. The git cat-file command lets you peek inside Git objects like commits, trees, and blobs to understand what data they hold.
When you want to see the content of a specific commit or file version stored in Git.
When you need to check the exact data stored in a Git object by its hash.
When debugging Git issues by inspecting low-level objects.
When learning how Git stores data internally by viewing raw object contents.
When verifying that a commit or blob contains the expected information.
Commands
Create a new Git repository named example-repo to start tracking files.
Terminal
git init example-repo
Expected OutputExpected
Initialized empty Git repository in /current/path/example-repo/.git/
Change directory into the new repository to work inside it.
Terminal
cd example-repo
Expected OutputExpected
No output (command runs silently)
Create a file named file.txt with the text 'Hello Git' inside.
Terminal
echo "Hello Git" > file.txt
Expected OutputExpected
No output (command runs silently)
Tell Git to track the new file.txt by adding it to the staging area.
Terminal
git add file.txt
Expected OutputExpected
No output (command runs silently)
Save the staged file.txt to the repository with a message describing the change.
Terminal
git commit -m "Add file.txt with greeting"
Expected OutputExpected
[master (root-commit) abcdef1] Add file.txt with greeting 1 file changed, 1 insertion(+) create mode 100644 file.txt
-m - Provide a commit message inline
Get the hash ID of the latest commit to use it for inspection.
Terminal
git rev-parse HEAD
Expected OutputExpected
abcdef1234567890abcdef1234567890abcdef12
Show the full content of the commit object identified by the hash. This reveals commit details like tree, parent, author, and message.
Terminal
git cat-file -p abcdef1234567890abcdef1234567890abcdef12
Expected OutputExpected
tree 1234567890abcdef1234567890abcdef12345678 author Your Name <you@example.com> 1686000000 +0000 committer Your Name <you@example.com> 1686000000 +0000 Add file.txt with greeting
-p - Pretty-print the object content in a human-readable form
Show the type of the Git object (commit, tree, blob, tag) for the given hash.
Terminal
git cat-file -t abcdef1234567890abcdef1234567890abcdef12
Expected OutputExpected
commit
-t - Show the object type
Key Concept

If you remember nothing else from this pattern, remember: git cat-file lets you look inside Git objects by their hash to see their type and content.

Common Mistakes
Using git cat-file without a valid object hash
Git cannot find the object and returns an error because the hash is missing or incorrect.
Always use a full or abbreviated valid object hash from git rev-parse or git log.
Not using the -p flag when wanting readable content
Git outputs raw binary or unreadable data which is hard to understand.
Use git cat-file -p to pretty-print the object content in a readable format.
Summary
Initialize a Git repository and commit a file to create objects.
Use git rev-parse HEAD to get the latest commit hash.
Use git cat-file -p <hash> to view the content of a Git object.
Use git cat-file -t <hash> to see the type of the object.