By admin , 29 April 2026

Synopsis

git hash-object [-t <type>] [-w] [--stdin] [<file>...]

Description

The git hash-object command computes the SHA-1 (or SHA-256, in modern repos) that Git would assign to a file's contents. With -w, it also writes the object into the object database, returning the SHA. This is the lowest-level mechanism behind git add: that porcelain command essentially calls hash-object -w and then updates the index.

In day-to-day use, git hash-object integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git hash-object --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.

Understanding how git hash-object interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git hash-object's side effects helps avoid all three.

When to Use

You almost never need this directly. It is most useful for scripts that synthesize content into the object database (importers, migration tools), for debugging by checking what hash a piece of content would have, or for educational exploration of Git's content-addressable model.

Common Options

OptionDescription
-t <type>Object type (default blob).
-wWrite the object to the database.
--stdinRead content from standard input.
--stdin-pathsRead filenames from stdin.
--no-filtersSkip smudge/clean filters.
--literallyDon't validate content type.

Examples

git hash-object README.md
# Print the SHA Git would assign

echo "hello" | git hash-object --stdin
# Hash arbitrary content

git hash-object -w newfile.txt
# Add the file's content as a blob in the object DB

find . -name '*.png' | git hash-object --stdin-paths
# Batch-compute hashes for many files

Common Mistakes

Using -w creates a dangling blob if you don't reference it from a tree. git gc will eventually prune unreachable objects. Hashing through filters (CRLF normalization) yields different SHAs than raw bytes — pass --no-filters to compare with raw hashes from other tools.

Related Commands

git add, git cat-file, git update-index, git write-tree