Pull requests and code review
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Showing steps for Windows. They are marked “On Windows” below.
In a team, nobody pushes straight to main. You put your change on a branch, push it, and open a pull request: a page on GitHub where teammates read the change, ask for fixes and merge it when it is right.
Why a pull request?
A pull request (PR) is a request on GitHub to merge the commits of one branch into another, usually a feature branch into main. It shows every changed line, so teammates can review and discuss it before it is merged.
main is the version everyone runs, so a mistake there hurts the whole team. A pull request is a checkpoint: the change waits on its own branch until someone else has read it.
The flow has five steps. Priya adds 5% GST to the shop bill, and Rahul reviews it.
Push your branch
Start from the latest main (git switch main, git pull), then work on a new branch as in Branches (opens in a new tab). Its first push needs -u, just like main had.
Example 1 · A new branch, one commit, the first push
Runs in: Your terminal
PS C:\Users\Priya\shop-bill> git switch -c add-gstPS C:\Users\Priya\shop-bill> git commit -am "Add 5% GST to the bill"PS C:\Users\Priya\shop-bill> git push -u origin add-gstOutputSwitched to a new branch 'add-gst' [add-gst 678afbd] Add 5% GST to the bill 1 file changed, 2 insertions(+), 1 deletion(-) Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 12 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 431 bytes | 431.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0) remote: remote: Create a pull request for 'add-gst' on GitHub by visiting: remote: https://github.com/priya-dev/shop-bill/pull/new/add-gst remote: To https://github.com/priya-dev/shop-bill.git * [new branch] add-gst -> add-gst branch 'add-gst' set up to track 'origin/add-gst'.
What this code does
git switch -cmade the branchadd-gstand moved to it; the commit went on that branch, not onmain.- The
remote:lines come from GitHub: a ready-made link to open a pull request for this branch. set up to track 'origin/add-gst': from now on a plaingit pushupdates this branch.
Open the pull request
GitHub notices the new branch. On the repository page, a bar says add-gst had recent pushes with a Compare & pull request button. Write a description that answers what, why and how you tested:
- Click Compare & pull request (or Pull requests → New pull request, then pick base: main and compare: add-gst).
- Write a short title that says what the change does:
Add GST to the bill. - Paste the description below. Under Reviewers on the right, pick Rahul.
- Click Create pull request. It gets a number,
#1, and the label Open.
Example 2 · The pull request description
## WhatAdds 5% GST to the shop bill.## WhyThe shop now charges GST on every order.## How I tested- Ran `cat bill.txt`- 2 notebooks, 3 pens, 1 ruler: GST Rs. 4.3, total Rs. 90.3What <- heading Adds 5% GST to the shop bill. Why <- heading The shop now charges GST on every order. How I tested <- heading • Ran cat bill.txt • 2 notebooks, 3 pens, 1 ruler: GST Rs. 4.3, total Rs. 90.3
What this code does
- The description box takes Markdown, as in Write a README in Markdown (opens in a new tab).
- What, why and how you tested: the reviewer understands the change without asking you.
- The test line shows real output, so the reviewer can spot the problem in it: Rs. 4.3 should be Rs. 4.30.
Code review
Code review is when a teammate reads the changes in a pull request before they are merged, to catch mistakes and suggest improvements. The reviewer comments on lines, then approves or requests changes.
Rahul opens the Files changed tab: red lines were removed, green lines added. He clicks + next to the GST line and writes: "Money should show 2 decimals: Rs. 4.30, not Rs. 4.3." Then Review changes → Request changes.
Priya fixes it on the same branch by writing the amounts with two decimals, checks the file and pushes again.
On Windows
In PowerShell, still on add-gst:
PS C:\Users\Priya\shop-bill> cat bill.txtPS C:\Users\Priya\shop-bill> git commit -am "Round GST to 2 decimals"PS C:\Users\Priya\shop-bill> git pushOutputNotebook x 2: Rs. 30 Pen x 3: Rs. 36 Ruler x 1: Rs. 20 GST 5%: Rs. 4.30 Total to pay: Rs. 90.30 [add-gst 29d5aff] Round GST to 2 decimals 1 file changed, 2 insertions(+), 2 deletions(-) Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 12 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 421 bytes | 421.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0) To https://github.com/priya-dev/shop-bill.git 678afbd..29d5aff add-gst -> add-gst
- The file shows the fix first:
Rs. 4.30instead ofRs. 4.3. - A plain
git pushsends the new commit to the same branch,add-gst. The open pull request shows it at once; there is no new pull request. - Priya replies "Done in the new commit" under the comment and clicks Resolve conversation.
Rahul looks again and chooses Approve. Good review comments are about the code, not the person, and they say why: "Show 2 decimals, because it is money" helps more than "wrong".
Merge and clean up
With an approval, the pull request is ready. Merge it on GitHub, then bring the merged main to your laptop and delete the old branch there too.
- On the pull request page, click Merge pull request, then Confirm merge. The label changes to Merged.
- Click Delete branch:
add-gstis gone from GitHub. - On your laptop, run the commands below.
Example 3 · After the merge: update main, delete the branch
Runs in: Your terminal
PS C:\Users\Priya\shop-bill> git switch mainPS C:\Users\Priya\shop-bill> git pullPS C:\Users\Priya\shop-bill> git branch -d add-gstPS C:\Users\Priya\shop-bill> git fetch --pruneOutputSwitched to branch 'main' Your branch is up to date with 'origin/main'. remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) From https://github.com/priya-dev/shop-bill add90c1..a4ccd25 main -> origin/main Updating add90c1..a4ccd25 Fast-forward bill.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) Deleted branch add-gst (was 29d5aff). From https://github.com/priya-dev/shop-bill - [deleted] (none) -> origin/add-gst
What this code does
git pullbrought the merge from GitHub:mainnow has the GST change.git branch -ddeletes the local branch; Git allows it because its commits are now inmain.git fetch --pruneremovesorigin/add-gst, the saved copy of the branch that was deleted on GitHub.
Merge options on GitHub
When the review is approved and the checks pass, the Merge pull request button has a small arrow with three ways to merge. Your team picks one, so use the one its README or CONTRIBUTING file names.
- Merge commit: keeps every commit of the branch and adds one merge commit.
- Squash and merge: joins all commits of the branch into one commit on
main, so the history stays short. - Rebase and merge: replays your commits on top of
mainwith no merge commit. - After any of them, delete the branch: its work now lives on
main.
Good pull requests and good reviews
A pull request title is its one-line name in the list. Many teams write it like a commit message, for example fix: round the GST to 2 decimals, so the list reads like a changelog.
A pull request is easy to review when it is small (one change), has a clear title and says what, why and how it was tested. Open the next pull request for the next change instead of growing this one.
- As a reviewer, read for correctness first, then for clarity. Comment on the code, not the person, and say why.
- Suggest a fix, not only a problem. Approve when the change is good enough, not perfect.
- As the author, answer every comment with a commit or a reply, so the reviewer knows it is done.
- Do not push to
maindirectly just to save time: the review is the safety net.
Next step
Check your understanding
12 quick questions