How Git stores data
Git is the foundation of every DevOps workflow: CI pipelines trigger on it, GitOps treats it as the source of truth, and code review, releases and audit trails all live in it. Senior interviews go beyond add/commit/push: they ask how Git stores data, merge vs rebase and when each is dangerous, how to recover from mistakes (a force-push, a leaked secret, a bad merge on main), which branching strategy suits continuous delivery, and how to run repositories at scale in CI.
Snapshots, not diffs
Git stores your project as a series of snapshots. Every commit records the full state of the tree at that moment; unchanged files are simply re-referenced, so this is cheap. (Git compresses similar objects into packfiles with deltas for storage, but conceptually each commit is a snapshot.)
The object database
Everything lives in .git/objects as content-addressed objects, named by a hash (SHA-1 by default, SHA-256 optional in newer Git):
| Object | Contains |
|---|---|
| blob | The contents of one file (no name, no permissions) |
| tree | A directory listing: names, modes, and pointers to blobs and sub-trees |
| commit | A pointer to the root tree, parent commit(s), author, committer, timestamp, message |
| tag (annotated) | A pointer to an object plus tagger, date, message, optional signature |
git cat-file -t 3f9c2a1 # type of an object
git cat-file -p 3f9c2a1 # pretty-print it
git cat-file -p HEAD^{tree} # the tree of the current commit
git hash-object README.md # the hash Git would give this content
Because objects are addressed by the hash of their content, history is tamper-evident: changing an old commit changes its hash and every descendant's hash. That's why "rewriting history" produces new commits and why shared history must not be rewritten.
References: branches, HEAD and tags
- A branch is just a file containing a commit hash (
.git/refs/heads/main). Creating a branch is creating a 41-byte file — that's why branching is cheap. - HEAD points to the current branch (
ref: refs/heads/main), or directly to a commit in detached HEAD state (e.g. after checking out a tag). - Tags point at a commit and don't move: lightweight tags are plain refs; annotated tags are objects with a message and author — use annotated (and signed) tags for releases.
- Remote-tracking branches (
origin/main) record where the remote's branches were at your last fetch.
The three areas
- Working tree — files on disk.
- Index (staging area) — the snapshot you're preparing for the next commit; lets you commit part of your changes (
git add -p). - Repository — committed history.
git status compares all three; git diff shows working tree vs index; git diff --staged shows index vs HEAD.