This post is for people who are interested in how commits and files are stored in git, and how you can look at the internal files yourself.
A small note up-front: Check the date of this post when trying to replicate the commands I am going to list.
On my machine, I am using git version 2.56.1.
In my ongoing adventure to understand the tools I use, my latest experiments were about Version Control Systems (VCS), specifically git.
It's one of these where:
I am mostly interested in the fundamentals of a git repository, specifically commits and file storage. Stuff like Large File Storage (LFS), remotes and branches are out of scope for this ramble.
For an overview of the various files, you can take a look at the official Git Directory Layout. Some of the files in there are quite easy to understand:
config contains some settings for this specific repo, likegit pull and git push do by defaultdescription contains an optional repository descriptionhooks can be used to automatically execute shell scripts on certain actions
(see my Cheat Sheet for an example)HEAD contains a reference to the currently active branchFETCH_HEAD, which is documented separately hereCOMMIT_EDITMSG contains the message of a commit in progress (see here)index already in a previous blog post. In short, it's a local cache of the state of files in this repository, which is used to accelerate the detection of changes (e.g. for when using git status)
I want to focus on the objects/ directory, as that directory contains the
internal git objects.
Listing out the files seems very cryptic at first:
> find .git/objects/ -type f
.git/objects/cd/31cfef495505a09e1e17ad645f070942b61ba1
.git/objects/cd/4c9a48a88a1f89d89a0b946944d9d8815e6b51
.git/objects/cd/08511ff1e38f5e92ea824c84dd6537c4cadac7
.git/objects/0f/57cec2d20dc8b29c75bfc0d2a63feac4397ff5
.git/objects/14/60b1dfd8619f2ef050f88ca6f8b9cf996fdd17
.git/objects/53/53ab2f5c78789eb46162e2b243762251bb48f8
.git/objects/53/38c152817d14ddfb527e2d3ffc4074c2140630
.git/objects/36/9000b82a6b0c5e31013adb8228ebc93a7de630
.git/objects/de/409ecfb9ae59fe36da7515dc3f0c797a635b2a
.git/objects/de/fdd320e289891a0ff3eb7e6e9f5628669acbf1
.git/objects/a3/71acbb72dba39dc961aeacbd02c920465d193d
[ ... ]
Looking at the file contents direcly will result in unreadable characters. The trick is to get a very basic understanding of the internal git objects. Basically, there are four simple types of objects:
Each file in the terminal output above is going to be one of these four objects, which are identified via an SHA-1 hash. To prevent producing directories with too many elements, one byte (two characters in hexadecimal) of the hash is used as a directory identifier. So you can already read the hash just from the directory listing above:
.git/objects/cd/31cfef495505a09e1e17ad645f070942b61ba1
^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
=> SHA-1 Hash: cd31cfef495505a09e1e17ad645f070942b61ba1
Git even provides an option to examine these objects more closely just by using the hash
(because of course git has an option like that), git cat-file [Hash]:
# Get the type of the object
> git cat-file -t cd31cfef495505a09e1e17ad645f070942b61ba1
blob
# "Pretty-print" the object
> git cat-file -p cd31cfef495505a09e1e17ad645f070942b61ba1
<!DOCTYPE html><html>
<head><title>Procrastinating by Creating This Website</title>
<link rel="stylesheet" type="text/css" href="/style.css">
<link rel="icon" type="image/png" href="/Icon16.png" sizes="16x16">
<link rel="icon" type="image/png" href="/Icon32.png" sizes="32x32">
[...]
In this instance, this blob is one of the HTML files I checked into the repository of the website. "Pretty-printing" this blob just outputs the file content.
Now, I will sidestep using the git cat-file command, because I want to show that there is really not a lot of magic going on.
These object files are simply zlib-compressed, you can look at the contents yourself by deflating the contents:
> git cat-file -t .git/objects/84/840baa180fc0b4000d0d1ccbce756a4e7fb64c6a
blob
> zlib-flate -uncompress < .git/objects/84/0baa180fc0b4000d0d1ccbce756a4e7fb64c6a
blob 18706#define _GNU_SOURCE
#include "base/include.h"
#include "base/include.c"
enum token_type : u8
{
TokenType_Nil, // @TODO?
TokenType_EndOfFile,
TokenType_Identifier,
TokenType_Literal, // (typed-)number, string, char, etc
[...]
This blob is just one of my code files, with a little blob 18706 prepended in-front,
followed by a null-byte which I can't really display well here.
blob identifies the object type, and 18706 is the size of bytes of the blob content.
We can verify this easily:
> zlib-flate -uncompress < .git/objects/84/0baa180fc0b4000d0d1ccbce756a4e7fb64c6a | wc -c
18717
> echo -ne "blob 18706\0" | wc -c
11
# 18706 + 11 = 18717
# ^ ^ ^_ Total uncompressed blob size
# | |_ Size of bytes before blob content
# |_ Size of blob content
Let's look at commits next. You can either try to find a commit object yourself, or you can look through
the git log to retrieve the hash, after which we can lookup the object ourselves:
> git log
commit 71c15d3cb1ff3c62657d1b31215e2dc7e9f33ec1 (HEAD -> main, origin/main, origin/HEAD)
Author: Felix <git@p4m.dev>
Date: Tue Nov 24 22:15:09 2026 +0100
Comments for what to work on next
commit 78de5ea98875a91089671cca22fac09ffd867452
Author: Felix <git@p4m.dev>
Date: Tue Nov 24 21:52:39 2026 +0100
'x' edit functionality
[ ... ]
> git cat-file -t 71c15d3cb1ff3c62657d1b31215e2dc7e9f33ec1
commit
> zlib-flate -uncompress < .git/objects/71/c15d3cb1ff3c62657d1b31215e2dc7e9f33ec1
commit 526tree 705e1dbb6f7d77c384ac474d1d169e4e9bb418cf
parent 78de5ea98875a91089671cca22fac09ffd867452
author Felix <git@p4m.dev> 1795554909 +0100
committer Felix <git@p4m.dev> 1795554909 +0100
gpgsig -----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgOmqUgUiiAgeX1DvkGO/9FYDnqp
g+iaB5cV3kXhx7EzEAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
AAAAQASui0SW42acERmiWhqg9zAVQ4l2NW4icTV9roxXuHW4azfyvBzmnXKI0pahbgSJ4c
Ym5mHcDCR5VELsY3LS3AA=
-----END SSH SIGNATURE-----
Comments for what to work on next
> git cat-file -p 71c15d3cb1ff3c62657d1b31215e2dc7e9f33ec1
tree 705e1dbb6f7d77c384ac474d1d169e4e9bb418cf
parent 78de5ea98875a91089671cca22fac09ffd867452
author Felix <git@p4m.dev> 1795554909 +0100
committer Felix <git@p4m.dev> 1795554909 +0100
gpgsig -----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgOmqUgUiiAgeX1DvkGO/9FYDnqp
g+iaB5cV3kXhx7EzEAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
AAAAQASui0SW42acERmiWhqg9zAVQ4l2NW4icTV9roxXuHW4azfyvBzmnXKI0pahbgSJ4c
Ym5mHcDCR5VELsY3LS3AA=
-----END SSH SIGNATURE-----
Comments for what to work on next
Again: The commit file consists of a small header, this time commit 526, and then you get the content,
which is mostly a bunch of readable strings:
tree: Hash of a tree-reference (effectively the list and state of files for this commit)parent: Hash of the previous commit (see the git log output)author and committer is me :)1795554909 +0100 is a unix timestamp and timezone offset of that commit, matches the git log outputgpgsig also shows that the commit of mine is signed (by me)Pretty simple, isn't it? A commit object is basically just a compressed and formatted string with a little header in front. To walk the git commit history, you can:
HEAD hash, probably via .git/HEAD / .git/refs/heads/main
As stated on the git-commit-tree documentation, merge commits aren't that special either:
Let's confirm this by looking at a minimal git repo:
# Setup Main
> git init
> touch test
> git add test
> git commit -m "initial commit"
###########################
# Looking at initial commit
> git log
commit 98f189154f4ebec3c87d01cae1e538c2b88a1fd7 (HEAD -> main)
Author: Felix <git@p4m.dev>
Date: Sat Dec 5 19:43:47 2026 +0100
initial commit
# No Parent in initial commit:
> git cat-file -p 98f189154f4ebec3c87d01cae1e538c2b88a1fd7
tree f05af273ba36fe5176e5eaab349661a56b3d27a0
author Felix <git@p4m.dev> 1796496227 +0100
committer Felix <git@p4m.dev> 1796496232 +0100
gpgsig -----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgOmqUgUiiAgeX1DvkGO/9FYDnqp
g+iaB5cV3kXhx7EzEAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
AAAAQIj+RUJsjqN/py/l2g+5RnQa7JdtMImPzJ9Ag42pmMsBTsQ1TIzxciMG2g8qhUR16K
SNDdkW7860XwiXh0H9lgw=
-----END SSH SIGNATURE-----
initial commit
###########################
# Merge
# Create branch to merge
> git checkout -b test_branch
> touch b.txt
> git add b.txt
> git commit -m "commit on branch"
> git checkout main
###########################
# Merge
> git merge test_branch
Updating 98f1891..169d508
Fast-forward
b.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
# Whoops, this is a fast-forward branch, meaning
# we take over the commits without having multiple parents:
> git cat-file -p 169d5089fd93cfd3e078384da750d643d5bcdcdb
tree a8b2f4119cc5eec06bfa225237291a8437d0f17a
parent 98f189154f4ebec3c87d01cae1e538c2b88a1fd7
author Felix <git@p4m.dev> 1796496253 +0100
committer Felix <git@p4m.dev> 1796496253 +0100
gpgsig -----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgOmqUgUiiAgeX1DvkGO/9FYDnqp
g+iaB5cV3kXhx7EzEAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
AAAAQIwk1OXWGJjq6XzfDh66niIjE5MK8Mf16jtoy9rRMW4cJAZl0MqS+jGyguIi3CYSbD
hPjg2hPumR7SIf0j9yTgE=
-----END SSH SIGNATURE-----
commit on branch
###########################
# Undo and redo merge,
# this time without fast-forward,
# resulting in multiple parents:
> git reset --hard 98f189154f4ebec3c87d01cae1e538c2b88a1fd7
> git merge --no-ff test_branch
Merge made by the 'ort' strategy.
b.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
> git log
commit 90e3bfd088c3484eab4b159122239f8572ffb28c (HEAD -> main)
Merge: 98f1891 169d508
Author: Felix <git@p4m.dev>
Date: Sat Dec 5 19:52:48 2026 +0100
Merge branch 'test_branch'
commit 169d5089fd93cfd3e078384da750d643d5bcdcdb (test_branch)
Author: Felix <git@p4m.dev>
Date: Sat Dec 5 19:44:13 2026 +0100
commit on branch
commit 98f189154f4ebec3c87d01cae1e538c2b88a1fd7
Author: Felix <git@p4m.dev>
Date: Sat Dec 5 19:43:47 2026 +0100
initial commit
> git cat-file -p 90e3bfd088c3484eab4b159122239f8572ffb28c
tree a8b2f4119cc5eec06bfa225237291a8437d0f17a
parent 98f189154f4ebec3c87d01cae1e538c2b88a1fd7
parent 169d5089fd93cfd3e078384da750d643d5bcdcdb
author Felix <git@p4m.dev> 1796496768 +0100
committer Felix <git@p4m.dev> 1796496768 +0100
gpgsig -----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgOmqUgUiiAgeX1DvkGO/9FYDnqp
g+iaB5cV3kXhx7EzEAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
AAAAQEwcEiihiZMHPN9pvEYCJw3etSVSyOz4N85ZPx7POb9O8XIPvcHvIslkvAje+HLNyT
ykcHo/97KtLdq+vDEntw0=
-----END SSH SIGNATURE-----
Merge branch 'test_branch'
It seems to to be that the first parent is the previous commit on this branch, while the second one is the "merge source" (or whatever you want to call this), although I'm not sure if this is specified anywhere, or if this is just a convention.
Apparently one can even merge more than one commit/branch at a time, as linked in this StackExchange post, describing a commit with 100'000 parents. But honestly I don't think I even ever did multiple merges at once.
So now for the last important type, the tree. I will just use the tree hash in the (non-test) repository investigated above:
> git cat-file -t 705e1dbb6f7d77c384ac474d1d169e4e9bb418cf
tree
> git cat-file -p 705e1dbb6f7d77c384ac474d1d169e4e9bb418cf
100644 blob 1e2b761b4f65810a95a066c67ef57a779db9559c .gitignore
100644 blob bd43f4d9b2f2feeb49520f720867224b23cf0192 .nvimrc
100644 blob e35c1e3b8ce896f16d3e7a1543f627c2b25eedec TODO
100755 blob 8daecf643b187572478b1846c262e07dbc63e58f build_emscripten.sh
100755 blob b3ca7fcd7cff5c1f9bbd6790fe88f62ce0c8b47b build_everything_desktop.sh
100755 blob 990da746325c642f19b481845d9204a08ac21010 build_linux.sh
100755 blob 8bde6c4b44cba36b5a71b25d089b15c8b18ca42e build_openbsd.sh
100644 blob 2836a888c6d3c1fd94cc6819706eefbeb79c6add chatClient03
100644 blob 2b45ea8ee509c8c70e2624e150429447ba760ece chat_client_notes.txt
100644 blob 02c7f03ca7428a75524ac7b11a4f5a463c2d50d5 client_private.pem
040000 tree 44541a2c7bb902e4e38da54cc596c3e73eabf479 code
040000 tree 7cf64f8ec12ecd0da78f29a1f768753a52372d35 data
100755 blob cd1af1173a753f61e218b62d8f19c0f804865856 fuse_nuke.sh
100755 blob cc510a5471faff48b3cb6ea2a79baf91064f3021 fuse_run.sh
100755 blob 41ed63173494f15bda74d805b6af1cd6e978a4f2 install.sh
100644 blob 18c0069dc2cc7e4a17c511218251a21afe5359f0 no-checkin.sh
100644 blob 61a4f0f81e210ff73a1e2d6b6c85ce2344ae14af server_cert.pem
100644 blob 63e371e87ba941dcd8dc0028ae08c0ec2068302b server_key.pem
100644 blob 96601626f1a5efc6dc7cd7875d98e75795d5a53d test.py
040000 tree aad78f40c43ccd0f60f852b6d19a8a405b2afa78 toolchain
# NOTE: Relax, the keys & certificates are for development only :)
> zlib-flate -uncompress < .git/objects/70/5e1dbb6f7d77c384ac474d1d169e4e9bb418cf
tree 804100644 .gitignore+ve
�f�~�zw�U100644 .nvimrc�C�ٲ���IRg"K#�100644 TODO�\;��m>zC�'²^��100755 build_emscripten.sh��d;urGF�b�}�c�100755 build_everything_desktop.sh���|�\�g��,�ȴ{100755 �F2\d/�]��100755 build_openbsd.sh�lKDˣkZq�ȱ�.100644 chatClient03(6������hpnᄋj�100644 chat_client_notes.txt+E�� ��&$�PBG�v�100644 client_private.pem��<�BuRJDZOZ�)��hu:R7-5100755 fuse_nuke.sh��:u?a��-��XV100755 fuse_run.sh�Q
Tq��H��n���O0!100755 install.shA�c4�[�t�����x��100644 no-checkin.sh���~J�!Q��SY�100644 server_cert.pema���!�:-kl�#D��100644 server_key.pemc�q�{�A���(�� h0+100644 test.py`&����|ׇ]�Wե=40000 toolchain�@�<�`�R�њ@[*�x⏎
The "pretty-print" is still very straight forward, but this time, the decompressed output is less readable. However, if we dump the decompressed output into a file and inspect it with a hex editor, you can see that the SHA-1 hashes are simply stored in binary form:
So, while less readable in console, the "pretty-print" doesn't do much magic here either:
tree 804 (with a trailing zero byte)100644) (with a trailing space), more on that in a moment
I found octal numbers confusing at first (100644 for blobs, and the 040000 for trees), but StackOverflow has an explanation for this.
These are just the type and permissions of each file/directory in octal form. The trees are especially misleading, as git does not store the permissions of a directory.
> stat -c '%f' .gitignore
81b4
> printf '%o\n' 0x81b4
100664
# Fits the output of the blob entry
> stat -c '%f' code
41fd
> printf '%o\n' 0x41fd
40775
# With Padding: 040775
# If we discard the permissions: 040000
At this point, I had a few questions about the hashing itself:
To answer the first bundle of questions, there is a wonderful collection of statements on StackOverflow. A good quote from Linus Torvalds himself (back in 2007):
So by my understanding, the core of git relies on the fact that the hash function is good enough such that, given reasonable input, no collisions occur in practice. A cryptographic hash function, even if considered insecure due to attacks, does generally generate uniformly distributed hashes. And as SHA-1 produces a 160 bit hash, it seems reasonable to expect that there won't be any hash collisions unless maliciously attacked (and the linked Wikipedia entry even states that the first hash collision was published in 2017). Regardless, there will be breaking changes for the upcoming git 3.0, which includes changing the default hash function for new repositories to SHA-256.
This leaves the question of how the hashes of git objects are computed. This is underwhelmingly simple - you hash the uncompressed contents with SHA-1:
> zlib-flate -uncompress < .git/objects/70/5e1dbb6f7d77c384ac474d1d169e4e9bb418cf | sha1sum
705e1dbb6f7d77c384ac474d1d169e4e9bb418cf -
I kind of expected this to be more complicated as I never worked with Merkle Trees before and know that this "tree of hashes"-datastructure is used in git as well (or rather Merkle-Directed-Acyclic-Graph, as mentioned in the Wikipedia link). But I suppose there is no reason this has to be complicated:
And you can also see how this helps with data integrity: If you have all of the git objects, you can recompute the hashes yourself and verify they match. As commits point to trees which point to blobs, any data errors will result in differing hashes, verifying the hash of the latest commit verifies the integrity of the entire previous git history and files. Neat.
This was a very limited view into git. I haven't covered / investigated stuff like:
.git/objects/pack/),
which (I think) also use delta encodingStill, I find it illuminating how (what I consider) the core operations of git are not that complicated. You can imagine building your own custom VCS using the building blocks we looked at. I mentioned briefly in a previous post that I have done that (or rather that I am still working on that). If I find the time, I will do a part 2 to this, which covers some thoughts on git's SSH file transfer, or what kind of algorithms you can use for large file storage (if you don't want to have a copy of every single variation of a file).
This post is already long enough, so just two last short things:
If you work collaboratively with git, I can recommend looking at git town. For me, I treat this as a set of scripts which helps me automate some boilerplate actions I have to do often:
And here are some git memes which didn't quite fit anywhere in this post: