Blog / git
Split a coding agent's big commit into smaller ones
An agent put a dependency, a feature and a reformat in one commit. Split it into reviewable commits with plain git, even a commit mid-branch.
On this page
- Before you start
- Split the last commit
- When one file holds two changes
- Split a commit in the middle of a branch
- Check every new commit still works
- If it goes wrong
- Once it's pushed
- Ask for small commits up front
- Where Joxo fits
- Frequently asked questions
- How do I split the last git commit into two?
- How do I split a commit that isn't the latest one?
- How do I commit only some changes in a file?
- How do I check that splitting a commit didn't change the code?
- Try it with your team
You asked for a login form. The agent delivered it in one commit called "Lots of changes":
git show --stat --format=%s HEADLots of changes
app.js | 1 +
package.json | 1 +
src/login.js | 1 +
test/login.test.js | 1 +
4 files changed, 4 insertions(+)A new dependency, the feature with its test, and a reformat of app.js it did on the way. Reviewing that as one change is hard, and if the reformat breaks something you can't revert it alone. Here is how to split it into one commit per change. We ran every command with git 2.43 on a scratch repository.
Before you start#
Make sure the agent has stopped, and make a backup branch so you can always get back:
git branch backup/bigSplit the last commit#
Undo the commit but keep every change in your folder:
git reset HEAD~1
git status --short M app.js
?? package.json
?? src/
?? test/Now stage and commit each part on its own:
git add package.json && git commit -m "Add login dependency"
git add src/login.js test/login.test.js && git commit -m "Add login form"
git add app.js && git commit -m "Format app.js"
git log --oneline main..HEAD02e3791 Format app.js
b494c18 Add login form
2ca64c1 Add login dependencyCheck that the three commits add up to exactly the one you started with:
git diff --stat backup/big HEADIt printed nothing, so the code is the same as before. Delete the backup once you're happy with git branch -D backup/.
When one file holds two changes#
If the agent reformatted app.js and also changed its logic, staging the file takes both. git add -p app.js shows each change in the file and asks whether to stage it: answer y for the hunks that belong in this commit, n for the rest, and s to split a hunk that is still too big. It is interactive, so it is a step for you rather than for the agent. Commit, then run it again for the next part.
Split a commit in the middle of a branch#
If the agent has committed again since, the big commit isn't the last one. Use an interactive rebase and stop at it. Here the branch has the big commit and a later fix:
761be54 Fix typo
cbca0ba Lots of changesMake the backup first (git branch -f backup/), then mark the big commit as edit. Setting GIT_ changes the plan without opening an editor: this one marks the first line, which is the oldest commit after main:
GIT_SEQUENCE_EDITOR="sed -i '1s/^pick/edit/'" git rebase -i mainOn macOS, sed -i needs an argument, so write sed -i '' there. Git stops at the big commit. Split it exactly as above, then let the rebase carry on:
git reset HEAD~1
git add package.json && git commit -m "Add login dependency"
git add -A && git commit -m "Add login form and format"
git rebase --continue
git log --oneline main..aa3c9ff Fix typo
d7238e5 Add login form and format
bc08e12 Add login dependencyThe later commit is replayed on top. To stop somewhere else, run git rebase -i main without the variable, change pick to edit on the right line in the editor and save. Compare with git diff --stat backup/ once more: it should print nothing.
Check every new commit still works#
Splitting proves the end result is unchanged, but not that each commit builds on its own. If "Add login form" needs the dependency from the commit before it, that's fine; if it needs the reformat that now comes after it, the middle commit is broken, and git bisect will trip over it later. Run your tests on each commit in turn:
git rebase --exec "npm test" mainGit replays the branch and runs the command after every commit, stopping at the first one where it fails. Fix that commit, git commit --amend, and run git rebase --continue.
If it goes wrong#
In the middle of a rebase, git rebase --abort puts everything back as it was before you started. After it, the backup branch does the same: git reset --hard backup/. Without a backup, git reflog lists where the branch has been.
Once it's pushed#
Splitting rewrites the branch, so the commits get new hashes. On a branch only you and your agent use, push with git push --force-with-lease. If a teammate or their agent has pulled it, don't rewrite it: review it as it is, or ask for the next change in smaller commits.
Ask for small commits up front#
Splitting is easier to avoid than to do. Add to your project instructions: "One commit per change: dependencies, refactors and formatting each in their own commit, never mixed with a feature. Don't reformat files you aren't otherwise changing." Agents follow this well, and a branch of small, named commits is quick to review. If you'd rather land it as one commit after all, squash it at merge time.
Where Joxo fits#
When several people's agents work on one project, reviews are where the time goes. Joxo gives each agent a task on a shared board and a note when it hands off, so you know what a branch is meant to change before you open it. Setup is one line pasted into your agent.
Frequently asked questions#
How do I split the last git commit into two?#
Run git reset HEAD~1 to undo the commit and keep the changes, then git add and git commit each part separately.
How do I split a commit that isn't the latest one?#
Run git rebase -i main, change pick to edit on that commit, then git reset HEAD~1, commit the parts, and run git rebase --continue.
How do I commit only some changes in a file?#
Run git add -p <file> and answer y for each change you want staged and n for the rest, then commit.
How do I check that splitting a commit didn't change the code?#
Make a backup branch first, then run git diff --stat backup HEAD at the end. No output means the code is identical.