Merkle Trees in Git

Learn how Git commits, trees, and blobs form a content-addressed object graph, and what hashes can and cannot prove about repository integrity.

published: reading time: 8 min read author: Geek Workbench updated: March 31, 2026
Quick Summary

Git stores commits, trees, and blobs as content-addressed objects. A commit references a root tree and parent commits, so hashes make changes detectable relative to a trusted object ID; they do not identify who created an object or establish trust on their own. This guide walks through those objects with `git cat-file` and explains the SHA-1 and SHA-256 repository formats.

Merkle Trees in Git

Git stores repository data as objects addressed by hashes. The objects form a content-addressed graph, not a simple binary Merkle tree. Commits reference a root tree and parent commits; tree objects reference blobs and other trees. This structure lets Git identify the exact object content and connect snapshots to history.

A hash is useful for detecting a mismatch when you already have a trusted object ID to compare against. The hash alone does not prove who created the object, whether a server is trustworthy, or whether the reference you received is authentic. For authorship, use a verified signature or another trusted channel for the commit or tag reference.

Git’s object graph

Git has four main object types:

  • Blob: file contents. A blob does not store its filename.
  • Tree: a directory snapshot. Each entry names a blob or subtree and includes a mode and object ID.
  • Commit: a snapshot reference. It names one root tree, zero or more parent commits, and metadata such as author, committer, and message.
  • Tag: a named reference that can point to another object, often a commit.

The graph is directed. Tree objects can share blob objects, and commits can share trees. Parent links create commit history with branches and merges. This is why calling Git a binary tree is misleading: the object types and reference patterns are not those of one uniform binary hash tree.

Branch and tag names are references stored outside the commit object. A branch normally points to a commit; moving the branch changes the reference, not the commit’s contents or object ID. A commit records its parent commit IDs, tree ID, and metadata, so the same snapshot can appear in commits with different parents or metadata and therefore have different commit IDs.

Changing a file changes the blob ID if the new object content differs. A tree that references that blob then has different content and an identifier; a commit that references the changed tree also changes. Later commits that name the changed commit as a parent would need new content to preserve that altered history. Unrelated objects do not change merely because one file changed.

How Git names objects

An object ID hashes the object’s type, byte length, a NUL separator, and its content. A blob with the text hello\n, for example, is not hashed as raw file bytes alone; the object header is part of the input. Tree and commit contents include references to other object IDs, which is how their identifiers depend on referenced objects.

Git repositories commonly use SHA-1 object IDs, while Git also defines a SHA-256 repository format. The format is selected for the repository; SHA-1 and SHA-256 IDs have different lengths and object references inside the formats differ. Git’s hash-function transition design describes repository-format extensions and compatibility mappings. Do not assume that changing an ID string converts or authenticates an object.

The repository hash format is not a per-object setting. A repository using SHA-1 does not silently mix in SHA-256 object IDs, and copying an ID string into another repository does not transform its object. Before choosing SHA-256 for a project, check the Git versions and hosting, backup, and tooling support in that environment; the transition document describes format and compatibility work rather than a universal migration command.

Inspect a commit with git cat-file

Run these commands inside a Git repository. They inspect one commit, its root tree, and a file blob without relying on a graphical client:

# Show the type of the current commit.
git cat-file -t HEAD

# Print the commit object: tree ID, parent IDs (if any), identity fields, and message.
git cat-file -p HEAD

# Resolve the root tree named by the commit, then inspect its entries.
git rev-parse 'HEAD^{tree}'
git cat-file -p 'HEAD^{tree}'

# Resolve and inspect the blob for a tracked file.
git rev-parse 'HEAD:README.md'
git cat-file -t 'HEAD:README.md'
git cat-file -p 'HEAD:README.md'

The tree output contains entries with a mode, object type, object ID, and path. A directory entry points to another tree; a regular file entry points to a blob. This is a practical way to follow the graph from commit to tree to file content.

To check repository object connectivity and object consistency, run:

