Code reviews with local checkouts and cleaner diffs
When it comes to code reviews, I’ve found that my most helpful feedback starts with checking the code out locally. The limited view on GitHub often falls short; it lacks full context and can’t match the navigability of a local environment. While you can expand the surrounding code on GitHub, it’s tedious and limited to nearby lines. I usually jump to function declarations, usage examples to trace calls, or related parts of the codebase to give meaningful feedback.
To fetch a pull request locally without cluttering my branches, I prefer a headless checkout using the following commands:
#!/bin/bash
# Fetch the latest changes from the remote repository.
git fetch origin
# Fetch the pull request branch by its number.
git fetch origin/pull/${PR_NUMBER}/head
I often discover insights I’d miss in a web view by checking out changes locally.
- If the update touches an unfamiliar area, I’m more likely to learn about it.
- Running tests, checking code coverage, or trying alternative solutions becomes effortless.
- Prototyping suggestions directly in the codebase is now a low-friction task. If I run into issues, I can fix them, provide much better concrete code suggestions, and help the author to polish their pull request.
Hide Whitespace in Diffs
Even with local checkouts, I start by reviewing the diff. When reviewing Go code, for example, hiding whitespace differences is especially important because Go’s auto-formatting can generate many irrelevant diff lines for minor changes. Consider the following struct definitions:
type User struct {
ID int
Name string
Email string
}
type Notification struct {
UserID string
Message string
}
type Subscription struct {
UserID int
Tier uint
}
Imagine a pull request adding a CreatedAt
field to all three structs. A standard git diff
with whitespace changes included would look like this:
diff --git a/model.go b/model.go
index 9c3bd7b..1abe805 100644
--- a/model.go
+++ b/model.go
@@ -1,17 +1,22 @@
package model
+import "time"
+
type User struct {
- ID int
- Name string
- Email string
+ ID int
+ Name string
+ Email string
+ CreatedAt time.Time
}
type Notification struct {
- UserID string
- Message string
+ UserID string
+ Message string
+ CreatedAt time.Time
}
type Subscription struct {
- UserID int
- Tier uint
+ UserID int
+ Tier uint
+ CreatedAt time.Time
}
Most of these changes are just indentation shifts due to gofmt
. This clutters the diff, making it harder to spot the actual code changes. GitHub offers a feature to hide whitespace, but it doesn’t always remember your preferences. Thankfully, git diff
can ignore whitespace changes using the --ignore-space-change
or -b
flag. This allows me to replicate GitHub’s whitespace hiding:
#!/bin/bash
# Show a diff with whitespace ignored between the pull request's common base
# commit and its latest commit (FETCH_HEAD).
git diff -b $(git merge-base origin/main..FETCH_HEAD)..FETCH_HEAD
With -b
, the diff now highlights only the relevant changes:
diff --git a/model.go b/model.go
index 9c3bd7b..1abe805 100644
--- a/model.go
+++ b/model.go
@@ -1,17 +1,22 @@
package model
+import "time"
+
type User struct {
ID int
Name string
Email string
+ CreatedAt time.Time
}
type Notification struct {
UserID string
Message string
+ CreatedAt time.Time
}
type Subscription struct {
UserID int
Tier uint
+ CreatedAt time.Time
}
No distractions and no formatting noise. It’s a minor tweak that makes code reviews faster and more focused.