Sub topic 11
Accepting part of a change and reverting the rest
This chapter assumes you know basic Git: branches, commits, staging, and diffs. It also assumes you have run a coding agent on a branch that started from a clean working tree, as set up in the earlier chapters on baselines and triage. You do not need to know advanced Git internals.
Agent changes are rarely all good or all bad. A typical run contains a correct rename, an unrelated edit to a second module, and a test that was quietly skipped. Accepting everything or discarding everything wastes the good work or lets the bad work in. Git lets you choose at the level of files and hunks. A hunk is one contiguous block of changed lines in a diff.
Why partial acceptance matters
Partial acceptance turns review into a series of decisions you make and record. The commit you end up with contains only changes you read and chose. That has three benefits:
- Good work is kept, so you do not pay for a full re-run.
- Out-of-scope edits never enter history, which keeps later
git blameandgit bisectresults meaningful. - Each commit describes one reviewed change, so a teammate can review it too.
The starting point is the clean baseline. Because the branch began with no uncommitted work, every difference from the starting commit belongs to the agent. That is what makes selective decisions safe.
The decision tools
Git offers several tools. They differ in what they touch and how easily you can undo them.
| Goal | Command | Touches | Reversible? |
|---|---|---|---|
| Stage only chosen hunks | `git add -p` | Index | Yes, with `git restore --staged` |
| Discard chosen hunks from the working tree | `git restore -p .` | Working tree | No, the edits are gone |
| Discard a whole tracked file's changes | `git restore path` | Working tree | No |
| Restore a file to another commit's version | `git restore --source=<commit> path` | Working tree | No |
| Remove new untracked files | `git clean -n`, then `git clean -f` | Working tree | No |
| Undo a commit you already made | `git revert <commit>` | History (adds a commit) | Yes |
The "No" entries matter. git restore and git clean delete work that was never committed, and Git cannot bring it back. Before any destructive step, save a checkpoint (see below).
A safe workflow
The workflow has four steps: checkpoint, stage what you accept, discard what you reject, and verify.
Step 1: Save a checkpoint
A checkpoint is a copy of the agent's complete output that you can return to. The cheapest way is a throwaway commit on the agent's branch:
git add -A
git commit -m "WIP: raw agent output (checkpoint)"
git tag checkpoint/agent-run-1The tag gives the checkpoint a stable name. With the raw output safely committed, you can discard freely. Everything you reject is still recoverable from the tag. If you prefer a clean history, you can squash or drop this commit later.
git add -A stages untracked files too. Those are often the agent's new files, which git diff does not show. Check git status --short first, so you do not commit a stray log file or a .env copy by accident.
Step 2: Review against the checkpoint
After the checkpoint commit, there is no diff against the working tree. Compare against the baseline commit you noted earlier instead. Use git reset in mixed mode to move the branch pointer back while keeping the files:
git reset --mixed <baseline-commit>
git status --shortThe files now sit in your working tree as unstaged changes, and the checkpoint tag still holds a copy. New files show as untracked. You can now stage selectively.
Step 3: Stage accepted hunks
git add -pGit shows each hunk and prompts for an action. The ones you will use most:
| Key | Action |
|---|---|
| `y` | Stage this hunk |
| `n` | Do not stage this hunk |
| `s` | Split the hunk into smaller hunks |
| `e` | Edit the hunk by hand |
| `q` | Quit, leaving remaining hunks undecided |
| `?` | Show help |
Use s when a hunk mixes a good change with a bad one on separate lines separated by unchanged lines. If the good and bad lines are adjacent, s cannot split them. Use e and delete the unwanted + lines, and change unwanted - lines into context by replacing the - with a space. Take care with e: a malformed edit makes Git reject the patch with an error such as "does not apply".
git add -p ignores untracked files by default. Add each new file you accept with git add path, or use git add -N path (intent to add) so its content appears in the hunk list.
Step 4: Review what you staged
Before committing, read exactly what will go into the commit:
git diff --staged
git diff --statThe first shows the staged changes. The second, with no flag, shows what is still unstaged: the changes you have not accepted. Both lists should match your intent. If something unexpected is staged, remove it with git restore --staged path.
Step 5: Test the staged state, not the working tree
Your working tree still contains the rejected hunks. A test run on it checks the combined state, which is not what you are about to commit. Hunks often depend on each other. Accepting a function call without accepting the function it calls gives a commit that does not run.
Use git stash to hide the unstaged changes while you test:
git stash push --keep-index --include-untracked
# run the project's tests, linter, and type checker here
git stash pop--keep-index leaves staged changes in the working tree and stashes everything else. --include-untracked also stashes new files you did not stage. Note that git stash pop can conflict if the tests modified files. Resolve those and re-check git status.
If the checks fail, a hunk you accepted depends on one you rejected, or the reverse. Return to staging and adjust.
Step 6: Commit and discard the rest
git commit -m "Rename config loader (reviewed agent changes)"
git restore -p .
git clean -ngit restore -p . walks through the remaining unstaged hunks and lets you discard them one at a time. git clean -n is a dry run that lists untracked files it would delete. Read that list. Run git clean -f only when every listed file is one you want gone. Your checkpoint tag still has all of it.
Worked example
You asked an agent to rename load_config to read_settings in src/config/loader.py and update its callers. After the run, git status --short shows:
M src/config/loader.py
M src/app.py
M src/billing/invoice.py
M tests/test_loader.py
?? scratch_notes.mdReviewing hunk by hunk gives these decisions:
loader.py: the rename is correct. Accept.app.py: the caller is updated. Accept.invoice.py: a reformatted function unrelated to the task. Reject.test_loader.py: one hunk updates the function name, and another adds@pytest.mark.skiptotest_missing_file. Accept the first, reject the second.scratch_notes.md: the agent's working notes. Delete.
The commands:
git add -A && git commit -m "WIP: raw agent output (checkpoint)"
git tag checkpoint/agent-run-1
git reset --mixed HEAD~1
git add src/config/loader.py src/app.py
git add -p tests/test_loader.py # answer y for the rename hunk, n for the skip hunk
git diff --staged
git stash push --keep-index --include-untracked
python -m pytest
git stash pop
git commit -m "Rename load_config to read_settings (reviewed agent changes)"
git restore src/billing/invoice.py tests/test_loader.py
rm scratch_notes.mdHere HEAD~1 is the baseline because the checkpoint was the only commit the agent's branch added. If you made more commits, use the baseline hash you noted at the start.
If test_missing_file now fails, the code is wrong and the test is right. That is the case for feedback to the agent, not for skipping the test.
Edge cases and common errors
- Renames look like a delete plus an add. Git detects renames by similarity when you run
git diff -Morgit status. Aftergit reset --mixed, a renamed file may appear as a deleted tracked file and a new untracked one. Stage both sides (git add -A path_old path_new) so Git records one rename. - Binary and generated files cannot be split.
git add -pcan only accept or reject them whole. Lock files and snapshots are the usual cases. Regenerate them with the project's tool after you finalize the source changes, instead of accepting the agent's version. - Deleted files.
git restore pathbrings a deleted file back from the index. To restore from the baseline, usegit restore --source=<baseline-commit> path. - Dependent hunks. A rejected hunk can be required by an accepted one. The failing test or type check from step 5 is how you find out. Do not rely on reading alone.
- Whitespace noise. If the agent reformatted a file,
git diff -whelps you judge. Staging, however, still includes the whitespace hunks. Reject them withn, or the commit carries reformatting that hides the real change. - Already committed bad work. If the agent committed on its own, use
git revert <commit>for shared history. On a private branch,git reset --mixed <baseline>followed by selective staging gives a cleaner result.
Deciding between partial acceptance and a re-run
Partial acceptance fits when the good hunks are independent and the bad ones are clearly separable. It stops being worth the effort when you reject most hunks, when rejected and accepted code are tangled together, or when the design itself is wrong. In those cases, restore the baseline, narrow the task, and run again. The chapter on escalation covers that decision.
Exercise
After a run, git diff --staged shows the rename you accepted, but your tests fail. The working tree also contains hunks you rejected. What is the most likely cause, and how do you check it?
Show answer
The tests ran against the working tree, or an accepted hunk depends on a rejected one. Run git stash push --keep-index --include-untracked to hide everything unstaged, then run the tests on only the staged state. If they fail there, an accepted hunk needs code from a rejected hunk, so return to git add -p and adjust. Restore your stashed changes afterwards with git stash pop.
Exercise
A single hunk in tests/test_loader.py contains an updated function name on one line and @pytest.mark.skip on a line two lines below, with unchanged lines between them. How do you accept only the rename in git add -p, and what do you do if the two lines are adjacent?
Show answer
Press s to split the hunk, then answer y for the rename hunk and n for the skip hunk. If the lines are adjacent, s cannot split them. Press e to edit the hunk and delete the + line that adds the skip marker.