Skip to content
Home DevOps GitHub Pull Requests and Code Review Explained for Beginners

GitHub Pull Requests and Code Review Explained for Beginners

Where developers are forged. · Structured learning · Free forever.
📍 Part of: Git → Topic 6 of 19
GitHub Pull Requests and code review explained from scratch — what they are, how to open one, review code like a pro, and avoid the mistakes that trip up beginners.
🧑‍💻 Beginner-friendly — no prior DevOps experience needed
In this tutorial, you'll learn
GitHub Pull Requests and code review explained from scratch — what they are, how to open one, review code like a pro, and avoid the mistakes that trip up beginners.
  • A Pull Request is a formal request to merge one branch into another — it's the review checkpoint that stands between your code and production, not just a Git feature.
  • A great PR description answers three questions: What changed, why it changed, and how to test it — skipping this is the single biggest thing that slows down code reviews.
  • Approve means 'I'd be comfortable if this shipped right now' — never click Approve to be polite; a hollow approval that lets a bug through is worse than a delayed review.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
Quick Answer
  • Branch: your isolated copy of the code where you develop a feature or fix
  • PR description: answers three questions — what changed, why it changed, how to test it
  • Code review: at least one teammate reads your diff line by line before approving
  • Merge strategy: how your commits land on main — merge commit, squash, or rebase
  • Branch protection: rules that enforce reviews, tests, and prevent direct pushes to main
