Skip to navigation

Claude Code Atomic Commits: Make Changes Revertible

In short: Split behavior, tests, and cleanup into reviewable units, then check that each commit has one clear purpose.

Published 17 min read
On this page

Direct answer

An atomic commit has one reason to exist. It changes one behavior, or adds the tests for one behavior, or does one cleanup. Split an AI-assisted change along those lines and each commit becomes something a reviewer can read alone and a release engineer can revert alone. A commit that mixes a behavior fix with unrelated cleanup and a version bump cannot be reverted without taking the other two along.

The discipline costs a few minutes of staging work. It pays back the first time a single change needs to come out of a release, and it makes review faster because each unit has a stated purpose rather than a pile of files.

Evidence map linking a commit to its single purpose, its dependencies, and the revert boundary it creates.
Editorial evidence map based on F061, F062, and F063. It organizes the atomicity method; it does not measure review speed or claim a defect reduction.

Why mixed-purpose commits are expensive later

A commit that does three things creates three problems. Review is harder because the reader has to separate concerns the author did not. Revert is riskier because removing one behavior removes the other two. Diagnosis is slower because a bisect lands on a commit whose diff explains several changes at once.

The cost is deferred, which is why it keeps getting paid. On the day the commit is written, the mixture feels efficient. Weeks later, when a single behavior must leave a release, the mixture is a blocker. The options become a partial revert that requires editing, or a full revert that takes unrelated work with it.

Claude Code can produce large diffs quickly. A single session that fixes a bug, renames a helper, and updates a dependency produces exactly the shape that causes the later problem. The agent will happily commit all of it at once if nobody asks for structure.

List the independent behavior changes

Before staging anything, list what the change actually does. Most AI-assisted edits contain more than one intent, and the list is usually shorter than the diff suggests.

Write each item as a behavior, not a file. “Rejected saves keep the user’s input” is a behavior. “Changed SaveButton.tsx and api.ts” is a file list. The behavior form is what becomes a commit purpose.

Ask Claude Code for the list. A prompt like “list the distinct behaviors this diff changes, one per line, ignoring file names” produces a starting inventory. Verify it against the diff, because the agent can merge two behaviors that look similar or split one behavior across lines.

Changed file Behavior Commit unit Depends on
api/save.ts Keep entered values when a save is rejected Commit 1: behavior fix None
SaveButton.test.tsx Cover the rejected-save path Commit 2: tests Commit 1
helpers/format.ts Rename formatDate to formatDateForUser Commit 3: cleanup None
package.json, lockfile Bump patch version for release Commit 4: release metadata Commits 1 to 3

The dependency column matters. A test commit that depends on the behavior commit cannot be reordered ahead of it, because the test would fail on its own. Order the units so each one builds and passes its own tests in sequence.

Workflow from listing independent behaviors to separating code, tests, and cleanup into ordered commit units.
Editorial workflow based on F061 and F063. The sequence describes a staging discipline, not an automated Claude Code feature.

Separate code, tests, and cleanup

Three categories cover most changes. Behavior code changes what the product does. Tests cover what the product should do. Cleanup changes how the code reads without changing behavior.

Keeping them apart has a specific benefit: a revert of the behavior commit leaves the tests behind, and those tests now fail, which is a signal that the behavior is gone. That is useful information. A combined commit hides it, because reverting the mixture removes the test along with the behavior.

Some teams prefer the test in the same commit as the behavior so the two never separate. That is a legitimate policy. The rule that matters is that the policy is explicit and applied consistently, not that tests always live in their own commit.

Commit purpose Included paths Excluded paths Verification
Fix rejected-save behavior api/save.ts tests, formatting, config Focused behavior test
Add rejected-save tests SaveButton.test.tsx implementation files Test run on the new commit
Rename format helper helpers/format.ts, callers behavior files Full type check
Release metadata package.json, lockfile source files Build check

The verification column is the test that the commit stands alone. A behavior commit whose focused test only passes after the cleanup commit is not standalone, and the order needs adjusting.

Keep release metadata in its own unit

