Track lessons
GitCode reviewIntermediate4 min

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.

bash
git commit --fixup=<sha>
git rebase -i --autosquash main

Steps

  1. 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.

    bash
    git log --oneline main..HEAD
  2. 2

    Option A: squash everything into one commit

    Fastest when the history has no value.

    bash
    git reset --soft main
    git commit -m "feat(auth): add password reset flow"
  3. 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.

    bash
    git add src/auth.ts
    git commit --fixup=a1b2c3d
  4. 4

    Fold the fixups in

    The editor opens with fixups already placed. Save and close.

    bash
    git rebase -i --autosquash main
  5. 5

    Enable autosquash by default

    If you use fixup commits frequently, this setting makes interactive rebase recognize and position them without repeating the flag.

    bash
    git 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 mainUnstage all commits, keep changes
git commit --fixup=<sha>Correction for an earlier commit
git rebase -i --autosquash mainApply fixups automatically
git rebase --abortCancel a rebase
git reflogFind lost commits

Related

Sources

Reviewing agent output? CodeCrab reviews pull requests locally with your own AI tools.