🚨 START HERE
Pull Request Triage Cheat Sheet
Fast resolution paths for the most common PR failures. Use when a PR is blocking you or your team right now.
🟡Merge conflict on PR — GitHub says 'cannot merge automatically'
Immediate ActionResolve locally, not in GitHub web editor
Commands
git checkout your-branch && git fetch origin && git merge origin/main
git diff --name-only --diff-filter=U (lists conflicted files)
Fix NowOpen conflicted files, remove conflict markers, keep correct version, git add + git commit + git push
🟡CI pipeline fails on PR — tests pass locally
Immediate ActionRead the CI failure log first, do not re-run hoping it passes
Commands
cat .github/workflows/*.yml (find what CI actually runs)
Run CI commands locally to reproduce: npm test / ./mvnw test / pytest
Fix NowCheck for: missing env vars, OS-specific paths, flaky tests, missing test data
🟡PR too large — 50+ files, reviewers refuse to review
Immediate ActionClose PR, split into 3-5 focused PRs of 200-400 lines each
Commands
git log --oneline main..your-branch (see all commits in the PR)
git rebase -i main (interactive rebase to split commits into logical groups)
Fix NowCreate separate branches for each logical group, open separate PRs, link them in descriptions
🟡Accidental force push to main — history overwritten
Immediate ActionDo NOT push anything else. Find the last good commit SHA immediately.
Commands
git reflog (find the last known good commit on your machine or a teammate's)
git reset --hard <good-commit-sha> && git push --force-with-lease origin main
Fix NowConfigure branch protection rules to disable force push to main. This should never happen again.
🟡PR branch is 50+ commits behind main — massive diff that isn't yours
Immediate ActionRebase your branch onto the latest main to isolate your actual changes
Commands
git fetch origin && git rebase origin/main
git diff origin/main (verify only your changes appear in the diff)
Fix NowForce push the rebased branch: git push --force-with-lease origin your-branch. The PR diff will now show only your changes.
Production IncidentForce Push to Main: 47 Commits Lost, Production Deploy Blocked for 6 HoursA junior developer accidentally force-pushed a local branch to main, overwriting 47 commits from 5 other developers. Production deploys were blocked for 6 hours while the team recovered from Git reflog. The root cause: no branch protection rules and no PR review process.
SymptomA junior developer was working on a feature directly on the main branch (they didn't know about branches). When they tried to push, Git rejected it because their local copy was behind remote. They ran git push --force and overwrote 47 commits from 5 other developers. The CI/CD pipeline deployed the old code. Production broke within minutes.
AssumptionThe junior developer assumed the force push was just 'updating their code to match what they had locally.' They didn't understand that force push replaces the remote history entirely — it doesn't merge, it overwrites. They also didn't know about branches, which would have isolated their work from main.
Root cause1. The repository had no branch protection rules — anyone could push directly to main without a PR or review. 2. The junior developer had never been taught about branches — they were working directly on main. 3. When their push was rejected (because main had moved ahead), they googled 'git push rejected' and found --force as the solution. 4. The force push replaced the remote main branch history with their local history, which was 47 commits behind. 5. CI/CD automatically deployed the overwritten code to production. 6. The team had no backup strategy — the only recovery option was Git reflog on machines that still had the correct commits.
Fix1. Immediate: identified the last known good commit SHA from the CI/CD pipeline logs and team members' local reflogs. 2. Recovered the lost commits using git reflog on the senior developer's machine (they had the most recent pull). 3. Force-pushed the correct history back to main from the recovered reflog. 4. Configured branch protection rules: require PR reviews, require CI checks to pass, disable force push to main. 5. Added a pre-push hook that warns when pushing to main without a PR. 6. Result: production restored in 6 hours. No data was permanently lost because team members had local copies.
Key Lesson
Never force push to a shared branch — main, develop, or any branch others depend on. Force push replaces history; it does not merge.Always work on a feature branch, never directly on main. One branch per feature is the golden rule.Branch protection rules are not optional — they are the safety net that prevents catastrophic mistakes. Require PR reviews and CI checks before merging.If you get a 'rejected' push error, the answer is never --force. The answer is git pull --rebase to incorporate remote changes first.
Production Debug GuideSystematic resolution paths for the most common PR and review failures in team workflows.
PR shows 50+ files changed and reviewers refuse to review it1. The PR is too large — reviewers cannot review 50 files in one sitting. 2. Close the current PR. Split the work into 3-5 smaller PRs, each focused on one logical change. 3. Open the first small PR, get it reviewed and merged, then open the next. 4. Rule of thumb: if a PR takes more than 30 minutes to review, it is too large. Target 200-400 lines of diff per PR. 5. If the changes are truly inseparable, add a detailed PR description explaining why splitting is not possible, and offer to walk the reviewer through it on a call.
CI/CD pipeline fails on your PR but passes locally1. Read the CI failure log — click the red 'X' on the PR and open the failing job. 2. Check for environment differences: CI runs on Linux, your machine may be macOS. Check for case-sensitive file paths, missing environment variables, or OS-specific dependencies. 3. Check for missing secrets: CI may not have access to API keys or tokens that are in your local .env file. 4. Reproduce locally: run the same commands CI runs (check the CI config file — .github/workflows/ for GitHub Actions). 5. If tests pass locally but fail in CI, the most common cause is timing-dependent tests (flaky tests) or missing test data setup.
PR has merge conflicts and GitHub says 'This branch has conflicts that must be resolved'1. Do NOT resolve conflicts in the GitHub web editor for complex conflicts — it is error-prone. 2. Locally: git checkout your-branch && git fetch origin && git merge origin/main. 3. Open the conflicted files — Git marks conflicts with <<<<<<<, =======, >>>>>>> markers. 4. Decide which version to keep (or combine both), remove the markers, save the file. 5. Run git add on the resolved files, then git commit to complete the merge. 6. Push the updated branch — the PR on GitHub will update automatically.
Reviewer keeps requesting changes and the PR has been open for 2 weeks1. This is a review bottleneck, not a code problem. Schedule a 15-minute pair review session with the reviewer. 2. Walk through the code together on a screen share — synchronous review is 5x faster than async comment threads. 3. If the reviewer is raising scope-creep concerns (adding new requirements not in the original ticket), escalate to the tech lead to clarify scope. 4. If the reviewer is unreachable, reassign to another reviewer. A stale PR blocks the entire team. 5. After merge, retro: why did this PR take 2 weeks? Was it too large? Was the reviewer overloaded? Fix the root cause.
PR was approved and merged but broke production within hours1. Immediate: revert the merge commit. On GitHub, go to the PR and click 'Revert' — this creates a new PR that undoes the changes. 2. Merge the revert PR immediately to restore production. 3. Root cause analysis: why did the review not catch the bug? Was the test coverage insufficient? Was the reviewer unfamiliar with the code area? 4. Add a regression test that would have caught the bug. 5. Update the PR template to include a 'Risk Assessment' section for changes that touch critical paths.

Every professional software team on the planet shares code. Not by emailing zip files or copying folders — they use a structured process where every single change gets proposed, discussed, and approved before it touches the production codebase. GitHub Pull Requests are the mechanism that makes this possible, and they're the beating heart of collaborative software development at companies like Google, Netflix, and Spotify.

Without a review process, one developer's typo can break the app for every user at 2am on a Friday. One misunderstood requirement can send a team down the wrong path for a week. Pull Requests solve this by creating a formal checkpoint: before any code merges into the main branch, at least one other human looks at it. That human catches bugs, asks clarifying questions, and ensures the change actually fits the bigger picture.

The merge strategy you choose — merge commit, squash, or rebase — directly affects how debuggable your history is six months later when you're chasing a regression at 3am. Branch protection rules are what enforce the review process; without them, anyone can push directly to main.

By the end of this article you'll know exactly what a Pull Request is, how to open one from scratch on GitHub, how to leave a code review that your teammates will actually appreciate, and how to avoid the three mistakes that make junior developers cringe in retrospect. You'll also have real terminal commands you can run right now, today.

What Is a Branch and Why You Need One Before a Pull Request

Before you can open a Pull Request, you need to understand branches — because a PR is really just a request to merge one branch into another.

Think of the main branch (often called main or master) as the official published version of your project — the printed textbook. A branch is your personal draft copy where you can experiment, write new features, or fix bugs without touching the published version. Once your work is ready and reviewed, you merge your branch back into main.

Here's the workflow in plain English: you copy the current state of main into a new branch, make your changes there, push that branch to GitHub, and then open a Pull Request to say 'hey team, I've made these changes on my branch — can someone review them before we include them in main?'

The branch name should describe the work you're doing. fix-login-button-alignment is a great branch name. my-branch or test123 will confuse everyone including future you. One branch per feature or bug fix is the golden rule — keep your PRs focused and small.

At production scale, branch naming conventions matter for automation. Teams use prefixes like feature/, fix/, hotfix/, and chore/ so CI/CD pipelines can auto-assign reviewers, auto-label PRs, and trigger different test suites based on branch type. A hotfix/ branch might skip integration tests and deploy directly to staging, while a feature/ branch runs the full suite.

io/thecodeforge/git/create_feature_branch.sh · BASH
1234567891011121314151617181920212223242526
# Make sure you start from an up-to-date main branch
# This avoids merge conflicts caused by working on stale code
git checkout main
git pull origin main

# Create a new branch with a descriptive name that explains the work
# 'checkout -b' creates the branch AND switches to it in one step
git checkout -b feature/add-user-profile-page

# Confirm you are now on the new branch — the asterisk (*) marks the active branch
git branch

# --- Make your changes to the code here ---
# For this example, we'll create a new file to simulate real work
echo "<h1>User Profile Page</h1>" > user-profile.html

# Stage the new file — 'add' tells Git to track this file in the next commit
git add user-profile.html

# Commit with a message that explains WHAT changed and WHY
# Present tense, imperative mood is the professional standard: 'Add' not 'Added'
git commit -m "Add initial user profile page HTML structure"

# Push this branch up to GitHub so others can see it
# '-u origin' links your local branch to the remote GitHub branch (only needed first time)
git push -u origin feature/add-user-profile-page
▶ Output
Already on 'main'
Your branch is up to date with 'origin/main'.
Switched to a new branch 'feature/add-user-profile-page'

* feature/add-user-profile-page
main

[feature/add-user-profile-page a3f92c1] Add initial user profile page HTML structure
1 file changed, 1 insertion(+)
create mode 100644 user-profile.html

Branch 'feature/add-user-profile-page' set up to track remote branch 'feature/add-user-profile-page' from 'origin'.
To https://github.com/your-username/your-project.git
* [new branch] feature/add-user-profile-page -> feature/add-user-profile-page
💡Pro Tip: One Branch = One Job
  • Branch names should describe the work: fix-login-button-alignment, not my-branch
  • Use prefixes: feature/, fix/, hotfix/, chore/ — CI/CD pipelines can use these for automation
  • One branch per feature or bug fix — keep PRs focused and small
  • Never work directly on main — always branch off, even for one-line fixes
📊 Production Insight
The most common branch failure in production is working directly on main without branching. When a developer pushes a broken commit to main, every other developer who pulls main gets the broken code. CI/CD deploys the broken code to staging. The fix takes 10 minutes, but the disruption to the team takes an hour. Branch protection rules — requiring PRs before merging to main — prevent this entirely. The second most common failure is stale branches: a developer branches off main, works for 2 weeks, and by the time they open a PR, main has diverged so far that the merge conflict resolution takes longer than the original feature. The fix: rebase onto main daily, not just before opening the PR.
🎯 Key Takeaway
One branch per feature, never work directly on main. Rebase onto main daily to avoid merge conflict nightmares. Branch naming conventions (feature/, fix/, hotfix/) enable CI/CD automation. If your branch is older than 2 weeks, rebase before opening the PR.
Branch Creation Decision Tree
IfStarting new work that will take more than 1 commit
UseCreate a feature branch: git checkout -b feature/descriptive-name
IfFixing a bug reported in production
UseCreate a fix branch from main: git checkout -b fix/issue-description
IfEmergency production fix needed immediately
UseCreate a hotfix branch from main: git checkout -b hotfix/critical-issue. Deploy through PR but with expedited review.
IfYou are already on main and have uncommitted changes
UseStash changes first: git stash. Then create branch: git checkout -b feature/name. Then unstash: git stash pop.
IfYour branch is 2+ weeks old and main has diverged significantly
UseRebase onto main before opening PR: git fetch origin && git rebase origin/main. Resolve conflicts locally.

How to Open a Pull Request on GitHub Step by Step

Once your branch is pushed to GitHub, opening the Pull Request takes about two minutes — but writing a good PR description takes a bit more thought, and that effort pays off every single time.

After you push a branch, GitHub usually shows a yellow banner on the repository page saying 'Your recently pushed branch... Compare & pull request'. Click that button. If the banner is gone, click the 'Pull requests' tab, then 'New pull request', and select your branch from the dropdown.

The PR form has three key parts: the title, the description, and the reviewers. The title should be a one-line summary of what the change does. The description is where you explain the context — why does this change exist? What does it do? How can the reviewer test it? A template like 'What, Why, How to Test' makes this systematic.

Assigning reviewers is critical. GitHub lets you request specific people to review your code. In a team setting, at least one approval is typically required before merging. You can also add labels ('bug', 'enhancement'), link to a related issue, and mark a PR as a 'Draft' if it's not ready for review yet — that's a great way to share work-in-progress and get early feedback without it accidentally getting merged.

At senior levels, the PR description is also where you document your design decisions. If you chose Event Sourcing over CRUD, explain why. If you skipped a test type, explain why. Future developers (including future you) will read the PR description when investigating why a piece of code exists. A PR with no description is a black box six months later.

io/thecodeforge/git/pull_request_description_template.md · MARKDOWN
123456789101112131415161718192021222324252627282930313233343536373839
## What Does This PR Do?
<!-- One or two sentences. What is the end result of merging this branch? -->
Adds an initial HTML structure for the User Profile page, which will display
the logged-in user's name, avatar, and recent activity feed.

## Why Is This Change Needed?
<!-- Link to the issue or explain the business reason -->
Addresses issue #47 — users currently have no dedicated profile page.
This is the first step in the Q3 personalisation milestone.

## How to Test It
<!-- Tell the reviewer exactly what steps to follow to verify the change works -->
1. Pull this branch locally: `git fetch && git checkout feature/add-user-profile-page`
2. Open `user-profile.html` in a browser
3. Confirm the heading 'User Profile Page' renders correctly
4. Confirm no existing pages are broken by checking `index.html` still loads

## Design Decisions (if applicable)
<!-- Document WHY you chose this approach over alternatives -->
- Chose server-side rendering over client-side SPA for this page because
  the profile data is static and SEO is a requirement.
- Used PostgreSQL `jsonb` column for user preferences instead of a separate
  table because the data is read-heavy and rarely queried independently.

## Risk Assessment
<!-- For changes touching critical paths -->
- Risk level: Lowthis is a new page with no dependencies on existing flows.
- Rollback plan: revert this PR. No database migrations involved.

## Screenshots (if relevant)
<!-- Drag and drop images here — essential for any UI changes -->
[Before: no profile page exists]
[After: /user-profile.html shows heading]

## Checklist
- [x] I have tested this change locally
- [x] I have added comments to complex code sections
- [ ] I have updated the documentation (not needed for this change)
- [x] No new console errors or warnings introduced
▶ Output
-- This is a Markdown template, not runnable code --
When pasted into the GitHub PR description box, GitHub renders it as:

A formatted description with bold headings, a numbered test list,
an image drop zone, and interactive checkboxes that teammates
can tick as they verify each item.
🔥Draft PRs Are Your Secret Weapon
  • Draft PRs cannot be merged accidentally — GitHub enforces this
  • Use Draft PRs to get architecture feedback before writing all the code
  • Convert to 'Ready for Review' when done — GitHub auto-notifies assigned reviewers
  • Draft PRs are especially useful for large features where you want early buy-in on the approach
📊 Production Insight
The single biggest thing that slows down code reviews is a bad PR description. When a reviewer opens a PR and sees 'fixed stuff' or no description at all, they have to read every line of code to understand what changed and why. This turns a 10-minute review into a 45-minute archaeology session. The fix: spend 5 minutes writing a description that answers what, why, and how to test. This saves the reviewer 30 minutes and gets your code merged faster. At the team level, PR description templates enforced through GitHub's .github/PULL_REQUEST_TEMPLATE.md file ensure every PR follows the same structure. The second biggest bottleneck is missing reviewers — a PR with no assigned reviewers sits in limbo. Always assign at least one reviewer when you open the PR.
🎯 Key Takeaway
A PR description answers three questions: what changed, why it changed, how to test it. Skipping this is the single biggest thing that slows down code reviews. Use Draft PRs for early feedback. Always assign at least one reviewer. At senior level, document design decisions and risk assessments in the PR description.
PR Description Decision Tree
IfPR touches only one file with a clear, self-explanatory change
UseMinimal description is acceptable: one-line summary + link to issue. Add screenshots if UI changed.
IfPR touches multiple files or introduces a new pattern
UseFull description required: what, why, how to test, design decisions. Include a risk assessment.
IfPR touches a critical path (auth, payments, data migrations)
UseFull description + risk assessment + rollback plan. Tag a senior reviewer even if not required by branch rules.
IfPR is a work-in-progress and you want early feedback
UseOpen as Draft PR. Description can be lighter — focus on 'here is what I am thinking, does this approach make sense?'
IfPR has no description and reviewer asks clarifying questions
UseStop. Write the description before answering questions. Every question the reviewer asks is a question the description should have answered.

How to Do a Code Review That Actually Helps

Being asked to review code is a responsibility, not a chore. A good review catches real bugs, shares knowledge across the team, and makes the codebase better. A bad review is either rubber-stamping everything or leaving vague, discouraging comments.

On GitHub, click the 'Files changed' tab in a PR to see a side-by-side diff — the red lines are what was removed, the green lines are what was added. You can click the '+' icon that appears when you hover over any line to leave an inline comment on that specific line.

There are three things to look for when reviewing: correctness (does it actually work?), clarity (can I understand what this code does in 30 seconds?), and consistency (does it follow the patterns the rest of the codebase uses?).

When you leave a comment, be specific and constructive. Instead of 'this is wrong', say 'this function will throw a null reference error if the user has no profile photo set — what if we add a fallback here?' GitHub lets you prefix comments with Nitpick: for minor style preferences so the author knows what's critical versus optional.

When you're done reviewing, click 'Review changes' and choose one of three options: Comment (general feedback, no decision), Approve (looks good to merge), or Request Changes (specific issues that must be fixed first). Only approve when you'd genuinely be comfortable with this code shipping.

At the senior level, code review is also about architectural consistency. Does this new service follow the same error handling pattern as the rest of the codebase? Does it use the same logging format? Does it introduce a new dependency that the team needs to evaluate? These are the questions that separate a senior review from a junior review.

io/thecodeforge/service/user_profile_service.js · JAVASCRIPT
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
// This is the kind of code you might review in a PR
// The inline comments below simulate what a good reviewer would flag

// REVIEWER COMMENT on line 3:
// 'getUserData' is too vague. What kind of data? Rename to 'fetchUserProfileById'
// to make the intent clear without reading the implementation.
function getUserData(id) {

  // REVIEWER COMMENT on line 7:
  // No input validation here. What happens if 'id' is null, undefined,
  // or a negative number? This will hit the API and potentially return
  // confusing errors. Suggest adding: if (!id || id <= 0) return null;
  const apiUrl = `https://api.example.com/users/${id}`;

  return fetch(apiUrl)
    .then(function(serverResponse) {
      // REVIEWER COMMENT on line 14:
      // We should check serverResponse.ok before calling .json()
      // If the API returns a 404 or 500, .json() will still run
      // and we'll get a misleading error, not a clear 'user not found'.
      return serverResponse.json();
    })
    .then(function(userData) {
      return userData;
    })
    // REVIEWER APPROVE NOTE:
    // Good job adding a .catch() — error handling is often forgotten.
    // Consider logging the error to your monitoring service here too.
    .catch(function(networkError) {
      console.error('Failed to fetch user profile:', networkError);
      return null;
    });
}

// IMPROVED VERSION after review feedback:
function fetchUserProfileById(userId) {
  // Guard clause — fail fast with a clear reason
  if (!userId || userId <= 0) {
    console.warn('fetchUserProfileById called with invalid userId:', userId);
    return Promise.resolve(null);
  }

  const profileApiUrl = `https://api.example.com/users/${userId}`;

  return fetch(profileApiUrl)
    .then(function(serverResponse) {
      // Explicitly check for HTTP errors before parsing the body
      if (!serverResponse.ok) {
        throw new Error(`User profile API returned status: ${serverResponse.status}`);
      }
      return serverResponse.json();
    })
    .then(function(userProfileData) {
      return userProfileData;
    })
    .catch(function(networkError) {
      console.error('Failed to fetch user profile:', networkError.message);
      return null;
    });
}
▶ Output
-- No runtime output for this example --
The value here is the inline review comments that demonstrate
what a senior developer looks for:
1. Naming clarity (getUserData -> fetchUserProfileById)
2. Input validation (guard clause for invalid userId)
3. HTTP error handling (checking serverResponse.ok)
4. Constructive tone: explains the WHY, not just 'this is wrong'
⚠ Watch Out: Approving Without Reading
Never click 'Approve' on a PR just to be friendly or avoid conflict. Your approval is a signal to the team that the code is safe to ship. If you find a PR too large to review properly, leave a comment saying 'This PR is too large for me to review thoroughly — can we break it into smaller pieces?' That's more helpful than a hollow approval.
📊 Production Insight
The most damaging code review anti-pattern is rubber-stamping: clicking 'Approve' without actually reading the code. This happens when reviewers are overloaded, when the PR is too large, or when the team culture treats reviews as a formality. The result: bugs reach production that a 10-minute review would have caught. The fix: enforce review quality through branch protection rules (require at least one approval), keep PRs small (200-400 lines), and create a team norm where 'Request Changes' is expected and not taken personally. The second anti-pattern is scope creep in reviews: a reviewer sees a PR to fix a login bug and leaves comments about the CSS on a completely unrelated page. Keep review comments scoped to the PR's stated purpose. If you see unrelated issues, open a separate issue or PR — do not hijack the current review.
🎯 Key Takeaway
Review for correctness, clarity, and consistency. Be specific and constructive — explain the WHY, not just 'this is wrong'. Never rubber-stamp approvals. If a PR is too large, say so and ask for it to be split. Approval means 'I'd be comfortable if this shipped right now.' Use Nitpick prefix for optional style feedback.
Code Review Decision Tree
IfPR is small (<100 lines), change is clear, tests pass
UseReview for correctness and consistency. If no issues: Approve. If minor style issues: Comment with Nitpick prefix, then Approve.
IfPR is medium (100-400 lines), touches multiple files
UseReview for correctness, clarity, consistency, and test coverage. Check for edge cases. If issues found: Request Changes with specific line references.
IfPR is large (400+ lines) or touches critical paths (auth, payments, DB migrations)
UseRequest the author to split the PR. If splitting is not possible, schedule a pair review session. Do not rubber-stamp.
IfReviewer disagrees with the author's approach
UseLeave a comment explaining your concern and suggesting an alternative. Use 'I'd suggest...' not 'You should...'. If the author explains their reasoning and it is sound, Approve.
IfPR has been open for 5+ days with no resolution
UseEscalate to tech lead. A stale PR blocks the team. Either merge with a follow-up issue, or close and rework.

Merging a Pull Request and Keeping History Clean

Once a PR has the required approvals and all CI checks pass (tests, linting, etc.), it's ready to merge. GitHub gives you three merge strategies and picking the right one matters for keeping your Git history readable.

'Create a merge commit' preserves every commit from your branch and adds a merge commit on top. This is great for long-running features where you want to see the full development history.

'Squash and merge' takes all your commits and compresses them into a single commit on main. This is perfect for small features or bug fixes where your branch had messy 'WIP' or 'fix typo' commits that aren't worth preserving. The result is a clean, linear history.

'Rebase and merge' replays your branch commits directly on top of main without a merge commit, keeping history linear. It's the cleanest option but can cause confusion if your branch had public commits others were building on.

After merging, always delete the feature branch — GitHub shows a 'Delete branch' button right after the merge. Stale branches pile up fast and confuse the whole team. Locally, run git branch -d to clean up too.

If the PR has merge conflicts — meaning main changed in ways that clash with your branch — you'll need to resolve them before merging. GitHub can handle simple conflicts in the browser, but for complex ones, resolve them locally.

At the production level, the merge strategy you choose affects how debuggable your history is. When you're chasing a regression at 3am using git bisect, a clean linear history (from squash or rebase) makes bisect dramatically faster. A branched history with 50 merge commits makes bisect nearly unusable because each merge commit is a diff of diffs.

io/thecodeforge/git/merge_and_cleanup.sh · BASH
123456789101112131415161718192021222324252627282930313233343536373839404142
# --- After your PR is approved and merged on GitHub ---
# Switch back to main and pull the freshly merged changes
git checkout main
git pull origin main

# Confirm your feature branch changes are now part of main
# The --oneline flag shows a compact one-line-per-commit view
git log --oneline -5

# Delete the local feature branch — the work is done, the branch is no longer needed
# '-d' (lowercase) is safe: it refuses to delete if the branch hasn't been merged yet
git branch -d feature/add-user-profile-page

# Confirm the branch is gone from your local machine
git branch

# --- If you need to resolve a merge conflict before merging ---
# First, pull the latest main into your feature branch to surface the conflict locally
git checkout feature/add-user-profile-page
git fetch origin
git merge origin/main

# Git will tell you which files have conflicts, e.g.:
# CONFLICT (content): Merge conflict in user-profile.html
# Open that file — you'll see conflict markers like this:
# <<<<<<< HEAD  (your changes)
# <h1>User Profile</h1>
# =======  (what's in main)
# <h1>User Details</h1>
# >>>>>>> origin/main

# Edit the file to keep the correct version, then:
git add user-profile.html
git commit -m "Resolve merge conflict: keep User Profile heading from feature branch"
git push origin feature/add-user-profile-page
# Now your PR on GitHub can be merged cleanly

# --- Verifying merge strategy on GitHub ---
# Check what merge options are available:
# Go to repo Settings > General > Pull Requests
# Options: Allow merge commits, Allow squash merging, Allow rebase merging
# Enable the ones your team uses, disable the rest to enforce consistency
▶ Output
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Already up to date.

a3f92c1 Add initial user profile page HTML structure
7b2e4d9 Update navigation links in header
3c8f1a2 Fix broken image path on homepage
9d4c7e8 Add footer contact information
1e5b0f3 Initial project setup

Deleted branch feature/add-user-profile-page (was a3f92c1).

* main
💡Pro Tip: Squash for Cleaner History
  • Squash and Merge collapses all branch commits into one clean commit on main
  • Use for small features and bug fixes where individual commit history doesn't matter
  • Do NOT squash when commit history is important for debugging (e.g., refactors where each step matters)
  • After merging, always delete the feature branch — stale branches confuse the team
📊 Production Insight
The merge strategy you choose has a direct impact on debugging speed six months later. When you're chasing a regression using git bisect, a clean linear history (from squash or rebase) makes bisect fast and reliable — each commit is a meaningful unit of change. A branched history with dozens of merge commits makes bisect nearly unusable because merge commits are diffs-of-diffs, and bisect can land on a merge commit that represents 30 changes at once. The trade-off: squash loses the individual commit history, which can be valuable for understanding the development process of a complex feature. The rule: squash for small, self-contained changes. Merge commit for large, multi-developer features where the commit-by-commit history is part of the documentation. The second production issue is stale branches: after merging, if you don't delete the branch, it stays in the remote. After 6 months, the repo has 200 stale branches and nobody knows which ones are safe to delete. Configure GitHub to auto-delete branches after merge (Settings > General > 'Automatically delete head branches').
🎯 Key Takeaway
Squash for small features and bug fixes — keeps main's history clean. Merge commit for large features where commit-by-commit history matters. Rebase for linear history with preserved commits. Always delete branches after merging. The merge strategy you choose affects how debuggable your history is when chasing regressions at 3am.
Merge Strategy Decision Tree
IfSmall feature or bug fix with messy WIP commits
UseSquash and Merge. One clean commit on main. History stays linear and readable.
IfLarge feature with meaningful commit-by-commit history
UseMerge Commit. Preserves the full development history. Useful when the commit messages document the evolution of the feature.
IfYou want linear history but individual commits matter
UseRebase and Merge. Replays commits on top of main without a merge commit. Cleanest history but use only when the branch has clean, meaningful commits.
IfBranch has merge conflicts with main
UseResolve locally first: git fetch origin && git merge origin/main. Fix conflicts, push, then merge on GitHub. Do not resolve complex conflicts in the GitHub web editor.
IfPR is merged and branch is no longer needed
UseDelete the branch immediately. On GitHub: click 'Delete branch' after merge. Locally: git branch -d branch-name. Configure GitHub to auto-delete branches after merge.
🗂 Merge Strategies Compared
Which merge strategy to use and when — based on branch size, commit quality, and debugging needs.
AspectMerge CommitSquash and MergeRebase and Merge
History styleBranched (non-linear)Linear — one clean commitLinear — all commits replayed
Individual commits preserved?Yes — every commit keptNo — squashed into oneYes — but rebased onto main
Best forLong-running feature branchesSmall features, bug fixesClean commit history fans
Merge commit added?YesYes (squash commit)No
Risk level for beginnersLow — safest optionLow — very popular choiceMedium — can cause confusion
When to avoidWhen branch has 20 'wip' commitsWhen commit history matters for debuggingWhen others branched off your branch
git bisect friendlinessPoor — merge commits are diffs-of-diffsExcellent — each commit is a meaningful unitGood — linear but individual commits preserved
Team convention recommendationUse for features > 1 week of workDefault for most PRs — cleanest main historyUse when individual commits tell a story

🎯 Key Takeaways

  • A Pull Request is a formal request to merge one branch into another — it's the review checkpoint that stands between your code and production, not just a Git feature.
  • A great PR description answers three questions: What changed, why it changed, and how to test it — skipping this is the single biggest thing that slows down code reviews.
  • Approve means 'I'd be comfortable if this shipped right now' — never click Approve to be polite; a hollow approval that lets a bug through is worse than a delayed review.
  • Squash and Merge keeps main's history readable by collapsing messy WIP commits into one clean entry — prefer it for small features and bug fixes unless individual commit history matters for debugging.
  • One branch per feature, never work directly on main. Rebase onto main daily to avoid merge conflict nightmares. Branch protection rules are not optional — they are the safety net.
  • Review for correctness, clarity, and consistency. Be specific and constructive. If a PR is too large, say so and ask for it to be split. Scope-creep in reviews hijacks the process.

⚠ Common Mistakes to Avoid

    Opening a PR directly against main from a branch you never updated
    Symptom

    GitHub shows 15+ merge conflicts and the PR diff is enormous.

    Fix

    Before pushing your branch, always run git fetch origin && git merge origin/main to incorporate the latest changes from main into your branch first. Resolve any conflicts locally, then push. Your PR will then only show the changes you actually made.

    Writing a PR description that just says 'fixed stuff'
    Symptom

    Reviewers ask three clarifying questions before they can even start reviewing, slowing the whole process down.

    Fix

    Always answer three questions in your PR description: What does this change? Why does it need changing? How can a reviewer verify it works? Paste the description template from the second section of this article and fill it in for every single PR, no matter how small.

    Responding defensively to review feedback
    Symptom

    The PR comment thread becomes a debate instead of a collaboration, and the reviewer either gives up or the author force-merges over objections.

    Fix

    Treat every review comment as a question, not an attack. Reply with 'Good point — I've updated line 14 to handle the null case' or 'I chose this approach because X — does that change your concern?' If you disagree, explain your reasoning calmly. The goal is the best possible code, not being right.

    Never deleting merged branches
    Symptom

    After 6 months the repo has 200 stale branches and nobody knows which ones are safe to delete.

    Fix

    Configure GitHub to auto-delete branches after merge (Settings > General > 'Automatically delete head branches'). Locally, run git branch -d branch-name after each merge.

    Using --force when git push is rejected
    Symptom

    You overwrite the remote branch history and potentially lose other developers' commits.

    Fix

    The answer to a rejected push is never --force. Run git pull --rebase to incorporate remote changes first, then push. If you must force push (e.g., after a rebase), use --force-with-lease which refuses to push if the remote has commits you haven't seen.

    Rubber-stamping approvals to be polite
    Symptom

    Bugs reach production that a 10-minute review would have caught.

    Fix

    Only click Approve when you'd be comfortable with the code shipping. If you don't have time to review properly, say so and reassign. A hollow approval is worse than a delayed review.

Interview Questions on This Topic

  • QWalk me through the complete lifecycle of a Pull Request — from creating a branch to the code being live on the main branch. What steps happen and who is responsible for each?
  • QWhat is the difference between Squash and Merge, Merge Commit, and Rebase and Merge on GitHub? Which would you choose for a small bug fix and why?
  • QYou open a PR and your reviewer leaves a comment saying your function has no error handling. You disagree because you think error handling belongs in the calling code, not the function itself. How do you handle this situation?
  • QA teammate force-pushed to main and overwrote 3 days of work. What do you do? Walk through the recovery process step by step.
  • QHow would you design branch protection rules for a team of 10 engineers working on a monorepo? What rules would you enforce and why?
  • QYour PR has been open for 10 days with 3 reviewers requesting changes on different aspects. How do you unblock this situation?

Frequently Asked Questions

What is the difference between a pull request and a merge request?

They're the same concept with different names. GitHub and Bitbucket call it a Pull Request (PR). GitLab calls it a Merge Request (MR). Both describe the same workflow: you propose merging one branch into another, get it reviewed, and then merge it once approved.

How many people need to approve a pull request before it can be merged?

That depends on the team's branch protection rules, which are configured in GitHub under Settings > Branches. Most professional teams require at least one approval, often two for critical repositories. As a beginner on a solo project, you can set it to zero — but getting into the habit of requesting reviews even from one peer dramatically improves code quality.

Can I keep committing to my branch after opening a pull request?

Yes, absolutely — and this is how it's meant to work. When you push new commits to your branch after the PR is open, GitHub automatically updates the PR with your new changes. Reviewers can see exactly what changed since their last review. This is how you address review feedback: make the fix locally, commit it, push, and the PR updates in real time.

What is --force-with-lease and why is it safer than --force?

--force pushes your local branch to remote and overwrites whatever is there, even if someone else pushed commits you haven't seen. --force-with-lease checks that the remote branch is at the commit you expect before pushing. If someone else pushed since your last fetch, --force-with-lease refuses and tells you. Always use --force-with-lease instead of --force when you must force push (e.g., after rebasing your branch).

How do I split a large PR into smaller ones?

Use interactive rebase: git rebase -i main. This lets you reorder, squash, or split your commits. Create separate branches for each logical group of changes using git cherry-pick or git checkout -b from specific commits. Open separate PRs for each branch and link them in the descriptions: 'This is part 2 of 3 — see PR #123 for part 1.'

🔥
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousGit Workflows — GitFlowNext →Git Tags and Releases
Forged with 🔥 at TheCodeForge.io — Where Developers Are Forged