Self Code Review with Git Add Patch

Code reviews reduce logical errors, call out typos, and prevents accidentally committing more code than intended (1). They're essential forall sized teamspushing to the same code base whilemaintaining sanity.

Should a developer find themselves discovering any accidental "oops!" in their pull request it's a good indicator of a flaw in workflow.Introducing the interactive self code review:

git add --interactive

Interactive adding in git offers a choose-your-own-adventure style "What Now>" series of options.

The Useful Bit:Git Patch

Today let's look at thepatchoption. We can skip to the patch option in the future with this handy shortcut:

Example pretends we have file.ext and have added a line that defines a version...

git add -p

diff --git a/file.ext b/file.ext
index sha1..sha2 sha3
-- a/file.ext
++ b/file.ext
{
  "name": "example json",
  +"version": "1.0.0",
  "private": true,
Stage this hunk [y,n,q,a,d,/,e,?]?

Looks like a huge chunk of stuff! Broken down, the response describes what file was modified followed by a chunk of color coated git diff.

Now What? Staging Hunks of Code

Many options are provided in the "Stage this hunk?" prompt following a git add patch.

Pressing?in response to the prompt explains each valid response. These are some essentials:

  • y- stage this hunk
  • n- donotstage this hunk
  • q- quit

You'll find that by going through this process you can read every line you'd like to add to a commit and make better choices about them.

Splittings

These "magical" feeling chunks aren't always smart enough. Sometimes there's a need to split (with thesresponse) a chunk into smaller chunks. This comes up more often than you'd think if you're developing empathetically.

aAdd ordDon't Add Entire File

Anaresponse will stage the current hunk and all the following within the current file automatically. This isnotrecommended because you're opting to skip parts of your personal code review.

However,dis a nice way to skip adding anything from an entire file and can save a lot of time.

Manually Editing

Typos or "oops!" can be quickly corrected with theeresponse. This will open just the chunk for quick adjustments including line adding and removal.

Summary

Git patch has become a core part of my workflow toensure qualityand boostpersonal confidence in the code I ship. Give it a try today!