Git: commits are snapshots, the staging area and the object model
Why a focused commit decides whether you can revert, bisect and review: Git's three areas, the object model (blobs, trees, commits), Conventional Commits, everyday undo with restore and amend, and how to review commits made by an AI agent.
On this page
1. The problem
An AI agent fixes a fee bug in 3 lines, but its formatter changes 300 more lines across 14 files, and git add . picks up .env too. It commits "fix stuff" to main. Three days later you need to revert just the fee fix and can't separate it, and the payment gateway key has leaked.
2. Key idea
The unit for revert, bisect and review is one commit: mix two ideas and you can't undo one alone.
Git is a content-addressed key-value store. A blob is one file's content, a tree is a directory, a commit points to the root tree and its parent commit. The commit hash is computed from the tree, parents, author and message, so changing anything gives a new hash.
| Operation | Use when | Don't use when |
|---|---|---|
git add -p (stage hunks) |
one edit contains two ideas | the change is already one idea |
git add . |
you just read git status and .gitignore is solid |
you haven't checked git status, unknown files exist |
git commit --amend |
fixing the last commit not yet pushed | it's pushed to a branch others use |
A separate style commit for the formatter |
the formatter touched many lines | formatting is on the lines you changed |
2.1 How should messages look?
Follow Conventional Commits: type(scope): description. The spec defines only feat and fix; style, refactor, perf, test are common conventions (Angular, commitlint). A ! or a BREAKING CHANGE: footer means a major bump.
- Subject in the imperative, about 72 characters: "add", not "added".
- The body explains why; the diff already shows what.
3. In practice
Read exactly what you're about to commit before committing; every other habit revolves around that.
git status # what's modified, staged, untracked
git add -p # pick hunks: just the fee fix
git diff --staged # read exactly what goes into the commit
git commit -m "fix(fee): apply member discount before express surcharge"
git add -p && git commit -m "style: reformat with google-java-format"
git restore --staged .env # staged by mistake: unstage, file stays
git commit --amend --no-edit # fold staged changes into the last commit (not pushed)
git log --oneline --graph --decorateIntelliJ and VS Code can stage individual lines too.
The object model: check it yourself
git init -b main lab && cd lab
echo "hello" > a.txt && echo "world" > b.txt
git add . && git commit -qm "first"
echo "world v2" > b.txt && git commit -qam "second"
git cat-file -p HEAD^{tree} # a.txt points to the same blob as in the first commit
git ls-tree HEAD~1 a.txt # same blob hash
cat .git/refs/heads/main # a branch is one line holding a hash- Unchanged files aren't stored twice: the repo doesn't grow with the number of commits.
- A branch holds one hash, so branches are nearly free;
HEADpoints to the current branch. - Content addressing isn't unique to Git: Docker image layers use
sha256:digests too.
4. Working with AI
Agents write code fast but commit greedily: they stage everything that changed. You decide what belongs in one commit.
Hand to AI: drafting messages from git diff --staged, proposing how to split a pile of changes into a few commits.
Check in AI-made commits:
-
git diff --staged --name-onlyhas no.env, build output or logs. - One idea per commit; formatter changes sit in their own
stylecommit. - The type matches the diff: a "refactor" that changes behaviour is wrong.
- The agent doesn't
--amendorpush --forcepushed commits. - Every commit builds and passes tests, so
git bisectworks.
5. Exercises
Exercise 1: Review a "refactor only" commit
The agent says: "Refactored the fee code, no behaviour change." In FeeCalculator.java, - if (subtotal >= 500_000) became + if (subtotal > 500_000). Find the problems, show how to prove them, redo the commit.
refactor(fee): clean up FeeCalculator
.env | 2 +
src/main/java/fee/FeeCalculator.java | 41 ++---
src/main/java/fee/MemberDiscount.java | 212 ++++++------
src/test/java/fee/FeeCalculatorTest.java | 6 +-Solution to Exercise 1
.envwas committed: if pushed, treat it as leaked and rotate the key first. Not pushed:git restore --staged .envand add it to.gitignore.- A behaviour change hidden in a "refactor": a cart of exactly 500,000 loses free shipping. Prove it with a boundary test at
subtotal = 500_000; a modified old test is a sign the agent bent the test to fit the new code. - 212 lines in
MemberDiscount.javaare almost certainly formatting: split into astylecommit.
Redo: git reset --soft HEAD~1 (not pushed), then git add -p into style: ..., refactor(fee): ... (old tests untouched), and if > was intended, fix(fee): ... with the reason.
Exercise 2: Read a leaked-key incident
At 10:02 the agent pushed a commit with .env holding the payment gateway key; you notice at 10:40. A colleague says "run git filter-repo to remove it from history, force push, done". What do you do in the first 15 minutes, and what's the root fix?
Solution to Exercise 2
- 15 minutes: rotate the key (revoke the old one, issue a new one, update the secret store), review access logs for the old key since 10:02, notify whoever owns security. Rewriting history does not revoke the key: every clone, fork and CI cache since 10:02 still has the commit.
- Afterwards:
git filter-repois cleanup, not containment; ask everyone to re-clone. - Root fix:
.envin.gitignore, secrets from a secret manager, secret scanning in pre-commit and CI, agents can't push to shared branches.
Summary
- A commit is a snapshot; unchanged files reuse blobs by hash.
- A branch is a file holding a hash; amend and rebase create new commits.
- Amend only unpushed commits; once pushed, add a fixing commit.
- One idea per commit; read
git diff --stagedbefore committing. - Pushed a secret: rotate now, clean history later.
Read more: Part 2: Branching, merge, rebase and PRs.
Practice interview questions on this topic
- How does Git store data? What are a commit, a branch, the staging area and HEAD really?
- How do git clone, fetch, pull and pull --rebase differ?
- Your push to the remote is rejected. How do you handle it?
- What is git cherry-pick, how do you use it, and when should you avoid it?
- How do you undo changes in Git: how do reset, revert, amend and reflog differ?
- How do CI, continuous delivery and continuous deployment differ? What stages does a Spring Boot pipeline need?
Comments
Community rules