Opscraft
Git · Lesson 1 of 8 · 4 min read

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.

Enlarge diagramHEADref: mainmaina branchcommit 8d21be4author, date, messagecommit 3f9c2a1the previous snapshottreethe folder listing:names and modesblobREADME.md contentsblobapp.py contentstreethe src/ folderparentUnchanged files reusethe same blob.
Figure 1. What a commit really is: a pointer to a tree (a folder listing), which points to blobs (file contents). Branches and HEAD are just names pointing at commits.
Swipe sideways to see all of it, or tap to open it full screen.

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.
Enlarge diagramABCDEtag v1.0origin/mainmainfeatureHEADtime →you are on feature
Figure 2. Branches, tags, HEAD and remote-tracking branches are all just labels on commits. Only branches move when you commit.
Swipe sideways to see all of it, or tap to open it full screen.

The three areas

Enlarge diagramWorking treethe files on disk youeditIndex (stagingarea)the next commitRepositorycommitted history(HEAD)git addgit commitunstagegit restore / git switch
Figure 3. Git's three areas. You choose what goes into the next commit by staging it first.
Swipe sideways to see all of it, or tap to open it full screen.
  • 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.