Tell Me a Good Story With Your Pull Request

tl;dr;

  • Structure the pull request in three commit history groups:
    • prepare the codebase for implementation;
    • use atomic commits for behavior changes and pair it with tests;
    • clean up the code for future self, and shape the commit history before review.
  • Keep commit history focused, passing, and useful to reviewers and git bisect.

A pull request can contain perfectly good code and still be painful to review. Open a large, tangled diff and the reviewer is dropped into the final scene with no idea how the story got there.

Git gives us a better way to present the work: Git commits.

If your application is the silver screen and your pull request is the script, strive to write a script worthy of a blockbuster film. Each commit is a scene, and a reviewer should be able to follow those scenes in order and understand how one decision led to the next. The title and description introduce the story, but the commit history shows how it unfolded.

An empty Art Deco cinema illuminated by a glowing white screen

An original long-exposure theater image generated with Nano Banana.

Your commits are the scenes

Commit organization is an overlooked communication tool. A carefully structured history separates the preparation from the behaviour change and the behaviour change from the final cleanup. Instead of asking the reviewer to untangle everything from one finished diff, it gives them a path through your reasoning.

This matters after the review, too. When a production issue appears months later, a useful history helps your future self understand why the code changed, revert one decision, or use tools such as git bisect to locate the commit that introduced a problem. Even if your team squash-merges pull requests, the commit sequence still makes the review itself easier.

A good film has a beginning, a middle, and an end. I organize a feature pull request in the same way:

  1. Set the stage with preparatory refactoring.
  2. Implement the behaviour and prove it with tests.
  3. Clean up and leave the code ready for the next change.

This guide focuses on adding a feature. A bug fix follows a slightly different plot, but it still needs to tell a clear story.

Planning the script

In English you may say, “Measure twice, cut once.” A Ukrainian proverb raises the standard: measure seven times, cut once.

It is tempting to jump straight into editing code, especially when the requested change sounds small. Don’t touch the keyboard yet. Make sure to understand the feature requirements and the parts of the code that relate to them. Read the code, don’t edit it.

  • What behaviour needs to change?
  • What behaviour must stay the same?
  • Which part is redundant or over-engineered?
  • Which part of the application owns that behaviour today?
  • What can be removed once the new implementation exists?
  • How can I prove that the change works?

The plan does not need to be a document. I usually note down filenames and module names, or sketch a rough outline of the scenes the pull request will need. Its purpose is to give the work a boundary before the codebase offers you twenty unrelated problems to fix.

The three-act structure of a pull request

The pull request title names the film, and the description is its poster: it sets expectations while the commits tell the story scene by scene.

I like to divide my story (a pull request) into three phases:

  1. Setting the Stage: Preparatory Refactoring
  2. Implementation
  3. Clean up

These are phases, not necessarily three commits. Setting the stage may take two commits, implementation may take five, and a tiny change may not need a separate clean-up commit at all. What matters is that the sequence is easy to follow.

For example, a pull request might read like this:

  1. Add tests for the current behaviour.
  2. Extract the decision-making code into one place.
  3. Add the new behaviour and its tests.
  4. Remove the old branch that is no longer reachable.
  5. Rename a few things now that their purpose is clearer.

Even before opening the diff, a reviewer can see where the change is going. Even if your team squash-merges every pull request, this structure still makes the review easier while the work is in progress.

Setting the Stage: Preparatory Refactoring

When you enter an unfamiliar part of a legacy codebase, the route to the feature is rarely a straight road. There may be duplicated conditions, missing tests, old code paths, or a method doing three jobs at once.

The first instinct is often to push the feature through all of that and promise to clean it up later. “Later” is where temporary code goes to become permanent and technical debt piles up.

Preparatory refactoring is the work required to make the implementation unsurprising. You are setting the stage, not performing the main act yet.

Tests, tests, tests

Tests are the most useful tool for setting the stage. Before changing a difficult piece of code, cover the behaviour you need to preserve. These are sometimes called characterization tests: they describe what the application does today, including behaviour that may not be obvious from reading the code.

Whether these are unit or integration tests matters less than whether they protect the boundary you are about to change. If the feature passes through a controller, a service, and a database query, one good integration test may give you more confidence than a dozen isolated mocks.

Keeping those tests in a separate commit is helpful. The reviewer can confirm that the commit only records existing behaviour. The next refactoring commit should then leave the tests green without changing what the user sees.

If the area already has strong coverage, great – this phase may be very small. If it has none, adding coverage is not ceremony. It is the safety rope that lets you untangle the code with confidence.

Remove old code

Every line of code carries a maintenance cost. It can break, mislead a reader, require a test, or become one more place that must change during the next feature.

Part of preparatory refactoring may be deleting code that is already unused: an obsolete branch, a duplicated helper, a compatibility path for a version nobody runs anymore. Deleting code feels good for a reason. A diff that is 70% red and 30% green can be a real improvement.

But deletion needs evidence. A method that appears unused may be called through reflection, a background job, a template, or an external integration. Search for callers, check the tests, and understand why the code existed before removing it.

When the deletion is independent of the feature, give it its own commit. That lets the reviewer answer one question at a time: was this code safe to remove? They should not have to separate that answer from the new behaviour.

Do not clean the whole neighbourhood

Existing code always provides opportunities for improvement. Once you start refactoring, it is easy to rename one thing, then another, and keep digging deeper into the rabbit hole. Keep the refactoring small and safe, then stop. If a cleanup does not make the feature safer or easier to understand, it probably belongs in another pull request – and it is okay to split it into a separate task altogether.

