Git internals
Git feels magical until you see how simple its core is. Underneath, Git is a content-addressable key–value store plus a few pointers. Understanding this makes every command, including reset, rebase and detached HEAD, suddenly make sense.
Everything is an object
Inside .git/objects, Git stores four kinds of objects. Each is identified by the SHA-1 hash of its contents, the 40-character ids you've seen, like a1b2c3d….
| Object | Stores | Analogy |
|---|---|---|
| blob | The contents of one file (no name!) | A file's data |
| tree | A directory listing: names → blobs and sub-trees | A folder |
| commit | A root tree + parent commit(s) + author + message | A snapshot with a label |
| tag (annotated) | A name + message pointing at a commit | A bookmark |
commit 9f8e7d6 ──────────────▶ tree 3c4d…
parent: a1b2c3d ├── README.md → blob 5e6f…
author: Asha ├── app.js → blob 7a8b…
message: "Add search" └── src/ → tree 9c0d…
└── search.js → blob 1e2f…Explore with plumbing commands (real terminal)
Git's everyday commands are called "porcelain". Underneath them are "plumbing" commands you can run yourself:
mkdir internals && cd internals && git init
echo "hello" > hello.txt
git add hello.txt
git commit -m "First"
git cat-file -t HEAD # commit
git cat-file -p HEAD # show the commit object
# tree 68aba62e560c0ebc3396e8ae9335232cd93a3f60
# author Asha <asha@example.com> 1732000000 +0530
# committer ...
#
# First
git cat-file -p HEAD^{tree} # show the tree
# 100644 blob ce013625030ba8dba906f756967f9e9ca394464a hello.txt
git cat-file -p ce01362 # show the blob
# hello
git hash-object hello.txt # compute the hash yourself — same id!Why content addressing is brilliant
- Integrity: if a single bit of any file or commit changes, its hash changes. History can't be silently tampered with.
- Deduplication: identical files are stored once, no matter how many commits or folders contain them.
- Snapshots, not diffs: each commit points to a full tree, but unchanged files reuse existing blobs, so it's cheap. Git computes diffs on demand. (It also compresses similar objects into packfiles to save space.)
- Immutability: objects never change. "Changing" a commit (amend, rebase) creates a new commit with a new hash. That's why rebased commits get new ids.
Refs: names for commits
Branches and tags are just files containing a commit hash:
cat .git/HEAD # ref: refs/heads/main
cat .git/refs/heads/main # 9f8e7d6c5b4a… (a commit hash)
git branch experiment
cat .git/refs/heads/experiment # same hash — a branch is a 41-byte file!- HEAD usually points to a branch (
ref: refs/heads/main). In detached HEAD state, it holds a commit hash directly. - Committing creates a commit object and moves the branch that HEAD points to.
git reset <commit>simply rewrites the branch file to point somewhere else.
Relative references
| Syntax | Means |
|---|---|
HEAD~1 / HEAD^ |
Parent of HEAD |
HEAD~3 |
Great-grandparent (following first parents) |
HEAD^2 |
The second parent of a merge commit |
main@{yesterday} |
Where main was yesterday (from the reflog) |
v1.0..main |
Commits in main that aren't in v1.0 |
Try them in the simulator:
The index, revealed
The staging area is a real binary file, .git/index. It lists every tracked path with the blob hash that's staged for it:
git ls-files --stage
# 100644 ce013625030ba8dba906f756967f9e9ca394464a 0 hello.txtDuring a merge conflict, the index holds three versions of a conflicted file: stage 1 (the base), 2 (ours) and 3 (theirs). That's exactly what merge tools show you.
Peek inside .git
.git/
├── HEAD current branch pointer
├── config repo settings and remotes
├── index the staging area
├── objects/ all blobs, trees, commits, tags (+ pack files)
├── refs/heads/ local branches
├── refs/tags/ tags
├── refs/remotes/ remote-tracking branches (origin/main…)
├── logs/ the reflog
└── hooks/ scripts run on events (pre-commit, pre-push…)Hooks let you run checks automatically, like running linting before every commit. Tools like Husky and pre-commit manage them for teams.
Practice (real terminal)
- Create a repo, commit two files with identical contents but different names, and use
git cat-file -p HEAD^{tree}to confirm both names point to the same blob. - Create a branch and confirm with
cat .git/refs/heads/<name>that it's just a hash. - Amend your last commit and compare the commit hash before and after.
1.What does a Git blob store?
2.Why does a rebased commit get a new hash?