Field note / Git
Git Reset --soft: The Simplest Way to Squash Commits
How git reset --soft squashes a branch into one clean commit without interactive rebase, and how to use it to clean up after AI coding agents.
You’ve been working on a feature branch. Nine commits deep, one per fix, nice and organized during development. But now it’s PR time, and your reviewer doesn’t want to wade through nine commits just to understand what changed. You need one clean commit.
If you’ve ever used git rebase -i for this, you know the drill: open an editor, change pick to squash or fixup on every line but the first, save, then edit the combined message. It works, but it’s a lot of ceremony for “make these one commit.”
There’s a simpler way. Enter git reset --soft.
How Git Tracks Changes
Before we get into the trick, let’s quickly review how Git organizes your code. Git has three “areas” where your changes live:
- Working directory: your actual files on disk, what you see in your editor.
- Staging area (index): what’s queued up and ready for the next commit.
- HEAD: the last commit on your current branch.
When you run git commit, Git takes everything in the staging area and creates a new commit, moving HEAD forward. Think of it like a conveyor belt. Files move from your editor, to the staging area, and finally into a commit.
The Three Flavors of Reset
git reset moves HEAD backward to a previous commit. The flag you pass determines what happens to the changes from the “removed” commits:
--hard: Throws away everything. Working directory and staging area are wiped clean. Destructive. Handle with care.--mixed(default): Keeps changes in your working directory but unstages them. You’d need togit addagain.--soft: Keeps changes staged and ready to commit. Your working directory is untouched.
That last one is the key. With --soft, Git only moves the HEAD pointer. Your files and staging area stay exactly as they are. All those changes are still there, just waiting to be committed again as one.
Squashing With –soft
Say your branch looks like this:
A - B - C - D - E - F - G - H - I - J
^ ^
base commit HEAD
(0bdf85c) (9 commits later)
Commits C through J are your nine feature commits. You want them as one.
git reset --soft 0bdf85c
Now your history looks like this:
A - B
^
HEAD
But here’s the catch: every change from C through J is still staged. Git didn’t touch your files or the index. It only moved the HEAD pointer backward.
Now commit:
git commit -m "Add user authentication with OAuth2 support"
A - B - K
^
HEAD
Commit K contains the exact same file changes as C+D+E+F+G+H+I+J combined. One commit, clean history. That’s it.
Finding the Base Commit
The one thing you have to get right is where to reset to. It’s tempting to write git reset --soft main, and that works only if main hasn’t moved since you branched. If someone merged to main in the meantime, you’d move HEAD to the new tip of main while your index still holds your branch’s older files. The commit you make next silently reverts everything they merged.
Reset to the point where your branch split off instead. Git will find it for you:
git merge-base main HEAD
That prints the base commit’s sha. Replace main with whatever your branch came from: master, develop, or another feature branch.
The Full Squash Workflow
Here’s the workflow I use every time I need to clean up a branch before opening a PR:
# 1. Keep a backup of the original commits
git branch my-feature-backup
# 2. Review everything that's about to become one commit
git diff $(git merge-base main HEAD)
# 3. Reset to where the branch started, keeping all changes staged
git reset --soft $(git merge-base main HEAD)
# 4. Commit everything as one
git commit -m "Add user authentication with OAuth2 support"
# 5. Push
git push --force-with-lease
Key points:
- Step 1 is a safety net. The backup branch still points at your original commits if you need them.
- Step 2 is easy to skip and worth doing. Debug logs and commented-out code are hard to spot when they’re spread across nine commits, and obvious in one diff.
- Step 3 is where the magic happens. All the changes from your commits are preserved in the staging area.
- Step 4 wraps everything into a single, clean commit.
- Step 5 needs
--force-with-leaseif the branch was already pushed, because you rewrote its history. Unlike plain--force, it refuses to overwrite commits someone else pushed in the meantime.
Delete the backup once the PR is merged: git branch -D my-feature-backup.
Why Not Interactive Rebase?
git rebase -i is the “official” way to squash, and it’s a powerful tool. But for the simple case of “I want all these commits as one,” it’s overkill:
- It opens an editor where you mark every commit as
squashorfixup - Deleting a line in that list by mistake drops the commit and its changes
- It replays commits one by one, so on a branch that also merged
mainin, it can stop partway with conflicts
git reset --soft skips all of that. No editor, no replay, no marking individual commits. Just: move the pointer back, commit once. Done.
When To Use Each
| Scenario | Use |
|---|---|
| Squash all commits into one | git reset --soft |
| Squash some, keep others | git rebase -i |
| Reorder commits | git rebase -i |
| Edit a commit message mid-history | git rebase -i |
If you just want one clean commit from many, reset --soft is the fastest path. If you need surgical precision over individual commits, reach for rebase -i.
Cleaning Up After AI-Assisted Development
This is where I use it most. AI coding agents like Claude Code and Cursor don’t write a feature in one pass. They work in tight loops: write some code, run the tests, read the failure, fix it, run again. Each loop often ends in a commit, either automatically or because you’re saving checkpoints. By the time the feature works, the branch looks like this:
a1b2c3d Add initial payment service
f4e5d6c Fix missing import
7g8h9i0 Update return type
b2c3d4e Fix test assertion
e5f6g7h Add error handling
1a2b3c4 Fix typo in error message
...
That’s not a feature story. That’s a debugging diary.
Checkpoints vs. history
During development, those commits are useful. If the agent takes a wrong turn on iteration 12, you can go back to iteration 11 and try a different prompt. Don’t fight them. Let the agent commit freely, on a feature branch so main stays clean and the base is easy to find.
Once the feature works, the checkpoints are clutter. The history should say what was built and why, not how an agent debugged its way there. So run the same workflow as above: back up, review the full diff, reset to the merge base, commit.
The review step matters more here than anywhere. Agents leave behind debug output, unused imports and half-finished helpers, and you won’t see them commit by commit.
Keeping some structure
Sometimes one commit is too aggressive, for example when the branch has a database migration you’d like reviewed on its own. Use a plain (mixed) reset instead, which unstages everything, then commit in logical groups:
git reset $(git merge-base main HEAD)
git add src/migrations/
git commit -m "Add payment tables migration"
git add src/payments/ tests/payments/
git commit -m "Add payment processing service with tests"
Two clean commits instead of one giant one or twenty tiny ones.
Write the message the agent didn’t
The micro-commits never explain the feature, so this is where you add that:
git commit -m "Add Stripe payment processing
Implements one-time charges and subscription billing, with input
validation and a webhook endpoint for payment status updates.
Closes #142"
Beyond PRs: Moving Work Between Branches
Say you built a feature on branch-a across 12 commits, and now you need it on branch-b. Cherry-picking 12 commits means 12 chances to stop on a conflict halfway through.
Squash on a temporary branch first, then cherry-pick the single result:
# Squash the 12 commits into one
git checkout -b temp-branch branch-a
git reset --soft $(git merge-base main HEAD)
git commit -m "Complete feature X"
# Now cherry-pick just one commit
git checkout branch-b
git cherry-pick temp-branch
# The temporary branch has done its job
git branch -D temp-branch
One cherry-pick instead of twelve, and at most one conflict to resolve.
Wrapping Up
git reset --soft is one of those Git commands that feels like a cheat code once you learn it. No editor, no replaying commits one at a time. Just find the merge base, move the pointer, commit once.
Next time you’re staring at a branch with a dozen commits, from you or from an agent, give it a try. Your reviewers will thank you.