Sub topic 2
Using Branches and Commits as Checkpoints
Assume you know basic Git commands, can run a test suite, and can read code in at least one language. A coding agent can make useful changes quickly, but you remain responsible for controlling the change and verifying its behavior.
Branches and commits as checkpoints
Start a refactoring on a separate branch. The branch isolates unfinished work from the stable branch. Commit after each verified step. A commit is a named checkpoint that lets you inspect or undo one change without discarding earlier work.
git switch -c refactor/extract-tax
pytest
# Ask the agent to make one small change.
pytest
git diff
git add pricing.py tests/test_pricing.py
git commit -m "Extract tax calculation"Keep commits focused. A commit that mixes a refactoring, formatting changes, and unrelated cleanup is harder to review and revert.
Pinning down current behavior with characterization tests
A characterization test records what the code does now. It is useful when the current behavior is poorly documented or may include accidental details. First run the existing code, then record its actual outputs.
# tests/test_pricing.py
from pricing import format_price
def test_current_price_format():
assert format_price(1234.5) == "$1,234.50"
assert format_price(0) == "$0.00"These tests do not necessarily describe ideal behavior. They create a safety net that can reveal behavior drift during restructuring. If the behavior should change, update the tests deliberately as a separate decision.
Asking for a plan before any edits
Give the agent the goal, files in scope, constraints, and verification commands. Ask for a short plan and wait before allowing edits. This exposes missing assumptions early.
A useful plan identifies the functions to change, tests to add or update, and the order of operations. Reject a plan that invents APIs, expands the file list without a reason, or lacks a way to verify behavior.
Working in small, reversible steps
Use one narrow step at a time:
- Add or confirm characterization tests.
- Make one structural change.
- Run tests, type checks, and the linter.
- Review the diff.
- Commit the verified result.
The smaller the step, the easier it is to locate a failure. If a later step is wrong, restore the last good checkpoint or revert that commit instead of repairing a large, tangled diff.
Exercise
Why should a refactoring branch use several small commits instead of one large commit?
Show answer
Small commits provide reviewable checkpoints and make it easier to identify or undo a faulty step.