Version numbers, changelogs, and dependency lockfile changes are release decisions, not code changes. They belong in a commit a release engineer can include or drop without touching the product.

Official, F062: GitHub documents configurable squash merging for pull requests. Squash changes the final published history, which means a tidy local series may collapse into one commit on the target branch.

Squash does not make local atomicity pointless. The local units are what you reason about while building the change and what you use to write the squash message. The published history is cleaner, but the reasoning happened at the unit level.

Release metadata also interacts with compatibility decisions. Choosing a version impact with SemVer covers how to classify the change; the atomicity point here is narrower. Keep the version decision in a unit that can be moved between releases without carrying code with it.

Write one purpose per commit

The commit message should state the single purpose. A reader who sees the message and the diff should be able to confirm they match in a few seconds.

Messages that name a purpose also make the series readable as a whole. Six commits with purposes read like a short changelog. Six commits that say “update” and “fix” and “more” read like noise.

Official, F061: Claude Code documentation describes a batch workflow that splits large mechanical changes into units, each opening a pull request. The example concerns mechanical batch work and is not a rule that every change must become a separate pull request.

The batch example is about pull requests, but the underlying idea transfers to commits. Large mechanical work is easier to review and reason about in units with one purpose each.

Do not create artificial tiny commits with no standalone meaning. Splitting a single behavior into five commits, each moving one line, produces noise without benefit. The test is whether each unit means something on its own.

Inspect each commit diff

After staging, look at each commit the way a reviewer will. Git show displays a single commit’s change, which is the reviewer’s view. Read the diff and the message together and check two things: does the diff match the stated purpose, and is anything in the diff unrelated to that purpose?

Ask Claude Code to perform the check. “For each commit in this range, state the purpose from the message and list the files changed. Flag any commit where the files do not match the purpose.” The agent is quick at this kind of comparison and will catch the stray file you stopped seeing.

Pay attention to generated files and lockfiles. They frequently end up in the wrong unit because they changed as a side effect. A lockfile change belongs with the dependency change that caused it, not with an unrelated behavior fix.

Review gate Pass condition Failure response
Message matches diff Files and purpose agree Restage into the correct unit
No unrelated files Every path serves the purpose Move the stray path to its own unit
No mixed cleanup Behavior and formatting are separate Split the commit
Generated files grouped Lockfiles sit with their cause Restage the lockfile
Message is specific A reader can tell what changed and why Rewrite the message

Check whether each unit builds and tests

The defining test of atomicity is whether each commit stands on its own. Walk the series and confirm the tree at each commit is in a working state: it compiles, its own tests pass, and nothing depends on a later commit.

Full builds at every commit are expensive for a long series. A practical compromise is to check the units that change behavior and the units that add tests, and to treat pure formatting commits as lower risk. The check exists to find ordering mistakes, not to run the full pipeline repeatedly.

Derived, F063: Giving each commit one purpose and checking whether its code, tests, and metadata stand independently is a derived review practice. Some changes have intentional coupling, and the check should confirm the coupling is deliberate rather than assume atomicity is always possible.

The honest exception is a change that cannot be split without breaking the build at an intermediate commit. A migration that renames a column and updates every caller in the same operation may need to stay together. Document that coupling in the commit message so the next reader does not try to split it.

Revert scenario Affected commit Compatibility note Owner
Rejected-save fix breaks a client Behavior commit Tests remain and will fail, which is expected Backend owner
Format rename causes a merge conflict Cleanup commit No behavior change, safe to drop Module owner
Dependency bump causes a regression Release metadata commit Drop the bump, keep the code Release owner
Migration must stay whole Combined migration commit Documented coupling, revert as one unit Data owner

Ask Claude Code to find mixed intent

A fresh pass over the finished series catches mixtures the author normalized. Two prompts cover it. First, ask for the purpose of each commit and whether any commit contains unrelated work. Second, ask which single commit would be hardest to revert safely and why.

The second question surfaces the coupling directly. A commit flagged as hard to revert is either genuinely coupled, in which case document it, or badly structured, in which case split it.

