Sub topic 11
Accepting Part of a Change and Reverting the Rest
This chapter assumes you know basic Git: staging, commits, and branches. It also assumes you have already triaged an agent's diff and decided that some of it is good and some is not.
Why partial acceptance matters
An agent's run rarely lands as all good or all bad. A typical result has a correct change to the target file, a stray edit in an unrelated module, and a test that was quietly skipped. Accepting everything keeps the defects. Rejecting everything throws away good work and costs you a re-run.
Git lets you commit only the hunks you chose. A hunk is one contiguous block of changes in a diff. The commit then records your decision, not the agent's output.
Start from the clean baseline
Selective revert only works safely if you began on a dedicated branch with a clean working tree. Then every change in git status is the agent's, and you can discard any of it without losing your own work. If you skipped that step, stash or commit your own edits before you go on.
git status --short
git diff --statStage the hunks you accept
git add -p walks through each hunk and asks what to do with it.
git add -p
git diff --staged
git commit -m "Rename config loader (reviewed agent changes)"The common answers are listed below.
| Key | Meaning |
|---|---|
| `y` | Stage this hunk |
| `n` | Do not stage this hunk |
| `s` | Split the hunk into smaller hunks |
| `e` | Edit the hunk by hand before staging |
| `q` | Quit, leaving the remaining hunks unstaged |
Use s when a hunk mixes a good edit with a bad one. Use e when a hunk is almost right and you want to fix one line yourself.
Always run git diff --staged before committing. It shows exactly what the commit will contain.
Note that git add -p ignores new, untracked files. Add those by name with git add path/to/file after you have read them.
Discard the rest
Once the good hunks are committed, what remains in the working tree is what you rejected. Discard it interactively so you can still look at each hunk once more.
git restore -p .For a whole file the agent should not have touched, restore it directly. For a new file you do not want, delete it.
git restore src/billing/invoice.py
git clean -n # dry run: lists untracked files that would be removed
git clean -f path/to/unwanted_file.pyCheck the result before moving on
Partial acceptance can leave the code in a state that neither you nor the agent has run. Hunks depend on each other. For example, you might keep a renamed function but revert the hunk that updates its caller. Run your checks on the committed state.
git status --short # should show nothing left over
pytestIf the checks fail because of a missing piece, the dependency was split across hunks you treated separately. Either stage the missing hunk or fix it yourself.
Exercise
The agent's diff contains a correct edit to src/config/loader.py and, in the same file, one hunk that adds # type: ignore to silence an error. You want to keep the first edit and reject the second. Which commands do you use, and what do you check before committing?
Show answer
Run git add -p, answer y for the correct hunk and n for the # type: ignore hunk (use s first if they are in one hunk). Run git diff --staged to confirm only the correct edit is staged, then commit. Afterwards use git restore -p . to discard the remaining hunk, and run the type checker and tests on the committed state.