Git Internals: Objects, Branches, and Rebase vs Merge
Understand Git's content-addressable object store (blobs, trees, commits), lightweight refs, and how rebase replays commits onto a new base.
TL;DR
Git is a content-addressable filesystem: files are blobs, directories are trees, snapshots are commits, and branches are just 41-byte pointer files (refs). Merge joins two histories with a new merge commit; rebase replays your commits onto a new base, producing brand-new commit hashes.
Intent
Understand what Git actually stores in .git/objects and .git/refs, so branches, HEAD, merge, and rebase stop feeling like magic when history diverges or a rebase conflicts.
Problem
Treating Git as a sequence of diffs or folders makes detached HEAD, fast-forward, and rebase force-pushes terrifying. Without knowing that branches are mutable pointers to immutable commits, developers accidentally rewrite shared history or lose track of dangling commits.
Solution
Every object in .git/objects is hashed (SHA-1 by default) and immutable:
- Blob: raw compressed file content (no filename or permissions). Identical files across commits share one blob.
- Tree: maps filenames and modes to blobs and subtrees.
- Commit: points to one root tree, zero or more parent commits, plus author, committer, and message.
- Branch (
refs/heads/<name>): a plain file containing a commit hash.HEADis usually a symbolic ref pointing to the current branch.
Because commits are immutable, merging feature into main creates a new commit with two parents (main tip and feature tip). Rebasing feature onto main computes the diff of each commit on feature and applies them one by one on top of main, generating new commit hashes (f1' and f2') and moving the feature branch pointer.
Real-world analogy
▶ Flow: watch it work
Git Rebase: Replaying Commits onto New Base
Press Start or tap any step dot to trace the path.
▸All steps (5)
Code
Inspect real Git objects directly in a repository with plumbing commands (git cat-file, git rev-parse):
# Verified with git 2.54.0.windows.1 in this repo
git rev-parse HEAD
# Inspect the commit object (tree, parent, author, message)
git cat-file -p HEAD
# Inspect the root tree pointed to by HEAD
git cat-file -p HEAD^{tree}# Rebase feature onto updated main (not run)
git switch feature
git rebase mainWhen to use / when to avoid
- Rebase your local, unpushed feature branch onto main to keep a clean linear history before opening a PR.
- Use merge (or squash-merge) when integrating a PR into a shared main branch so shared history is never rewritten.
- Never rebase commits already pushed to a shared branch that teammates have pulled.
- Avoid rebase when you want to preserve the exact historical topology of a long-lived integration branch.
How to implement
Check your branch state
Run git status and git log --oneline --graph -n 10 before rewriting anything.
Rebase local commits cleanly
Run git fetch origin && git rebase origin/main. Resolve any conflict per commit, then git rebase --continue.
Recover safely if needed
If a rebase goes wrong, run git rebase --abort during the rebase, or git reflog afterward to reset to the old hash.
Pros / Cons
- Rebase produces a linear, bisectable commit history without noisy merge commits.
- Content-addressable storage deduplicates identical blobs automatically.
- Rebasing a long-diverged branch forces you to resolve conflicts commit by commit instead of once.
- Rewriting published commits breaks teammates unless everyone resets or uses --force-with-lease.
Relations
Check yourself
Check Yourself
1. If two commits contain an identical 10 MB asset file, how many blob objects does Git store in .git/objects?
2. After running git rebase main on a 3-commit feature branch, what happens to the original 3 commits?