Review the agent’s answer rather than accepting it. The agent reads the diff and the messages, but it does not know which couplings the team intended. Overengineering large diffs covers the review-side cost of oversized changes; atomic commits are the author-side habit that keeps diffs small enough to review.

Tradeoff between the effort of splitting commits and the later ability to revert one behavior independently.
Editorial tradeoff diagram based on F061 through F063. Splitting costs staging effort now and buys independent reverts later; it does not guarantee a clean revert in every case.

Document any intentional coupling

Some commits are deliberately not atomic. A schema migration and the code that uses the new column may need to land together. A protocol change on both sides of a service boundary may be inseparable.

Say so in the commit message. “Migration and caller update are coupled: the code fails to compile against the old schema” tells the next reader not to attempt a split and tells the revert planner to treat the pair as one unit.

The record also helps a bisect. When a search lands on a coupled commit, the message explains why the diff is large. Without the note, the commit looks like a mistake.

Worked example: a bug fix, a rename, and a version bump

Suppose a session fixes a rejected-save bug, renames a formatting helper, and bumps the patch version. All three appear in the working tree at once. Staging them as one commit is the default outcome.

The inventory finds three behaviors and one release decision. Four units result. The behavior fix goes first because the tests depend on it. The tests go second. The rename goes third because it is independent. The version bump goes last because it represents the release.

Checking each unit in sequence: the behavior commit compiles and its focused test passes. The test commit adds coverage and passes. The rename commit compiles and the full type check passes. The version bump changes no source and the build passes.

Derived arithmetic for the series: The working tree contained changes to 4 logical intents across 6 files. Split into 4 commits, the largest single-commit diff covers 2 files (the behavior fix touching implementation plus its direct call site). Reverting the behavior commit alone affects 2 files and leaves 3 tests in place that now fail, which is the intended signal. This arithmetic tracks the structure; it is not a benchmark of review time.

The concrete win is that any one of the four units can leave a release without dragging the others. The concrete loss is that the split required staging discipline and a few extra checks, and a team that squash-merges will collapse the four units into one published commit anyway.

Practical checklist

  1. List the distinct behaviors in the change before staging anything.
  2. Group files by purpose, not by directory or edit order.
  3. Keep code, tests, and cleanup separable unless the team policy says otherwise.
  4. Put release metadata in its own commit at the end of the series.
  5. Write one purpose per commit message.
  6. Inspect each commit with git show and compare the diff with the message.
  7. Walk the series to confirm each unit builds and its own tests pass.
  8. Ask Claude Code to flag mixed intent and hardest-to-revert commits.
  9. Document intentional coupling in the commit message.
  10. Keep force-push and history rewrites consistent with the branch policy before publishing.
Control checklist covering purpose inventory, unit separation, per-commit inspection, and standalone verification.
Editorial control checklist derived from F061 through F063. Passing the checklist improves revertability; it does not guarantee every change can be split cleanly.

Concrete win, honest loss, and practical implication

Win: Each unit in the worked example can be reverted on its own. The behavior fix leaves its tests behind as a signal, the rename can be dropped without touching behavior, and the version bump can move between releases freely.

Loss: Squash merging at the pull request level can collapse the local series into one published commit, so the published history may not show the structure. The structure still helped during review and while writing the message, but it does not survive into the target branch by itself.

Implication: Use atomic commits when the change mixes behavior, cleanup, and release decisions, which is the common case for AI-assisted work. For a single-purpose change with no unrelated edits, a single commit is already atomic and no splitting is needed.

Verdict that one-purpose commits improve revertability while intentional coupling still needs documenting.
Editorial verdict based on F061 through F063. The benefit is independent reverts; the honest limit is that some changes are coupled on purpose and must stay together.

Where this connects in the batch

The message that states each unit’s purpose is covered in encoding change intent in commit messages. The integration method that carries the units to the target branch is covered in choosing merge or rebase from the branch policy.

Reverting is the recovery side of the same coin. Planning a rollback with revert covers the release-level decision, and backports and cherry-picks for hotfixes covers moving a single fix to another line, which depends on the fix being isolated.

