Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Make Atomic Git Commits—and Repair a Messy History

Use Git’s index to commit one coherent change at a time. Learn to stage selected hunks, split a local commit, amend the tip, and handle pushed history carefully.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

An atomic Git commit captures one coherent, reviewable idea—not every change currently in your working tree. Use the index to choose what goes into the next commit, inspect that selection, and then commit it. If a local commit combines unrelated work, split or reorganize it; if it has already been shared, coordinate before rewriting it.

What makes a Git commit atomic?

An atomic commit is a logical, reviewable unit of change with a specific purpose. The Git project guide puts it this way: “Commits should be logical, atomic units of change that represent a specific idea.” A commit might, for example, add one validation rule or fix one bug; unrelated formatting changes belong in a separate commit if they have an independent purpose.

As an Amazon Associate I earn from qualifying purchases.

Git builds a commit from the index, also called the staging area. Your working tree can contain other edits that are not staged, so a plain git commit does not automatically include every uncommitted change. That separation is what lets you assemble a focused commit from a larger set of edits.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to create a focused commit

  1. Check your branch and working-tree state with git status. Inspect the unstaged changes with git diff.

  2. Stage the files that belong to one purpose: git add path/to/file. To select only parts of a file, run git add --patch path/to/file (or git add -p path/to/file) and respond to the prompts for each change hunk.

  3. Review exactly what is staged with git diff --cached. If you staged a change by mistake, adjust the index before committing; for example, use git restore --staged path/to/file to unstage a file without discarding its working-tree edits.

  4. Commit with a concise message that says what the change accomplishes: git commit -m "Validate input before saving". Git’s --dry-run option can preview what a commit would include: git commit --dry-run.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can also use git commit --patch to select hunks for a commit interactively. For routine work, explicitly staging hunks and checking git diff --cached makes the boundary of the commit easy to verify.

How to handle common staging cases

Several unrelated changes are in one file

Use git add --patch path/to/file to stage only the hunks that serve the first purpose. Review and commit them, then stage the remaining hunks for another commit. If a single hunk contains both concerns, the interactive patch prompt offers options to split it when Git can divide it; otherwise, edit the change so the concerns can be staged separately, taking care not to lose either edit.

Tracked files and new files

git add -A stages additions, modifications, and deletions across the working tree. By contrast, git commit -a stages modifications and deletions of already tracked files as part of the commit, but does not add new, untracked files. If you are aiming for an atomic commit, check git status and inspect the staged diff rather than relying on either shortcut to choose the right purpose for you.

How to split a commit that already exists locally

If the mixed commit is the latest commit and has not been shared, the most direct approach is to undo that commit while keeping its changes in your working tree, then stage and commit each coherent slice:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Run git reset HEAD^. This removes the tip commit from the current branch and leaves its changes in the working tree, unstaged.

  2. Use git add --patch (or stage whole files with git add path/to/file) to assemble the first coherent slice. Review it with git diff --cached, then commit it.

  3. Stage and commit the remaining changes as one or more separate units. Use git status and git diff to check that no intended changes were left behind.

For an older local commit, start an interactive rebase that includes it, such as git rebase -i HEAD~3 when the target is among the last three commits. In the todo list, change pick to edit for the target commit, save and close the editor, then run git reset HEAD^. Stage and commit the separate slices, and finish with git rebase --continue. Resolve any conflicts Git reports before continuing. Choose a range that includes the target commit; do not include commits you do not intend to rewrite.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to fix or combine local commits

Correct the latest commit

For a small correction to the tip commit, stage the intended change and run git commit --amend. Git replaces the tip commit rather than adding a second one. The command can update the commit contents, its message, or both.

Combine intermediate steps

During git rebase -i, mark a later commit as squash to combine it with the preceding commit and edit the resulting message, or as fixup to combine it while discarding the later commit’s message. Fixup commits can also be created with git commit --fixup=<target> and arranged next to their targets by running git rebase --autosquash -i <base>. Autosquash prepares the rebase todo list; review it before proceeding.

Keep separate commits when each records a useful, understandable step. Squash intermediate work when it is merely cleanup for a single purpose and does not need to stand alone in the history.

When is rewriting a commit safe?

Amending, resetting, and rebasing change the commit sequence. They are straightforwardest when the affected commits exist only on a branch you control. Once a commit has been pushed and others may have based work on it, rewriting can leave collaborators with a different history to reconcile. GitHub’s guidance cautions against rebasing already-pushed commits as a general practice.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.