Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Plumbing Commands

git hash-object command

Reference for git hash-object: compute the object ID of a file or text, store it with -w, choose the object type, and see how filters and --path affect the ID.

git hash-object takes content, computes the ID Git would give it, and prints that ID. With -w it also writes the object into the object database. It is the low-level equivalent of "add this file's content to Git" without touching the index: afterwards the blob exists, but nothing refers to it yet.

The ID depends on the type and the exact bytes: Git hashes <type> <size>\0<content>. So the same text always gets the same ID, in every repository on every machine, which is why hash-object is also used to check whether two files are identical without comparing them byte by byte.

Terminal
git hash-object file.txt
echo 'text' | git hash-object -w --stdin

Try it

Bash Hash, store and verify

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Filters change the ID, --path picks the rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin [--literally]] [--] <file>...
git hash-object [-t <type>] [-w] --stdin-paths [--no-filters]
Name Description
<file>... Files to hash. Each prints one ID.
-t <type> Object type: blob (default), tree, commit or tag. Content is checked for the type's format.
-w Write the object into the database.
--stdin Read the content from standard input instead of a file.
--stdin-paths Read file names from standard input, one per line.
--path=<file> Apply the filters and attributes of this path (line-ending conversion, ident, clean filters) to the content. Only valid with --stdin or a file outside the working tree.
--no-filters Hash the file as it is, skipping all conversions.
--literally Skip format checking, allowing any type name and malformed content. Meant for testing Git itself.

Returns: one object ID per input, 40 hexadecimal characters in a SHA-1 repository (64 in SHA-256).

Errors: error: ... : Is a directory; fatal: ... : cannot open; with a typed object, a format failure such as error: object fails fsck: badTree or fatal: corrupt commit if the content is not a valid tree or commit.

Usage notes

  • A file in the working tree is filtered by default. The result is the ID that git add would store: core.autocrlf, .gitattributes (text, eol, filter, ident) all apply. Content from --stdin has no name and no filters unless you pass --path.
  • Hashing is not storing. Without -w the object does not exist afterwards. Objects written with -w that nothing references are removed by git gc after the expiry time.
  • Outside a repository git hash-object still prints IDs, which makes it a handy "Git-compatible checksum" tool. It uses SHA-1 unless GIT_DEFAULT_HASH or the repository says otherwise.
  • Building history by hand. The usual chain is hash-object -w for a blob, git update-index --cacheinfo to put it in the index, git write-tree and git commit-tree.
  • Validation. -t commit and friends parse the data and refuse bad input; that makes it a safer way to create objects than --literally. Use git mktag for tags, which gives better messages.
  • Large files. Files bigger than core.bigFileThreshold (512 MiB by default) are streamed instead of loaded in memory. Hashing does not compress or delta anything; the packfile logic does that later.
  • Line endings caveat. If two machines report different IDs for the "same" file, compare with --no-filters to see whether conversions are the cause.