Blog / git
Fix an older commit on a coding agent's branch
Spotted a mistake three commits back on an agent's branch? Make a fixup commit and fold it in with autosquash, or reword the message, without an editor.
On this page
- Find the commit
- Make a fixup commit
- Fold it in
- Check that only history changed
- Undo the rebase
- Why not just amend?
- Change an older commit's message
- The branch was already pushed
- It stopped on a conflict
- Ask agents to do it the same way
- Where Joxo fits
- Frequently asked questions
- How do I change an older commit in git?
- How do I run autosquash without opening an editor?
- Why did git rebase --autosquash do nothing?
- How do I edit the message of an older commit?
- Do I need to force-push after a fixup rebase?
- How do I undo a rebase that went wrong?
- Why not use git commit --amend?
- How do I check a fixup rebase didn't change the code?
- Try it with your team
You're reviewing an agent's branch before it's merged. The first commit left a debug line in, and two commits came after it. You could add a "remove debug line" commit at the end, or fix the commit where the mistake is, so each commit stays clean. We ran every command with git 2.43 on a scratch repository.
Find the commit#
git log --oneline main..agent/login8876410 Add login tests
24c2848 Add API client
8b703b2 Add login formThe debug line is in 8b703b2.
Make a fixup commit#
Edit the file, stage it, and commit it marked as a fix for that commit:
git add login.js
git commit --fixup=8b703b2[agent/login cd85569] fixup! Add login form
1 file changed, 1 deletion(-)Fold it in#
GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash mainSuccessfully rebased and updated refs/heads/agent/login.git log --oneline main..agent/logina0aeac1 Add login tests
918487d Add API client
50cacd7 Add login form--autosquash moves each fixup! commit next to the one it fixes and merges them. GIT_ accepts the plan without opening an editor, which matters if an agent runs this. On git 2.43, git rebase --autosquash main without -i does nothing to the fixups; from git 2.44 it works without -i.
All three commits get new hashes, because each one now sits on a changed parent.
Check that only history changed#
The rebase moves your fix into an earlier commit; the code at the tip of the branch should be exactly what it was with the fixup commit on top. Note the hash before you rebase, then compare:
git rev-parse --short HEADcd33ed0GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash main
git diff --stat cd33ed0 HEADNo output means the code is the same and only the commits were rearranged. If something shows up, a later commit's change was lost or doubled, and you can go back.
Undo the rebase#
Right after a rebase, ORIG_ points at the branch as it was before:
git reset --hard ORIG_HEAD
git log --oneline -1cd33ed0 fixup! Add login formThe fixup commit is back on top, ready to try again. This also throws away uncommitted edits, so commit or save them first. If you ran other commands since, git reflog lists every place the branch has been.
Why not just amend?#
git commit --amend only changes the newest commit. The mistake here is in the oldest of three, so amending would put the fix in "Add login tests", which is the wrong place. Fixup commits name their target, so you can make several, for different commits, and fold them all in with one rebase.
Change an older commit's message#
Use --fixup=reword: and write the new message:
git commit --fixup=reword:918487dGit opens an editor with amend! Add API client on the first line. Leave that line, and below it write the full new message:
amend! Add API client
Add API client for v1Then run the same rebase:
GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash main
git log --oneline main..agent/login5c79c88 Add login tests
d555629 Add API client for v1
50cacd7 Add login formThe branch was already pushed#
The rebase rewrote commits, so a plain push is refused:
! [rejected] agent/login -> agent/login (non-fast-forward)Push with a lease, which refuses if someone else pushed in the meantime:
git push --force-with-leaseOnly do this on a branch nobody else builds on. If a teammate or their agent may have pulled it, say so first; see a coding agent force-pushed over my work for why.
It stopped on a conflict#
A later commit may change the same lines. Fix the file, then git add it and git rebase --continue. To go back to how the branch was:
git rebase --abortAsk agents to do it the same way#
Add to your project instructions: "When you fix a mistake in an earlier commit on this branch, use git commit --fixup=<hash>, and run GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash main before asking for review." If you'd rather have one commit in the end, see squash a coding agent's commits.
Where Joxo fits#
When your agent and a teammate's agent review each other's branches, small fixes come back all the time. In Joxo the agents can hand a branch back with the commit to fix, and you approve the force-push from your phone. Setup is one line pasted into your agent.
Frequently asked questions#
How do I change an older commit in git?#
Commit the fix with git commit --fixup=<hash>, then run git rebase -i --autosquash <base>. The fix is merged into the commit it names.
How do I run autosquash without opening an editor?#
Set GIT_SEQUENCE_EDITOR=: for the command: GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash main.
Why did git rebase --autosquash do nothing?#
Before git 2.44, --autosquash only works with -i. Add -i, or update git.
How do I edit the message of an older commit?#
Run git commit --fixup=reword:<hash>, write the new message under the amend! line, then rebase with --autosquash.
Do I need to force-push after a fixup rebase?#
Yes, if the branch was pushed. Use git push --force-with-lease, and only on a branch nobody else has built on.
How do I undo a rebase that went wrong?#
Right after it, run git reset --hard ORIG_HEAD to put the branch back as it was. If you've run other commands since, find the old position with git reflog and reset to that.
Why not use git commit --amend?#
--amend only changes the newest commit. To change one further back, make a fixup commit that names it and fold it in with git rebase -i --autosquash.
How do I check a fixup rebase didn't change the code?#
Note the hash with git rev-parse HEAD before the rebase, then run git diff --stat <old hash> HEAD afterwards. No output means only the commits changed, not the code.