Git has a well-known, or at least sort-of-well-known, empty tree whose SHA1 is:
4b825dc642cb6eb9a060e54bf8d69288fbee4904
(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:
6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321
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,
4b825dc642cb6eb9a060e54bf8d69288fbee4904
?(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.)
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 (Command Prompt):
git mktree <NUL
# Windows (PowerShell):
$null | git mktree
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 asoid_object_info()
was taught the empty tree in c4d9986f5f ("sha1_object_info
: examinecached_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 user.name username
git config user.email email@com
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
4b825dc642cb6eb9a060e54bf8d69288fbee4904
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
4b825dc642cb6eb9a060e54bf8d69288fbee4904
(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
.
#define EMPTY_TREE_SHA1_HEX \
"4b825dc642cb6eb9a060e54bf8d69288fbee4904"
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
andempty_blob_oid
to use thecurrent_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 valuesSigned-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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With