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.
git hash-object Syntax:
Section titled “git hash-object Syntax:”git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin [--literally]] [--] <file>...git hash-object [-t <type>] [-w] --stdin-paths [--no-filters]Options:
Section titled “Options:”| Option | Description |
|---|---|
-t <type> | Object type: blob (default), tree, commit, tag |
-w | Write object to object database |
--stdin | Read content from standard input |
--stdin-paths | Read file paths from stdin, one per line |
--path=<file> | Hash as if file were at this path (affects filters) |
--no-filters | Hash without applying Git filters |
--literally | Hash stdin as-is without Git filters |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
<file>... | Files to compute object IDs for |
<type> | Object type (blob, tree, commit, tag) |
git hash-object Command Samples:
Section titled “git hash-object Command Samples:”Compute ID for existing file without storing
Section titled “Compute ID for existing file without storing”git hash-object README.mdOutputs SHA-1 hash for README.md without creating object.
Create blob object from file
Section titled “Create blob object from file”git hash-object -w README.mdCreates blob object in database, outputs its SHA-1.
Hash content from stdin
Section titled “Hash content from stdin”echo "Hello World" | git hash-object --stdinComputes hash for stdin content as blob.
Create blob and get ID in one command
Section titled “Create blob and get ID in one command”echo "test content" | git hash-object --stdin -wCreates blob object from stdin and outputs its ID.
Hash file with specific object type
Section titled “Hash file with specific object type”git hash-object -t tag -w my-tag.txtCreate tag object from file content (file must contain valid tag format).
Bypass filters for exact hashing
Section titled “Bypass filters for exact hashing”git hash-object --no-filters --stdin < binary-fileHash file exactly as-is, without Git’s text conversion or clean filters.
Process multiple files from command line
Section titled “Process multiple files from command line”git hash-object file1.txt file2.txt file3.txtOutputs hashes for multiple files.
Read file paths from stdin
Section titled “Read file paths from stdin”echo "README.md" | git hash-object --stdin-pathsRead file paths from stdin instead of command arguments.
Hash file as if in different location
Section titled “Hash file as if in different location”git hash-object --path=src/main.c backup.cApply filters as if backup.c were at src/main.c path.
Create tree object from index entry
Section titled “Create tree object from index entry”git update-index --add --cacheinfo 100644 $(git hash-object -w file.txt) file.txtManually add file to index without using git add.
Direct object database manipulation
Section titled “Direct object database manipulation”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.
Hash validation for backup integrity
Section titled “Hash validation for backup integrity”git hash-object backup.tar.gz | tee backup.hashHash 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.
Why use —path option?
Section titled “Why use —path option?”—path applies location-specific Git filters to content, ensuring hash matches what git add would produce for that file in that location.
When should I use —literally?
Section titled “When should I use —literally?”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
What’s the use case for —stdin-paths?
Section titled “What’s the use case for —stdin-paths?”Batch processing multiple files where file paths are generated dynamically: find . -name "*.txt" | git hash-object --stdin-paths.
How do filters affect hashing?
Section titled “How do filters affect hashing?”Git applies clean filters, text/crlf conversion, and encoding filters. —no-filters hashes raw content, —literally skips all filter processing.
Can hash-object create commit objects?
Section titled “Can hash-object create commit objects?”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.
How do I create empty tree objects?
Section titled “How do I create empty tree objects?”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.
How does hash-object handle encoding?
Section titled “How does hash-object handle encoding?”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”- Object Database Debugging: Manually create objects for testing repository operations
- Backup Integrity Verification: Hash files for comparison with Git object store
- Content Addressability: Compute unique IDs for files outside Git repository context
- Custom Import Scripts: Create objects manually during repository migration
- Educational Experiments: Understand Git’s object storage by creating objects directly
- Content Analysis: Calculate hashes for duplicate detection and content comparison