How Git Works Under the Hood

Matt George

Fractional CTO · Founder of Injuria.ai

Most people learn Git as a pile of commands to memorize. Add this, commit that, and when something goes wrong, paste a scary incantation from a forum and hope. It feels confusing because the commands are taught before the model, and the model is actually simple. Git is a small content-addressed database with a few object types and a couple of pointers on top.[7] Learn that, and rebase, reset, and cherry-pick stop being magic.

Everything is stored by the hash of its content

The core idea is content addressing. When Git stores something, it names that thing by a hash of its own bytes, so identical content always produces the same hash and the same file stored twice is kept once. You can see this directly.

echo "hello" | git hash-object --stdin
# ce013625030ba8dba906f756967f9e9ca394464a

That 40-character string is a SHA-1 hash, and it is the object's address inside the repository.[6] One subtlety matters if you ever try to reproduce it with a plain sha1sum: Git does not hash the raw file bytes alone. It first prepends a small header, the object type and its length followed by a null byte, like blob 6\0, and hashes that too. That is why Git's hash of "hello" does not match a bare SHA-1 of the same text, and knowing it is the difference between the hash feeling arbitrary and feeling inevitable. Git historically used SHA-1 and is moving toward SHA-256, but the principle is the same.

Four object types

Git has only four kinds of objects, and they combine to represent your whole history.[1] A blob holds the raw contents of a file, with no name and no path, just bytes and their hash. A tree represents a directory, listing names, file modes, and the hash of the blob or sub-tree each name points to.[8] The filenames live in the tree, not the blob, which is why two files with identical content share one blob. A commit is a snapshot: it points to one tree (the full project state at that moment), to its parent commit or commits, and records the author, committer, timestamp, and message. A tag object is an annotated, named pointer to another object, usually a commit, used for releases. (Git also has lightweight tags, which skip the tag object entirely and are just a file pointing straight at a commit.)

A commit is a snapshot, not a diff

This is the point that trips people up. A commit does not store "the lines you changed." It stores a pointer to a tree that describes the entire project as it looked then. Git shows you a diff by comparing two trees on the fly, but what it saves is a snapshot.

That sounds wasteful until you remember content addressing. If a file did not change between two commits, its blob hash is identical, so both trees point at the same existing blob, and unchanged files cost nothing to store again. For files that did change, Git does not keep full copies forever either: periodically it compresses history into packfiles, where similar objects are stored as deltas against one another.[2][8] So you get the simplicity of snapshots, the deduplication of content addressing, and the storage efficiency of delta compression, all at once.

History is a graph

Because each commit records its parent, commits form a chain, and because a merge commit has two parents, that chain is really a directed acyclic graph. Directed because parent links point backward in time, and acyclic because you can never be your own ancestor.

A ─── B ─── C ─── E   (main)
             \     /
              D ──      (feature, merged at E)

When you read your project's history, you are reading this graph, and git log --graph draws it. Operations that feel advanced are usually just walking or rewriting this graph.

Branches and HEAD are just pointers

Here is the part that makes branching feel cheap, because it is. A branch is not a copy of anything. It is a reference containing a single commit hash, stored either as a tiny loose file like .git/refs/heads/main or, once Git packs them for efficiency, as one line inside .git/packed-refs.[3] A loose ref file holds the 40 hex characters plus a trailing newline. HEAD is one more pointer, usually naming the current branch rather than a commit directly. When you commit, Git writes the new commit object and updates your branch pointer to it. When you create a branch, Git writes one tiny reference. When you switch, it moves HEAD and updates your working files. Nothing is duplicated, which is why creating and deleting branches is instant.

The staging area in the middle

Git has three places a file can live: your working directory, the repository, and a layer between them called the index, or staging area. git add writes the file's blob into the object store and records it in the index, which is a binary file at .git/index that caches each path along with its blob hash, mode, and timestamps.[5] git commit takes the current index, builds a tree from it, and wraps that tree in a commit object. This is why you stage before committing: the index lets you assemble exactly the snapshot you want, including only some of your edits, before you freeze it into history.

Why the model makes everything click

Once you hold these facts at the same time, snapshots addressed by content, history as a graph, branches as movable pointers, the confusing commands become obvious. A merge finds a common ancestor and combines two lines into a commit with two parents. A rebase replays your commits onto a different base, producing new commits with new hashes. A reset moves a branch pointer. A cherry-pick applies one commit's change as a new commit elsewhere. None are special cases, they are all moving pointers and creating objects in the same little database.

This also explains Git's safety net, which is the thing that turns "I destroyed my work" into a two-minute fix. Operations like rebase and reset do not delete the old commits, they just stop pointing at them, and those orphaned commits linger until a garbage collection much later. The reflog records every position your branches and HEAD have held, so a "lost" commit after a bad rebase is almost always still sitting in the object store, findable with git reflog and recoverable with a reset. There is a security property here too: because each commit's hash includes its tree and its parent's hash, and that parent includes its own parent, the whole history is a chain of hashes, and you cannot quietly alter an old commit without changing every hash after it.

Git is not complicated. It is a content-addressed object store with four object types and a few pointers, and almost everything you do is composing those pieces.[7] The commands make sense the moment you stop thinking in commands and start thinking in objects, and the single most freeing realization is that Git almost never throws anything away, so most mistakes are not losses, they are just pointers you can move back.

Sources (8)
  1. Pro Git: Git Internals, Git Objects
  2. Pro Git: Git Internals, Packfiles
  3. Pro Git: Git Internals, Git References
  4. Git documentation: gitrepository-layout
  5. Git documentation: index-format
  6. Git documentation: git-hash-object
  7. Aske Olsson, Rasmus Voss, Git Version Control Cookbook (Packt Publishing Ltd, 2014)
  8. Rheinwerk Publishing, Inc, Bernd Öggl, Michael Kofler, Git (Packt Publishing Ltd, 2025)

Recommended reading