git hash-object: Usage, Options & Common CI Errors
By Daniel Zoghalchali·Latchkey
git hash-object returns the SHA Git would assign to a piece of content.
This plumbing command is how you compute a blob id without committing, write loose objects directly, or verify that a file matches an expected object id in a pipeline.
What it does
git hash-object reads content from a file or standard input, computes the object id Git would give it (a blob by default), and optionally writes the object into the object database with -w.
Line-ending and filter settings can change the computed hash: a file run through autocrlf or a clean filter hashes differently than its on-disk bytes. Use --no-filters for byte-exact hashing, and remember -w needs to run inside a repository so .git/objects exists.
Using this in CI
CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.
.github/workflows/ci.yml
- uses:actions/checkout@v4with:fetch-depth:0 # history, tags, and git describe all need this- run:|git rev-parse --is-shallow-repository # expect falsegit rev-parse --abbrev-ref HEAD # prints HEAD when detached
Frequently asked questions
git hash-object: Usage, Options & Common CI Errors?
This plumbing command is how you compute a blob id without committing, write loose objects directly, or verify that a file matches an expected object id in a pipeline.
What it does?
git hash-object reads content from a file or standard input, computes the object id Git would give it (a blob by default), and optionally writes the object into the object database with -w.
Common errors in CI?
Line-ending and filter settings can change the computed hash: a file run through autocrlf or a clean filter hashes differently than its on-disk bytes. Use --no-filters for byte-exact hashing, and remember -w needs to run inside a repository so .git/objects exists.