Skip to content
LatchkeyLatchkey home

git hash-object: Usage, Options & Common CI Errors

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.

Common usage

Terminal
git hash-object file.txt
git hash-object -w file.txt        # also write into .git/objects
echo "hello" | git hash-object --stdin
git hash-object -t blob --stdin < data.bin

Options

FlagWhat it does
-wWrite the object into the database
--stdinRead content from standard input
-t <type>Object type (blob, tree, commit, tag)
--path <file>Apply filters as if at this path
--no-filtersSkip clean/CRLF filters

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.

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@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git 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.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card