Pull Requests and Merge Requests
Before an article goes into a newspaper, the writer hands the draft to an editor. The editor reads it, writes comments in the margin, and sends it back. The writer fixes what was asked, and only when the editor is happy does the article go to print.
A pull request is that hand-over for code. You push your work on a branch, then ask on the hosting platform: "please review this, and merge it into main". Teammates read the changes, comment on individual lines, and approve or ask for changes. Automated checks run the build and the tests. When everything is green, the branch is merged. GitHub and Bitbucket call it a pull request (PR), and GitLab calls the same thing a merge request (MR).
In this lesson, you will follow one pull request from start to finish: open it, respond to a review, merge it, and tidy up afterward.
The Life of a Pull Request
- Create a branch, commit, and push it.
- Open a pull request from the branch into
main, with a title and a description. - Teammates review it. Automated checks run.
- You update it by pushing more commits to the same branch.
- Someone merges it on the platform.
- You tidy up: delete the branch, and update your local
main.
Only steps 1, 4 and 6 happen in your terminal. Steps 2, 3 and 5 happen on the website.
Step 1: Push a Branch
As in the previous lessons, a bare repository stands in for the platform. Navika builds the late fee feature on a branch and pushes it:
git init --bare --quiet fee-tracker.git
mkdir navika
cd navika
git init --quiet fee-tracker
cd fee-tracker
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git remote add origin ../../fee-tracker.git
git push --quiet -u origin main
git switch --quiet -c feature/late-fee
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git push -u origin feature/late-feeTo ../../fee-tracker.git
* [new branch] feature/late-fee -> feature/late-fee
branch 'feature/late-fee' set up to track 'origin/feature/late-fee'.In a terminal, GitHub, GitLab and Bitbucket also print a line starting with remote: after a branch push, with a link to create a pull request for it.
Step 2: Open the Pull Request
1. Open the repository. A yellow banner offers "Compare & pull request"
for the branch you just pushed. Click it
(or: Pull requests > New pull request).
2. base: main <- compare: feature/late-fee
3. Title and description (see below).
4. Optional: reviewers, assignees, labels.
5. Click Create pull request
(or the arrow next to it > Create draft pull request).1. Open the project. A banner offers "Create merge request"
(or: Code > Merge requests > New merge request).
2. Source branch: feature/late-fee Target branch: main
3. Title and description (see below).
4. Optional: assignee, reviewers, labels, "Delete source branch
when merge request is accepted".
5. Click Create merge request (check "Mark as draft" for a draft).1. Open the repository > Pull requests > Create pull request.
2. Source: feature/late-fee Destination: main
3. Title and description (see below).
4. Optional: reviewers, "Close source branch" after merging.
5. Click Create pull request.A good pull request is easy to review:
| Part | Good practice |
|---|---|
| Title | Like a commit subject: what the change does. "Add a late fee of $250 after the 10th" |
| Description | Why the change is needed, what you changed, and how you tested it. Screenshots for visual changes. |
| Linked issue | On GitHub and GitLab, "Closes #12" in the description closes issue 12 when the PR is merged. Bitbucket links Jira issues by their key, such as FEE-12. |
| Size | Small. A few hundred changed lines can be reviewed carefully, a few thousand can't. Split big work into several pull requests. |
| Draft | Open as a draft to share work in progress. It can't be merged until you mark it ready. |
Step 3: Review
Reviewers open the Files changed (GitHub), Changes (GitLab) or Diff (Bitbucket) tab, which shows the diff you learned to read in "How to Read a Diff". They can comment on any line, suggest an exact change, and finish with one of three verdicts: comment only, approve, or request changes.
Meanwhile, checks run: the platform's automation (GitHub Actions, GitLab CI/CD, Bitbucket Pipelines) builds the branch and runs the tests, and shows a green check mark or a red X. Protected-branch rules, from the previous lesson, can require approvals and green checks before the merge button works.
Step 4: Update the Pull Request
Advitha reviews the PR and asks: "Please say when the late fee applies." There is no special command to update a pull request: Navika commits on the same branch and pushes. The pull request shows the new commit automatically:
echo "Late fee applies after the 10th of the month" >> fees.txt
git commit --quiet -am "Say when the late fee applies"
git pushTo ../../fee-tracker.git
48a944f..456fde7 feature/late-fee -> feature/late-feeShe replies to the comment, and Advitha approves.
If main moved on and the pull request shows a conflict, bring main into the branch locally (git merge origin/main, or rebase), resolve the conflict as in "Resolving Merge Conflicts", and push. Small conflicts can also be resolved in the browser on GitHub and GitLab; on Bitbucket Cloud, resolve them locally.
Step 5: Merge
The merge button on the platform offers the choices from "Fast-Forward vs Merge Commit":
| Button | What main gets | GitHub | GitLab | Bitbucket |
|---|---|---|---|---|
| Merge commit | All the branch's commits, plus a merge commit | Create a merge commit | Merge commit | Merge commit |
| Squash | One new commit with all the branch's changes | Squash and merge | Squash commits (check box) | Squash |
| Rebase / fast-forward | The branch's commits, replayed on top of main, no merge commit | Rebase and merge | Fast-forward merge (project setting) | Fast forward / Rebase |
The team chooses with Squash: one commit per feature in main. Here, a clone called platform does what the merge button does on the server:
cd ../..
git clone --quiet fee-tracker.git platform
cd platform
git merge --squash --quiet origin/feature/late-fee
git commit --quiet -m "Add the late fee (#1)"
git push --quiet origin main
git push --quiet origin --delete feature/late-fee
git log --onelineSquash commit -- not updating HEAD
a24112a (HEAD -> main, origin/main, origin/HEAD) Add the late fee (#1)
59ee64f Add the term feeSquash commit -- not updating HEAD is the same note you saw in "Fast-Forward vs Merge Commit". GitHub adds the pull request number to a squash commit's subject, as in (#1). The platform then deletes the branch, if the option for it is on.
Step 6: Tidy Up
On her computer, Navika switches to main, pulls the merged work, and removes her finished branch:
cd ../navika/fee-tracker
git switch --quiet main
git pull --pruneFrom ../../fee-tracker
- [deleted] (none) -> origin/feature/late-fee
59ee64f..a24112a main -> origin/main
Updating 59ee64f..a24112a
Fast-forward
fees.txt | 2 ++
1 file changed, 2 insertions(+)--prune removed origin/feature/late-fee, which no longer exists on the server. Now the local branch:
git branch -d feature/late-feeerror: the branch 'feature/late-fee' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D feature/late-fee'
hint: Disable this message with "git config set advice.forceDeleteBranch false"Git refuses: after a squash merge, the branch's own commits aren't in main, only a new commit with the same changes, so Git can't see that the work is merged. Check that the squash commit is there, then delete with -D:
git log --oneline -1
git branch -D feature/late-feea24112a (HEAD -> main, origin/main, origin/HEAD) Add the late fee (#1)
Deleted branch feature/late-fee (was 456fde7).After a merge-commit or rebase merge, git branch -d works, as in "Git Branch".
Common Mistakes
Opening a new pull request for every change the reviewer asks for. Push to the same branch. The pull request updates itself.
Huge pull requests. They wait for days, and get shallow reviews. Keep them small and focused.
An empty description. The reviewer has to guess why. Say why, what and how you tested.
Working on the merged branch afterward. Start new work from an updated main on a new branch.
Deleting a squash-merged branch with -d and assuming something is wrong. It is expected. Check main, then use -D.
Merging your own pull request without review on a team project. Ask for at least one approval, and let protected branches enforce it.
Under the Hood
A pull request isn't a Git feature. It is a record in the platform's database: a source branch, a target branch, the discussion and the reviews. To make every pull request fetchable, even from forks, GitHub and GitLab also store its newest commit under a special name on the server: GitHub uses refs/pull/<number>/head, and GitLab refs/merge-requests/<number>/head. A normal fetch doesn't download these. You can ask for one by name.
To show what that looks like, the stand-in server gets the same kind of name for a second pull request, the way GitHub would create it:
git switch --quiet -c feature/hostel-fee
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git push --quiet origin feature/hostel-fee:refs/pull/2/head
git switch --quiet main
git ls-remote origin "refs/pull/*"
git fetch --quiet origin pull/2/head:pr-2
git log --oneline -1 pr-2ed0ecaab3b02bf743939d3c850d36a391a5acfee refs/pull/2/head
ed0ecaa (pr-2, feature/hostel-fee) Add the hostel feegit fetch origin pull/2/head:pr-2 downloaded pull request 2's newest commit into a local branch pr-2. On GitHub, this is how you can test any pull request locally, even one from a fork you don't have as a remote. On GitLab, the same works with merge-requests/<number>/head.
Interview Corner
What is a pull request?
A request on a hosting platform to merge one branch into another, usually a feature branch into main, with a place for review comments, approvals and automated checks. GitLab calls it a merge request. It is a platform feature, not part of Git itself.
How do you update a pull request after review comments?
Commit the changes on the same branch and push. The pull request shows the new commits automatically.
What are the merge options for a pull request?
A merge commit (keeps all commits and adds a merge commit), squash (one new commit with all the changes) and rebase or fast-forward (replays the commits on top of the target without a merge commit). Teams pick one style, often enforced in the repository settings.
Why does git branch -d refuse to delete a branch that was squash-merged?
Squash creates a new commit with the branch's changes, so the branch's original commits aren't in main's history, and Git can't tell the work is merged. After checking main, delete it with git branch -D.
How do you check out someone's pull request locally?
If the branch is in your repository, git fetch and git switch <branch>. On GitHub, any PR can be fetched with git fetch origin pull/<n>/head:<local-branch> (GitLab: merge-requests/<n>/head), or with gh pr checkout <n>.
Practice
Challenge: a merge-commit merge. This time the team merges with a merge commit. Navika pushes feature/hostel-fee as a normal branch, the platform clone merges it with --no-ff and pushes main, and Navika pulls main and deletes her branch with -d.
What does Git print? Before you open the solution, decide whether git branch -d works this time.
Show solution
git push --quiet -u origin feature/hostel-fee
cd ../../platform
git pull --quiet
git merge --quiet --no-ff --no-edit origin/feature/hostel-fee
git push --quiet origin main
cd ../navika/fee-tracker
git pull --quiet
git branch -d feature/hostel-feeDeleted branch feature/hostel-fee (was ed0ecaa).It works. With a merge commit, the branch's own commit is part of main's history, so Git sees the branch as merged, and -d deletes it without complaint.
Checkpoint. On GitHub, GitLab or Bitbucket, open a pull request on a practice repository. Ask someone (or use a second account) to comment on one line. Respond with a new commit, merge it with the option your team would use, delete the branch, and update your local main.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git push -u origin <branch> | Share a branch for a pull request |
git push | Update the pull request with new commits |
git merge origin/main | Bring main into the branch to fix conflicts |
git switch main then git pull --prune | Update main after the merge |
git branch -d <branch> / -D | Delete the merged branch (-D after a squash) |
git fetch origin pull/<n>/head:pr-<n> | Fetch a GitHub pull request locally |
Was this tutorial helpful?