Git has a well-known, or at least sort-of-well-known, empty tree whose SHA1 is:


(you can see this in any repo, even a newly created one, with git cat-file -t and git cat-file -p). [Edit made in 2020: the SHA-256 empty tree hash ID is:


as VonC mentions in his answer. My question was apparently about 8 years early. 😀]

If you work hard and are very careful you can sort of use this empty tree to store a directory that has no files (see answer to How do I add an empty directory to a git repository), although it's not really a great idea.

It's more useful as one argument to git diff-tree, which one of the sample hooks does.

What I'm wondering is,

  1. how reliable is this—i.e., will some future version of git not have a git object numbered 4b825dc642cb6eb9a060e54bf8d69288fbee4904?
  2. Why is there no symbolic name for the empty tree (or is there one?).

(A quick and dirty way to create a symbolic name is to put the SHA1 in, e.g., .git/Nulltree. Unfortunately you have to do this for every repo. Seems better to just put the magic number in scripts, etc. I just have a general aversion to magic numbers.)

Solution 1

This thread mentions:

If you don't remember the empty tree sha1, you can always derive it with:

git hash-object -t tree /dev/null

Or, as Ciro Santilli proposes in the comments:

printf '' | git hash-object --stdin -t tree

Or, as seen here, from Colin Schimmelfing:

git hash-object -t tree --stdin < /dev/null

So I guess it is safer to define a variable with the result of that command as your empty sha1 tree (instead of relying of a "well known value").

Note: Git 2.25.1 (Feb. 2020) proposes in commit 9c8a294:

empty_tree=$(git mktree </dev/null)
# Windows:
git mktree <NUL

And adds:

As a historical note, the function now known as repo_read_object_file() was taught the empty tree in 346245a1bb ("hard-code the empty tree object", 2008-02-13, Git v1.5.5-rc0 -- merge), and the function now known as oid_object_info() was taught the empty tree in c4d9986f5f ("sha1_object_info: examine cached_object store too", 2011-02-07, Git v1.7.4.1).

Note, you will see that SHA1 pop up on some GitHub repo when the author wants its first commit to be empty (see blog post "How I initialize my Git repositories"):

$ GIT_AUTHOR_DATE="Thu, 01 Jan 1970 00:00:00 +0000" GIT_COMMITTER_DATE="Thu, 01 Jan 1970 00:00:00 +0000" git commit --allow-empty -m 'Initial commit'

Will give you:

(See the tree SHA1?)

You can even rebase your existing history on top of that empty commit (see "git: how to insert a commit as the first, shifting all the others?")

In both cases, you don't rely on the exact SHA1 value of that empty tree.
You simply follow a best practice, initializing your repo with a first empty commit.

To do that:

git init my_new_repo
cd my_new_repo
git config username
git config [email protected]

git commit --allow-empty -m "initial empty commit"

That will generate a commit with a SHA1 specific to your repo, username, email, date of creation (meaning the SHA1 of the commit itself will be different every time).
But the tree referenced by that commit will be 4b825dc642cb6eb9a060e54bf8d69288fbee4904, the empty tree SHA1.

git log --pretty=raw

commit 9ed4ff9ac204f20f826ddacc3f85ef7186d6cc14
tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904      <====
author VonC <[email protected]> 1381232247 +0200
committer VonC <[email protected]> 1381232247 +0200

    initial empty commit

To show just the tree of a commit (display the commit tree SHA1):

git show --pretty=format:%T 9ed4ff9ac204f20f826ddacc3f85ef7186d6cc14

If that commit, referencing an empty tree, is indeed your first commit, you can show that empty tree SHA1 with:

git log --pretty=format:%h --reverse | head -1 | xargs git show --pretty=format:%T

(and that even works on Windows, with Gnu On Windows commands)

As commented below, using git diff <commit> HEAD, this will show all your file in the current branch HEAD:

git diff --name-only 4b825dc642cb6eb9a060e54bf8d69288fbee4904 HEAD

Note: that empty tree value is formally defined in cache.h.


Since Git 2.16 (Q1 2018), it is used in a structure which is no longer tied to (only) SHA1, as seen in commit eb0ccfd:

Switch empty tree and blob lookups to use hash abstraction

Switch the uses of empty_tree_oid and empty_blob_oid to use the current_hash abstraction that represents the current hash algorithm in use.

See more at "Why doesn't Git use more modern SHA?": it is SHA-2, since Git 2.19 (Q3 2018)

With Git 2.25 (Q1 2020), tests are preparing for a SHA-2 transition, and is involving the empty tree.

See commit fa26d5e, commit cf02be8, commit 38ee26b, commit 37ab8eb, commit 0370b35, commit 0253e12, commit 45e2ef2, commit 79b0edc, commit 840624f, commit 32a6707, commit 440bf91, commit 0b408ca, commit 2eabd38 (28 Oct 2019), and commit 1bcef51, commit ecde49b (05 Oct 2019) by brian m. carlson (bk2204).
(Merged by Junio C Hamano -- gitster -- in commit 28014c1, 10 Nov 2019)

t/oid-info: add empty tree and empty blob values

Signed-off-by: brian m. carlson

The testsuite will eventually learn how to run using an algorithm other than SHA-1. In preparation for this, teach the test_oid family of functions how to look up the empty blob and empty tree values so they can be used.

So t/oid-info/hash-info now includes:

rawsz sha1:20
rawsz sha256:32

hexsz sha1:40
hexsz sha256:64

zero sha1:0000000000000000000000000000000000000000
zero sha256:0000000000000000000000000000000000000000000000000000000000000000

algo sha1:sha1
algo sha256:sha256

empty_blob sha1:e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
empty_blob sha256:473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813

empty_tree sha1:4b825dc642cb6eb9a060e54bf8d69288fbee4904
empty_tree sha256:6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321

The SHA2 "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321" is the new SHA1 "4b825dc642cb6eb9a060e54bf8d69288fbee4904" empty tree.

Solution 2

Here is the answer on how to create empty tree commit even in the case when the repository is not already empty.

But I prefer "empty" to be tag, but not a branch. Simple way is:

git tag empty $(git hash-object -t tree /dev/null)

Because tag can point to tree-ish directly, without commit. Now to get all the files in the working tree:

git diff --name-only empty

Or the same with stat:

git diff --stat empty

All files as diff:

git diff empty

Check whitespaces in all files:

git diff --check empty

Solution 3

I wrote up a blog post with two different ways of finding the hash:

If it were to ever change for some reason, you could use the two ways below to find it. However, I would feel pretty confident using the hash in .bashrc aliases, etc., and I don't think it will change anytime soon. At the very least it would probably be a major release of git.

The two ways are:

  1. The answer above: git hash-object -t tree --stdin < /dev/null
  2. Simply initing an empty repo and then running git write-tree in that new repo - the hash will be output by git write-tree.