Skip to content

hash-object Git Command Guide

The git hash-object command computes object ID values for files and optionally creates the corresponding objects in the Git object database. It provides low-level interface for creating objects that works without repository checkout.

Terminal window
git hash-object [-t <type>] [-w] [--path=<file> | --no-filters]
[--stdin [--literally]] [--] <file>...
git hash-object [-t <type>] [-w] --stdin-paths [--no-filters]
OptionDescription
-t <type>Object type: blob (default), tree, commit, tag
-wWrite object to object database
--stdinRead content from standard input
--stdin-pathsRead file paths from stdin, one per line
--path=<file>Hash as if file were at this path (affects filters)
--no-filtersHash without applying Git filters
--literallyHash stdin as-is without Git filters
ParameterDescription
<file>...Files to compute object IDs for
<type>Object type (blob, tree, commit, tag)

Compute ID for existing file without storing

Section titled “Compute ID for existing file without storing”
Terminal window
git hash-object README.md

Outputs SHA-1 hash for README.md without creating object.

Terminal window
git hash-object -w README.md

Creates blob object in database, outputs its SHA-1.

Terminal window
echo "Hello World" | git hash-object --stdin

Computes hash for stdin content as blob.

Terminal window
echo "test content" | git hash-object --stdin -w

Creates blob object from stdin and outputs its ID.

Terminal window
git hash-object -t tag -w my-tag.txt

Create tag object from file content (file must contain valid tag format).

Terminal window
git hash-object --no-filters --stdin < binary-file

Hash file exactly as-is, without Git’s text conversion or clean filters.

Terminal window
git hash-object file1.txt file2.txt file3.txt

Outputs hashes for multiple files.

Terminal window
echo "README.md" | git hash-object --stdin-paths

Read file paths from stdin instead of command arguments.

Terminal window
git hash-object --path=src/main.c backup.c

Apply filters as if backup.c were at src/main.c path.

Terminal window
git update-index --add --cacheinfo 100644 $(git hash-object -w file.txt) file.txt

Manually add file to index without using git add.

Terminal window
BLOB_ID=$(git hash-object -w file.txt)
TREE_ID=$(git mktree < <(echo "100644 blob $BLOB_ID file.txt"))

Create blob and tree objects for manual repository construction.

Terminal window
git hash-object backup.tar.gz | tee backup.hash

Hash large files for integrity verification without Git object creation.

How does hash-object work without object creation?

Section titled “How does hash-object work without object creation?”

It computes SHA-1/SHA-256 hash of file content after applying Git’s filters (crlf, clean filters) but doesn’t store the result, useful for verification or comparison.

What’s the difference between hash-object and git add?

Section titled “What’s the difference between hash-object and git add?”

git add applies filters, creates object, and updates index. git hash-object just computes IDs without affecting repository state.

—path applies location-specific Git filters to content, ensuring hash matches what git add would produce for that file in that location.

For exact binary content hashing without any Git processing - useful when you want to hash content exactly as stored on disk.

Can I use hash-object to verify object integrity?

Section titled “Can I use hash-object to verify object integrity?”

Yes, combine with git cat-file to verify: git hash-object --stdin < <(git cat-file blob <id>) should match .

Batch processing multiple files where file paths are generated dynamically: find . -name "*.txt" | git hash-object --stdin-paths.

Git applies clean filters, text/crlf conversion, and encoding filters. —no-filters hashes raw content, —literally skips all filter processing.

Yes with -t commit, but input must be valid commit format. Usually used for debugging or manual repository construction.

What’s the relationship to git ls-files?

Section titled “What’s the relationship to git ls-files?”

git ls-files shows files in index with their current blob IDs. hash-object computes what the ID would be after potential filter application.

Empty tree has fixed hash: printf "" | git hash-object --stdin -t tree or use git mktree < /dev/null.

Can hash-object work in bare repositories?

Section titled “Can hash-object work in bare repositories?”

Yes, it operates directly on object database and doesn’t require working directory.

What’s the performance impact on large files?

Section titled “What’s the performance impact on large files?”

It processes entire file content to compute hash - large files take proportionally longer.

By default, applies Git’s encoding filters. Use —literally to bypass encoding processing.

Can I get SHA-256 hashes instead of SHA-1?

Section titled “Can I get SHA-256 hashes instead of SHA-1?”

hash-object uses repository’s hash algorithm - if repo uses SHA-256, it produces SHA-256 hashes automatically.

Applications of the git hash-object command

Section titled “Applications of the git hash-object command”
  1. Object Database Debugging: Manually create objects for testing repository operations
  2. Backup Integrity Verification: Hash files for comparison with Git object store
  3. Content Addressability: Compute unique IDs for files outside Git repository context
  4. Custom Import Scripts: Create objects manually during repository migration
  5. Educational Experiments: Understand Git’s object storage by creating objects directly
  6. Content Analysis: Calculate hashes for duplicate detection and content comparison