How does Git work under the hood? Objects, hashes and branches explained
Most people learn Git as a list of commands. Understanding the data model underneath makes those commands much less mysterious.
Everything is an object
Git stores content in a simple database inside the .git folder. There are four main object types:
- Blob: the contents of a file. No filename, just the bytes.
- Tree: a directory listing that maps names to blobs and other trees.
- Commit: points to one tree (a snapshot of the project), plus parent commit(s), author, date and message.
- Tag: an annotated label pointing to another object.
Content addressing
Each object is identified by a hash of its contents. Historically Git uses SHA-1, with support for SHA-256 repositories added later. If two files have identical content, they are stored once. If anything changes, the hash changes. That also makes history tamper-evident: changing an old commit changes its hash and the hash of every commit after it.
Snapshots, not diffs
A commit records a full snapshot via its tree. Git saves space because unchanged files reuse existing blobs, and it compresses objects into packfiles. Diffs are computed when you ask for them.
Branches are just pointers
A branch is a tiny file containing a commit hash. Making a commit moves the branch pointer forward. HEAD tells Git which branch you are on. This is why creating branches is nearly instant.
Why this helps you
- Rebasing creates new commits with new hashes, which is why rewriting shared history causes trouble for collaborators.
- Lost work is often recoverable, since commits stay in the object database for a while and the reflog records where pointers used to be.
Questions:
- Which Git concept finally clicked once you understood the internals?
- Have you ever rescued "lost" commits with the reflog?
Comments · 2
Understanding that a branch is just a pointer to a commit was the moment Git clicked for me. Suddenly rebasing and resetting were things you could reason about.
Great explainer. I knew commits were snapshots, but the hashing part explained why history can't be changed quietly.