Blog / git
Squash a coding agent's wip commits into one
An agent left a branch of wip commits. Squash them into one clean commit with plain git, keep a backup, fold fixups in, and check nothing changed.
On this page
- Before you start
- Squash the whole branch into one commit
- Check that nothing changed
- Keep a few commits, fold the fixups in
- Squash while you merge
- If it goes wrong
- When the branch is already pushed
- Ask the agent for better commits
- Frequently asked questions
- How do I squash all commits on a branch without interactive rebase?
- How do I run git rebase --autosquash without an editor?
- What is the difference between squash and merge --squash?
- How do I undo a squash in git?
- Try it with your team
Coding agents commit often, which is good while they work: every step can be undone. It is less good when the branch lands. You open the pull request and find this:
c4c545e fix test
d13a71e wip 6
374e2ad wip 5
eda0412 wip 4
1bcd720 wip 3
a1dfed3 wip 2
48f2571 wip 1Seven commits for one change, and none of the messages say what it does. Here is how to turn a branch like this into one commit with a real message, without an editor, an interactive menu or any risk to the work. We ran every command with git 2.43 on a scratch repository.
Before you start#
Make sure the agent is finished or stopped. Squashing while it still commits to the branch will mix its next commit into your history in an odd place, or it will commit on top of a branch you are rewriting.
Check how many commits the branch has on top of main:
git rev-list --count main..HEAD7Then make a backup branch. It costs nothing, and if anything below goes wrong you can go straight back:
git branch backup/agent-loginSquash the whole branch into one commit#
The quickest way doesn't use rebase at all. Move the branch back to where it left main, keep every change staged, and commit once:
git reset --soft $(git merge-base main HEAD)
git status --shortM app.js
A test.jsgit merge-base main HEAD is the commit the branch started from, so this still works if main has moved on since. --soft moves the branch pointer and nothing else: your files and the staged changes are exactly what the last agent commit had. Now write the message the change deserves:
git commit -m "Add login form validation"
git log --oneline main..HEAD5e105d4 Add login form validationCheck that nothing changed#
The squashed commit should hold exactly what the seven did. Compare it with the backup:
git diff --stat backup/agent-login HEADIt printed nothing, so the two trees are identical. Once you're happy, delete the backup with git branch -D backup/agent-login.
Keep a few commits, fold the fixups in#
Sometimes the branch has real steps worth keeping, plus small fixes for them. If each fix is committed with a message that starts with fixup! and the name of the commit it fixes, git can fold them in for you:
git commit -am "fixup! Add b"Then rebase with autosquash. Setting GIT_ accepts git's plan without opening an editor, so this works in a script or from an agent:
GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash main
git log --oneline main..HEADcb88cb3 Add d
2e546e5 Add bThe fix went into "Add b" and "Add d" stayed its own commit. git commit --fixup=<commit> writes the fixup! message for you. This is a good habit to ask of an agent: one commit per step it would describe to you, and fixups for anything that only repairs an earlier step.
Squash while you merge#
If you don't need the branch itself tidied, squash at merge time. From main:
git merge --squash agent/login
git commit -m "Add login form validation"Squash commit -- not updating HEADmain gets one commit with all the changes, and the agent's branch keeps its history, which you can then delete. Most hosting sites have the same thing as a button: GitHub's "Squash and merge", GitLab's "Squash commits". If your team always merges this way, you never need to rewrite an agent's branch at all.
If it goes wrong#
Every step above can be undone. The backup branch is the simplest way back:
git reset --hard backup/agent-loginWithout a backup, git reflog lists where the branch has been, newest first:
cb88cb3 HEAD@{0}: rebase (finish): returning to refs/heads/agent/two
cb88cb3 HEAD@{1}: rebase (pick): Add d
2e546e5 HEAD@{2}: rebase (fixup): Add bPick the entry from before the squash and git reset --hard to it. reset --hard throws away uncommitted changes in your folder, so commit or copy them first.
When the branch is already pushed#
Squashing rewrites the branch, so its commits get new hashes. If you've pushed it, you'll need git push --force-with-lease to update it. That's fine for a branch only you and your agent use. If a teammate or their agent has pulled it, they'll have to reset onto your version, and anything they committed on top can get lost: squash at merge time instead. If an agent has already force-pushed over someone's work, see a coding agent force-pushed over my work.
Ask the agent for better commits#
You can save yourself most of this with one rule in your project instructions: "Commit after each step with a message that says what changed, not 'wip'. Use git commit --fixup for fixes to an earlier step." Agents follow it well, and the history becomes useful while you review it: you can read the branch step by step.
Two more lines that pair well with it: "Never commit to main" (see stop a coding agent committing to main) and "Don't rewrite or force-push a branch someone else has pulled".
Frequently asked questions#
How do I squash all commits on a branch without interactive rebase?#
Run git reset --soft $(git merge-base main HEAD), then git commit with a new message. It keeps every change and replaces the branch's commits with one.
How do I run git rebase --autosquash without an editor?#
Set GIT_SEQUENCE_EDITOR=: before the command, as in GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash main. Git accepts its own plan and folds every fixup! commit in.
What is the difference between squash and merge --squash?#
Squashing rewrites the branch itself. git merge --squash leaves the branch as it is and puts all its changes on main as one new commit.
How do I undo a squash in git?#
Reset to the backup branch, or find the commit from before the squash in git reflog and run git reset --hard to it.