For diagnosis, hunting regressions with git bisect works best when each commit has one purpose. A mixed commit makes the bisect result ambiguous.

Adjacent reading on review cost includes the code review bottleneck, overengineering large diffs, and verification done proof. The false success from passing tests post covers the case where each unit passes in isolation but the combination misbehaves.

The six posts in this batch form one sequence. Choosing merge or rebase from the branch policy covers how the units travel to the target branch. Resolving merge conflicts by behavior covers the decisions that produce the units. Encoding change intent in commit messages covers the record each unit leaves. Recovering failed merge-queue groups covers the queue that validates the combination. Defining merge checks with branch protection covers the settings the queue relies on.

Release-facing decisions continue in choosing a version impact with SemVer, annotated release tags, hotfix backports and cherry-picks, planning a rollback with revert, preparing a changelog from consumer-visible changes, and running a release checklist. Diagnosis and testing coverage starts at hunting regressions with git bisect, selecting tests for changed code, and triaging flaky tests.

Adjacent library posts on review cost and safety include the code review bottleneck, overengineering large diffs, false success from passing tests, verification done proof, destructive git-reset guardrails, database migration safety, dependency install side effects, and session handoff for team context. Estimating monthly LLM API cost is the non-blog companion for the spend side of producing large changes.

Data gap and future update

The fact pack contains no measured link between atomic commits and review time, defect rate, or revert safety. No benchmark is claimed here, and the worked example’s arithmetic tracks structure rather than performance.

The official evidence covers the mechanics of git log, git show, git revert, squash merging, and one Claude Code batch example. It does not quantify the benefit of one-purpose commits. A future update should add a documented repository case study if one becomes public.

Until stronger evidence exists, treat atomicity as a reviewable practice with a known limit: some changes are coupled on purpose, and the discipline includes recognizing and documenting those cases rather than forcing a split.

One AI API Key option for OpenAI-format agents

Claude Code uses an Anthropic-format route, so the One AI free model IDs are not a Claude Code model source. The research packet records Claude IDs as unavailable on 2026-11-27.

For a separate OpenAI-format agent, One AI API Key offers https://api.oneaiapikey.com/v1, one key, and 46 live free IDs. That option concerns a different client and request format, not a workaround for Claude Code authentication. Review the free model list and eligibility details before choosing an agent route, or start from the quickstart.

FAQ

Does squash merging make atomic commits pointless?

No. Squash changes the published history on the target branch, but the local units are what you reason about while building and reviewing the change. They also drive the squash message content.

Should tests be in the same commit as the behavior they cover?

Either policy works if it is consistent. Separate test commits make a behavior revert visible, because the tests remain and fail. Combined commits guarantee the two never drift apart. Pick one and write it down.

Is there a minimum or maximum commit size?

Size is not the test. One purpose is. A large commit can be atomic if it implements one behavior that cannot be split without breaking the build, and a tiny commit can be noise if it has no standalone meaning.

Can Claude Code split my commits for me?

It can propose a split and stage files into groups when you describe the units. Verify the result with git show, because the agent does not know which couplings the team considers intentional.

What if a change cannot be split without breaking the build?

Keep it as one commit and document the coupling in the message. Forcing a split that leaves an intermediate commit uncompilable makes bisect and revert harder, not easier.

More Claude Code troubleshooting

Sources

More in Blog

Try it

One API key for GPT, Grok, DeepSeek, Kimi, GLM, MiniMax, Qwen, Step, Beam, Nemotron, ERNIE, Hunyuan, Gemma, Laguna, LFM, LongCat, Llama, Solar, Jais, Mistral, K2 and Roleplay.

One AI API Key speaks the OpenAI and Anthropic formats, so you change the base URL and the key, not your code. Credit packs start at $10, and every model has a published per-token rate.

New accounts$10 credit on sign-up
  • 100requests
    per day
  • 3Mtokens
    per day
  • 47free
    models
  • $0per
    token
  • No card
  • No end date
Claim $10 credit →