Claude Code or Codex finishes the feature, and the branch history reads like a diary: wip, fix, fix tests, actually fix tests. The code might be fine, but nobody wants to review that history, and future you running git blame will like it even less.
Reviewing what the agent changed is one job. Turning its pile of commits into something mergeable is another, and git has had good tools for that for years. Here are 13 short ones I'd keep handy on a Mac, from checking out the agent's PR to pushing the cleaned-up branch.
1. Check out agent PRs into their own worktree
When a cloud agent opens a pull request, gh pr checkout can put it in a separate folder with --worktree, so your own branch stays exactly where you left it:
gh pr checkout 42 --worktree ../review-pr-42 2. Make a backup branch before rewriting anything
Every trick below rewrites history, so take ten seconds first: git branch backup/agent-run. If a rebase goes sideways, the reflog still remembers where every branch tip used to be (HEAD@{2} means "where HEAD was two moves ago"), but a named branch is easier to find.
3. Collapse the "wip" pile with reset --soft
If the agent's commits have no structure worth keeping, squash them all at once. git reset --soft moves the branch pointer but leaves every change staged, so you can write one real commit:
git reset --soft $(git merge-base main HEAD) git commit 4. Point small fixes at the right commit with --fixup
When the history is worth keeping and you just need to patch commit three, stage the fix and run git commit --fixup=<sha>. Per the git commit docs, that creates a fixup! commit that changes the target's content but leaves its message alone. There's also --fixup=reword:<sha> when the agent's commit message is the part that needs fixing.
5. Fold them in with rebase --autosquash
git rebase -i --autosquash main moves every fixup!, squash! and amend! commit next to its target and marks it for squashing. Set git config --global rebase.autoSquash true and git rebase does this by default for every interactive rebase.
6. Let git absorb find the commits for you
git absorb (brew install git-absorb) skips the SHA hunting. Stage your fixes, and it works out which earlier commits each change belongs to and writes the fixup! commits itself:
git add -u git absorb --and-rebase Leave off --and-rebase if you want to inspect the fixups with git log before squashing.
7. Pull a stray hunk out of an old commit in lazygit
Agents love slipping an unrelated formatting change into a feature commit. In lazygit, focus the commit's diff, select the hunk and press d, and lazygit rewrites that commit without it. i starts an interactive rebase where s, f and d squash, fixup and drop commits.
8. Run the tests on every commit, not just the last
A clean history where commit four doesn't build is worse than a messy one, because it breaks bisect later. --exec appends a command after each commit in the rebase and stops the moment one fails:
git rebase -i --exec "npm test" main 9. Compare before and after with range-diff
After a big cleanup, check that you only changed history and not behavior. git range-diff compares two versions of a patch series commit by commit, and your backup branch from tip 2 is the "before":
git range-diff main backup/agent-run HEAD 10. Turn on rerere before a long conflict session
Agent branches drift from main fast, and you end up rebasing the same branch more than once. With git config --global rerere.enabled true, git rerere records how you resolved a conflict and replays that resolution the next time the same conflict shows up.
11. Hand the regression hunt to bisect run
If something broke somewhere in a 20-commit agent branch, let git do the binary search. git bisect run treats exit code 0 as good, 1 to 127 (except 125) as bad, and 125 as "can't test this one, skip it":
git bisect start HEAD main git bisect run npm test git bisect reset 12. Check the quota before giving an agent the rebase
Handing a long conflict-resolution rebase or a bisect-and-fix session to Claude Code or Codex works well, until the session limit hits halfway and leaves you mid-rebase. TokenBar shows usage and reset times for Claude, Codex, Cursor, Gemini and Copilot in the macOS menu bar, so I can tell whether to hand the job over now or wait for the reset. It's built on Peter Steinberger's MIT-licensed CodexBar, credits Pixel Agents, and gives no extra quota. macOS 14+, $5/month (Basic) or $15 lifetime (Pro).
(Disclosure: TokenBar is made by the team behind this account.)
13. Push the cleaned branch with --force-with-lease
Rewritten history needs a force push, but plain --force will happily wipe out a commit a cloud agent or teammate pushed while you were cleaning up. git push --force-with-lease only overwrites the remote branch if it's still where you last saw it.
git push --force-with-lease What's your cleanup move for agent branches: squash everything into one commit, or keep the agent's history and fix it up commit by commit?


