Track lessons
Clean Up AI Agent Commits with rebase -i and Autosquash
Agents tend to leave a trail of partial attempts and follow-up fixes. Cleaning that history before review lets every remaining commit explain one intentional change instead of the conversation that produced it.
When to use
- Fold small review fixes into the commit that introduced the change.
- Turn an experimental local branch into a readable PR before pushing it.
- Preserve several meaningful commits while removing noisy intermediate ones.
Example
An agent creates an auth feature, then three commits named fix test, fix lint and handle null. Attach those corrections to the original auth commit before opening the PR.
TL;DR
Mark corrections as fixups, then let Git fold them into the right commit.
git commit --fixup=<sha>
git rebase -i --autosquash mainSteps
- 1
See what the agent committed
Inspect only commits on the current branch so you can decide whether to keep their boundaries or rebuild them as one change.
bashgit log --oneline main..HEAD - 2
Option A: squash everything into one commit
Fastest when the history has no value.
bashgit reset --soft main git commit -m "feat(auth): add password reset flow" - 3
Option B: attach a fix to an earlier commit
Stage the correction and point the fixup at the original commit; replace a1b2c3d with its real commit ID.
bashgit add src/auth.ts git commit --fixup=a1b2c3d - 4
Fold the fixups in
The editor opens with fixups already placed. Save and close.
bashgit rebase -i --autosquash main - 5
Enable autosquash by default
If you use fixup commits frequently, this setting makes interactive rebase recognize and position them without repeating the flag.
bashgit config --global rebase.autoSquash true
Gotchas
- Rewriting pushed commits requires git push --force-with-lease.
- Do not rewrite branches other people already built on.
- If a rebase goes wrong: git rebase --abort, or recover with git reflog.
- Use --fixup=amend:<sha> to also change the commit message.
Cheat sheet
| git reset --soft main | Unstage all commits, keep changes |
| git commit --fixup=<sha> | Correction for an earlier commit |
| git rebase -i --autosquash main | Apply fixups automatically |
| git rebase --abort | Cancel a rebase |
| git reflog | Find lost commits |
Related
Sources
Reviewing agent output? CodeCrab reviews pull requests locally with your own AI tools.
