By admin , 29 April 2026

What is an atomic commit?

An atomic commit captures exactly one logical change. It compiles, passes its own tests, and can be reverted in isolation without breaking anything else. Atomic commits make code review pleasant, git bisect precise, and git revert safe.

The smell test

If you find yourself writing a commit message with the word "and" - "Add login and fix typo and refactor helper" - you are looking at three commits crammed into one. Split them.

Staging selectively with git add -p

The -p (patch) flag walks you through each hunk in your working tree and asks whether to stage it. This is the single best tool for crafting atomic commits after the fact.

git add -p src/checkout.js
# y - stage this hunk
# n - skip this hunk
# s - split into smaller hunks
# e - manually edit the hunk

A worked example

Suppose you fixed a bug in checkout.js, added a test, and noticed a typo in README.md along the way. Three atomic commits beat one mixed commit:

git add src/checkout.js
git commit -m "Fix off-by-one in cart total calculation"

git add tests/checkout.test.js
git commit -m "Add regression test for cart total bug"

git add README.md
git commit -m "Fix typo in installation section"

Splitting commits you already made

Used git rebase -i to revisit a sloppy commit:

git rebase -i HEAD~3
# Mark the messy commit as 'edit'
# When rebase pauses:
git reset HEAD^
git add -p
git commit -m "First atomic piece"
git add -p
git commit -m "Second atomic piece"
git rebase --continue

Combining accidental over-splitting

The opposite problem - too many tiny commits - is solved with squash. During interactive rebase, mark commits as squash or fixup to fold them into the previous commit.

Keep tests with their code

An often-debated point: should the test for a feature live in the same commit as the feature, or a separate commit? The pragmatic answer is "same commit." That way git bisect never lands on a commit where the feature exists but its tests do not, which would create false negatives.

Atomic commits and code review

When you open a pull request with five atomic commits instead of one giant one, GitHub and GitLab let reviewers walk commit-by-commit. The reviewer reads each commit as a self-contained story, and the review becomes a conversation about decisions rather than a hunt through tangled hunks.

Atomic commits and bisect

Atomic commits make git bisect precise. If a regression is introduced, bisect lands on the exact commit responsible. If commits mix concerns, bisect points to a hundred-line commit and you still have to dig.

git bisect start
git bisect bad HEAD
git bisect good v2.4.0

The discipline of atomic commits compounds. After a year, your project's history becomes a readable narrative instead of a swamp.