git fsck --full

git fsck can report missing or corrupt objects and unreachable objects. A successful check does not tell you that a remote server, branch name, or commit author is trustworthy. Compare the object ID with a reference you trust, or verify a signed commit or tag where authorship matters.

Compare a working file with the committed blob

This small check separates a content mismatch from a question about trust. Run it from the repository root:

# The object ID recorded for the file in HEAD.
git rev-parse 'HEAD:README.md'

# The blob ID Git computes from the current working file.
git hash-object -- README.md

If the IDs differ, the working file’s bytes do not match the blob recorded in HEAD under the repository’s hash format. That may be an ordinary local edit. It is not, by itself, evidence of tampering. To establish which version is approved, compare against a trusted commit or verify a signed commit or tag under your project’s key policy.

What hashes help verify

Situation What to check What it establishes
A file differs from a checked-out commit Compare its blob ID with git rev-parse <commit>:<path> Whether the file bytes match that commit’s blob
An object store may be damaged Run git fsck --full Whether Git can read objects and follow expected object references
A commit’s author or approver matters Verify the signed commit or tag and key policy Whether a trusted key signed that object, according to local policy
A remote branch name may be untrusted Compare with an independently obtained commit ID Whether the fetched commit matches that reference

These checks answer different questions. git fsck checks repository structure and object integrity; it does not certify that every reachable commit is authorized or that the remote is trustworthy. A signature authenticates a signature key only after the verifier decides that key is trusted.

Trade-offs and common misconceptions

Content-addressed objects make identical object content reusable and let parent objects name the exact trees and commits they depend on. That provides stable references for snapshots and history. It also means an edit changes the affected blob and any tree or commit whose content refers to the changed ID. Unrelated objects can remain shared.

Misconception Correction
“The commit hash identifies the author.” Identity fields are data inside the commit; they are not proof that the named person created it. Verify a signature when authorship matters.
“A matching hash proves the server is honest.” It proves a match to the object ID being checked, subject to the hash function. The ID itself needs a trusted source.
“Git history is one binary Merkle tree.” Commits, trees, blobs, and tags are typed objects with different references; the structure is a graph.
“SHA-1 and SHA-256 object IDs are interchangeable.” They belong to different repository hash formats. Use the documented transition and confirm every tool supports the chosen format.
“git fsck tells me whether history is approved.” It checks object and connectivity consistency, not project authorization or the trustworthiness of a remote.

Integrity, identity, and trust

These are separate properties:

Question What a Git object hash can tell you What you still need
Did the object content change relative to this ID? A different content-addressed ID signals a different object, subject to the hash algorithm’s collision properties A trusted ID or reference to compare against
Who created or approved the commit? Nothing by itself A verified signature and a policy for trusted keys or identities
Is the remote server honest? Nothing by itself Independent references, signatures, or another verification process
Is the content confidential? Nothing; object contents are not encrypted by hashing Encryption and access controls

Git’s object IDs make accidental corruption easier to detect and make history references compact. They are one part of a verification process, not a substitute for identity or authorization.

For the generic binary-tree construction and proof assumptions, see Merkle Trees: Efficient Data Integrity in Distributed Systems.

Further Reading

Conclusion

Git’s objects form a content-addressed graph. A commit points to a tree and parent commits; trees point to blobs and subtrees. Hashes let Git name and compare those objects, while signatures and trusted references answer questions about identity and trust.

Category

Related Posts

Password Hashing and Message Integrity

Learn why passwords need slow, salted hashes, how SHA-256 differs from a keyed MAC, and how to verify credentials and data safely.

#cybersecurity #cryptography #password-security

Centralized vs Distributed VCS: Team Trade-offs

Compare centralized and distributed version control by offline work, repository size, access control, branching, review, and migration effort.

#git #version-control #svn

Automated Changelog Generation

Generate release notes from structured commits, choose a release policy, and add changelog generation to CI without treating every merge as a release.

#git #version-control #changelog