IntermediateLesson 4 of 9

Commits & Pull Requests

Write meaningful commits, create high-quality PRs, and run effective code reviews that improve reliability and team velocity.

Simple Explanation (ELI5)

A commit is one clear step in your work. A pull request is asking the team, “Please review my steps before we add them to main.” Good commits and PRs make collaboration easy and safe.

Technical Explanation

Commits should be atomic and logically scoped. PRs aggregate branch commits for review, CI validation, policy checks, and merge. Review quality depends on small diff size, clear context, reproducible test evidence, and explicit risk notes.

Commit Quality Rules

Good PracticeReason
Small atomic commitsEasier review and rollback
Conventional message formatReadable history and changelog automation
One concern per commitIsolates defects and improves traceability
No generated noiseKeeps diffs meaningful
Link issue/ticketBusiness context and audit trail

Visual: PR Lifecycle

Feature Branch
push →
Open PR
checks →
Code Review
approve →
Merge

Hands-on Commands

bash
# Stage selected hunks interactively
git add -p

# Create descriptive commit
git commit -m "feat(auth): add JWT refresh token endpoint"

# Amend last commit message or staged changes
git commit --amend

# Squash last 3 commits interactively
git rebase -i HEAD~3

# Push updated branch
git push origin feature/auth-refresh

# If history rewritten, force push safely
git push --force-with-lease

PR Checklist

Debugging Scenarios

Real-world Use Case

A fintech team reduced escaped defects by 35% after introducing a mandatory PR template with “risk, tests, rollback” sections. Reviewers caught migration issues before merge because PRs explicitly required backward compatibility notes.

Interview Questions

Beginner

What makes a good commit message?

Clear scope and intent, e.g., fix(api): handle null payment token.

Why use pull requests?

To review code, run checks, and maintain quality before merge.

What is code review?

Peer evaluation of code for correctness, readability, and maintainability.

What does squash merge do?

Combines all PR commits into one commit on target branch.

Why link PR to issue?

Creates traceability between business requirement and code change.

Intermediate

Amend vs new commit?

Amend for correcting latest commit before share; new commit for transparent history on shared branch.

When to use force-with-lease?

After rebasing your own branch; safer than force because it checks remote state.

How keep PRs reviewable?

Limit diff size, isolate concerns, include tests and clear PR description.

Merge commit vs rebase merge?

Merge commit preserves branch context; rebase merge keeps linear history.

How automate PR quality gates?

Use branch protections requiring CI checks, reviews, and status contexts.

Scenario-based

Reviewer requests major changes late. What do you do?

Address feedback in focused commits, update PR summary, request re-review.

You committed secrets in PR history.

Rotate secrets immediately, rewrite branch history, and run secret scan before re-open.

CI passes locally but fails in PR.

Compare environment differences, pin versions, and reproduce in containerized CI-like setup.

PR blocked by conflict with main.

Fetch latest main, rebase/merge, resolve conflicts, rerun tests, push update.

Hotfix needed while big PR in review.

Create separate hotfix branch from main, merge quickly, then rebase big PR.

Summary

Strong commit hygiene and disciplined PR review are core to healthy engineering culture. Keep changes small, context-rich, and fully validated before merge.