This is not only about keeping the diff small. A focused preparatory phase makes your claim easier to believe: these changes were necessary for the implementation that follows.

Implementation

The main set of changes should look almost boring. The tests are in place, the confusing code has been separated, and there is a clear home for the new behaviour.

Each behaviour change should arrive with a test that demonstrates it. Treat the behaviour and its proof as one atomic change: the implementation and the test belong in the same final commit. A reviewer should not have to inspect one commit containing an unproven behaviour and another containing the test that finally explains it.

Following the same principle as continuous integration, I try to keep the branch working after every commit: the application builds, the relevant tests pass, and the next commit begins from a stable point. If a commit leaves the application broken until the following commit, the boundary between those commits is probably in the wrong place. Keeping each commit green also makes decisions easier to reverse, reorder, and debug later.

Small, working commits make review easier because they preserve cause and effect. A reviewer can see the new rule introduced in one commit instead of discovering it among file moves, renamed methods, and deleted code.

Commit messages should explain the intention, not narrate the diff. Add fallback for accounts without a billing profile is useful. Update billing_service.rb only repeats information Git already knows.

Red, green, refactor

Red, yellow, and green traffic lights

Traffic lights photographed by Pixabay. Photo on Pexels.

A final commit should be green, but getting there still involves seeing the test fail. I use the red-green-refactor loop from test-driven development:

  1. Write one focused test for the behaviour.
  2. Run it and confirm it fails for the expected reason.
  3. Write the minimum code needed to make it pass.
  4. Run it again and confirm it is green.
  5. Refactor while keeping the test green.

The red state belongs in your working tree, not necessarily in the final pull request history. If the test passes before the implementation changes, it may not be testing the new behaviour at all. If you commit the red test separately, the branch is temporarily broken and the reviewer has to combine commits to understand one change. Once the test and implementation are green, commit them together.

A bug fix starts with proof

A bug fix tells a shorter version of the same story. First reproduce the bug, then write a regression test that captures it. Run the test and confirm it fails because of the bug, and only then write the fix and make the test pass.

The failing test proves that the problem existed. The passing test proves that the change fixed it, and keeping both in the same commit prevents the bug from quietly returning later. Keeping them together also signals to the reviewer that the behaviour change is intentional and covered by a regression test.

Red-green-refactor is a tight loop while building behaviour. Setting the stage, implementation, and clean up describe the shape of the pull request as a whole. These work together: tests protect the preparatory refactoring, guide the implementation, and give you confidence during the final cleanup.

Clean up

Once the feature works, read the entire diff from the reviewer’s point of view. The code may be correct while still carrying the history of how you discovered the solution.

Look for things such as:

  • temporary names that no longer describe the final idea
  • debug output and commented-out code
  • conditionals made redundant by the new behaviour
  • comments that describe the old implementation
  • test setup copied during experimentation
  • TODOs that were actually completed by the feature

Clean up for the next developer, not for the satisfaction of making the code clever. The goal is to make the finished idea easier to understand.

If the cleanup is behaviour-preserving refactoring, keep it separate from the implementation. A reviewer can then move quickly through it, knowing the feature itself was completed in the previous commits.

Edit the final cut

A person cutting a film strip with scissors

Film strip being cut by Tima Miroshnichenko. Photo on Pexels.

The history created while solving a problem is rarely the history another person should review. It contains false starts, forgotten files, and small corrections that belong to earlier commits. Before opening the pull request, edit that raw footage into the story you intended to tell.

Do not reach for git add -A by default. Review the diff and stage the relevant files explicitly, or use git add -p to select individual hunks. This keeps unrelated edits, generated files, and unfinished experiments out of the commit.

If a correction belongs to the latest commit, stage it and use git commit --amend. If it belongs to an earlier commit, create a fixup commit and autosquash it, or stop at that commit with git rebase -i, amend it, and continue the rebase. Interactive rebase also lets you remove abandoned commits, combine reworked attempts, reorder dependent steps, and improve commit messages.

Rewrite only history that is still yours to rewrite. Once other developers are building on the same branch, changing its commits creates a different kind of story – usually one involving conflict.

A history you can debug

A photographer examining film negatives with a magnifying glass

Photographer examining film negatives by Annushka Ahuja. Photo on Pexels.

A clean commit history remains useful after the pull request is merged. When a regression appears and you know one old commit was good while the current version is bad, git bisect can perform a binary search through the commits between them. At each step, Git checks out a commit and asks whether it is good or bad. If every commit builds, passes its tests, and contains one coherent change, the answer is usually easy.

Git can narrow hundreds of commits to the likely culprit in only a handful of checks. If the failure can be reproduced with a command, git bisect run can automate the search.

Broken intermediate commits make this much harder. So do commits that mix several unrelated behaviours or separate tests from their implementation. The ability to use git bisect is one more reason to treat every commit as a working checkpoint rather than a temporary save button.

Finally, make the pull request description a synopsis of the finished story. Explain the problem, summarize the approach, mention any tradeoffs, and say how you tested it. Add screenshots, migration notes, or rollout steps when they are relevant. The description should provide context; it should not compensate for a diff that has no structure. There are many established practices for writing pull request descriptions, so follow the one that best suits your team.

Make the review easy

A reviewer should be able to move through your pull request in order and understand why each commit exists. First you establish the current behaviour. Then you clear a path. Then you add the feature. Finally, you leave the area cleaner than you found it.

Not every change needs five commits or a dramatic story arc. Small changes should stay small. But when the work is complicated, organizing it this way turns the commit history from a record of typing into a record of reasoning.

That is the story I want a pull request to tell. Exceptions are fine, but generally I find that this structure helps me in the long run.