Loading

T7 Git and GitHub · Lesson 6

Pull requests

On a shared repository, nobody changes main alone. Put each change on a branch of its own, push it, and ask for it to be merged: a pull request. Others read it and leave reviews, you answer with more commits, and when it is approved the branch joins main, by one of three merge methods. Then the branch is tidied away.

  • put a change on a topic branch and push it for review
  • open a pull request, and see what it introduces with a three-dot diff
  • review a pull request, and answer a review with more commits
  • bring main's new commits into a pull request's branch
  • merge a pull request with a merge commit or a squash
  • delete a merged branch, here and on the remote, and prune the rest
About 55 minutesChecked against Git on the laptop (the kit compares every git block with it, in its sandbox) 2.53.0 (2026-11-07); GNU Bash on the laptop (the kit compares every shell block with it, in a sandbox) 5.3.9 (2026-11-07)Play this lesson's games

The back cover

On a shared repository, nobody changes main alone. Each change waits on a branch of its own until others have read it, so that two people check every line before it joins the work everyone depends on.

These pages show how to put one change on one branch and open a pull request for it. They show how others review it, how to keep it up to date while it waits, and how it is finally merged. They also show how to tidy up the branches afterwards, so that only work in progress remains.

Before you start

The story: Corin's improvement

Corin has improved the guild's shared pump schedule, and he is proud of it. He walks up to the city archive to put it straight into the main drawer that every hall reads.

The clerk at the hill stops him at the desk. Nobody changes the shared drawer alone, however good the change.

Keeper Hollis, who has followed him up the hill, is not surprised either. "Here, you ask before you change," she says. "Others read it first. That is how a shared sheet stays right."

Lesson 05 gave the repository a home that everyone shares. Shared work needs a rule, or the main line fills with changes that nobody checked. The rule that most teams follow is simple to state: no change goes into main until someone else has read it. A pull request is how that reading happens.

It builds on these ideas. Each one has a tiny example, so you can check that you remember it.

What you already knowA tiny exampleWhere it comes back
Branches.git switch -c pump-scheduleEach change gets one.
Pushing a branch.git push -u origin pump-scheduleA pull request starts from a pushed branch.
Fetching.git fetchA reviewer brings the branch down.
Ranges.main..origin/pump-scheduleWhat the branch would bring.
Merges.git merge --no-editMerging a pull request is a merge.
Fast-forward.Fast-forwardA pull request's merge usually makes a merge commit instead.
Conflicts.<<<<<<< HEADSettled on the branch before the merge.
git branch -d.git branch -d summerTidying after the merge.
Two folders.cd ../corinCorin's clone and the shop's.
A refused push.non-fast-forwardAfter rewriting your own branch.

By the end of this lesson you will be able to:

  • put a change on a topic branch and push it for review;
  • open a pull request, and see what it introduces with a three-dot diff;
  • review a pull request, and answer a review with more commits;
  • bring main's new commits into a pull request's branch;
  • merge a pull request with a merge commit or a squash;
  • delete a merged branch, here and on the remote, and prune the rest.

How to follow this lesson

Two people share the bare repository ../hill.git, which stands in for the guild's GitHub repository: Asha, in the shop's folder, where every block starts, and Corin, in his clone ../corin. A block that says cd ../corin works as Corin. The pull request's pages on GitHub are described in words, as this page has no network; everything git does is run.

In one line

This lesson is about pull requests: a change on its own branch, read and approved by others, before it joins main.

One branch for one change

At a glance

On a shared repository, main is the work that everyone builds on. Nobody changes it directly.

Each change goes on a topic branch of its own, named after the change, such as pump-schedule. Push the branch with -u, so that others can see it and so that later pushes need no names. Main stays untouched until the change has been read and approved. One branch holds one change, so it can be judged, and merged or dropped, on its own.

Take a guess: How many lines make a change too large for one review?

Corin has improved the pump schedule. On a shared repository he does not commit it to main. He puts it on a topic branch: a branch made for one change, named after it. Then he pushes the branch, so that others can see it:

In the shop's repository, whose main is pushed to origin, ../hill.git, with Corin's clone of it in ../corin

cd ../corin
git switch -c pump-schedule
printf 'pump 7 at dawn\n' > pumps.txt
git add pumps.txt
git commit -q -m 'Add the pump schedule'
git push -u origin pump-schedule 2>&1 | cat
Switched to a new branch 'pump-schedule'
To /home/asha/shop/../hill.git
 * [new branch]      pump-schedule -> pump-schedule
branch 'pump-schedule' set up to track 'origin/pump-schedule'.

Everything here is from lessons 03 and 05: a branch, a commit, and a push with -u. The shared repository now holds two branches. main is untouched:

In the same setup, where Corin has pushed a branch pump-schedule (one commit: pumps.txt, pump 7 at dawn); the shop has not fetched it yet

git -C ../hill.git branch
* main
  pump-schedule

The star marks main as the bare repository's own HEAD, the branch that a clone checks out. A topic branch can hold one commit or many; what matters is that it holds one change, so that it can be read, judged and merged as one.

Topic branchHolds
pump-schedulethe new pump schedule
fix-gate-4-timeone fix
east-gateone feature, perhaps over several commits

GitHub calls this the shared repository model: everyone may push to one repository, and each change goes on a branch of its own. In the other model, fork and pull, you cannot push to the project at all. You make a fork, a copy of the repository under your own account, push your branch there, and ask the project to pull it from your fork. The steps that follow are the same in both.

Worked example 1

Corin is ready to share his pump schedule. Put it on its own branch, push it, and check where things stand on both sides.

What we have: Corin's clone, on main, level with the hill

What we need: a branch pump-schedule on the hill, with Corin's commit, and main unchanged

The plan: git switch -c; add and commit; git push -u; then the short status, and the branch's log inside the hill

In the shop's repository, whose main is pushed to origin, ../hill.git, with Corin's clone of it in ../corin

cd ../corin
git switch -q -c pump-schedule
printf 'pump 7 at dawn\n' > pumps.txt
git add pumps.txt
git commit -q -m 'Add the pump schedule'
git push -q -u origin pump-schedule
git status -sb
git -C ../hill.git log --oneline pump-schedule
## pump-schedule...origin/pump-schedule
2e26e06 (pump-schedule) Add the pump schedule
aba517b (HEAD -> main) First orders
  1. Corin's branch is level with its upstream. Inside the hill, pump-schedule has his commit on top of the first orders, and main, the hill's HEAD, is still at aba517b.

The hill has pump-schedule at 2e26e06, and main is untouched.

Check: git -C ../hill.git log --oneline main shows only aba517b.

A path that fails: committing the change on main and pushing that. The change would be on the shared main before anyone read it, which is what the rule forbids.

When you are stuck: you are not sure your branch is where others can see it

git status -sb should show the branch with its upstream, as in ## pump-schedule...origin/pump-schedule.

No upstream in that line: push once with -u, naming the remote and the branch.

git branch -r, after a fetch, lists the branches on the remote.

Your turn

Why does Corin push a branch instead of committing to main?Tap to reveal
  1. The shared main is only changed after someone has read the change.

So that the change can be read and approved before it joins main.

What is a fork?Tap to reveal
  1. It belongs to your own account.

A copy of the repository under your own account, to push to when you may not push to the project.

git -C ../hill.git branch
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
* main
  pump-schedule
  1. The hill now has two branches.
  2. Its HEAD, starred, is main.

It prints * main and pump-schedule.

Did you know?

100 lines, not 1000

Google's published guide for code review has a rule of thumb for size. It says there are no hard and fast rules, but that 100 lines is usually a reasonable size for a change, and 1000 lines is usually too large, leaving the final call to the reviewer. It adds that a change spread across many files counts as larger than the same number of lines in one file.

Why it fits here: It answers this topic's question: Google's guide calls 100 lines a reasonable change and 1000 lines usually too large.

Source: Google Engineering Practices: Small CLs

The pull request before the button

Asking someone to pull your work is older than GitHub's buttons. Git itself has a command for it, git request-pull. Its manual says it generates a request asking an upstream project to pull changes into its tree, printed to the screen, with a summary of the changes and where they can be pulled from. Such requests were sent by email, as projects like the Linux kernel still do.

Why it fits here: It shows where this topic's pull request comes from: git's own request-pull writes such a request for sending by email.

Sources: git manual: git-request-pull; The Linux kernel documentation: Submitting patches

In one line

On a shared repository, each change goes on a topic branch of its own, which is pushed with -u for others to see; main stays untouched until the change has been read.

Opening a pull request

At a glance

Once the branch is pushed, you ask for it to be merged. On GitHub that request is a pull request.

A pull request proposes to merge a compare branch, your topic branch, into a base branch, usually main. You give it a title and a description of what the change does and why. Its Files changed tab shows a three-dot diff: only the branch's own changes since it began. In git, the same diff is git diff main...topic. Reviewers read that diff first.

Take a guess: Can a pull request close an issue by itself when it is merged?

A pull request is a proposal to merge one branch into another. It is opened on GitHub's website, not with git. GitHub's documentation gives the steps:

  1. On the repository's page, choose the branch with your commits in the Branch menu.
  2. Click Compare & pull request, in the yellow banner above the files.
  3. In the base menu, choose the branch to merge into, here main; in the compare menu, the topic branch, here pump-schedule.
  4. Write a title and a description: what the change does, and why.
  5. Click Create pull request. Or choose Create draft pull request, for work that is not ready: a draft cannot be merged.

The pull request gets a number, such as #1, and a page of its own. The page shows the commits of the branch, the conversation, and a tab named Files changed with the diff. Pushing more commits to the branch later adds them to the same pull request.

What the pull request introduces

Before asking others to read a change, see it as they will. After a fetch, the range of lesson 03 lists the branch's commits that main lacks:

In the same setup, where Corin has pushed a branch pump-schedule (one commit: pumps.txt, pump 7 at dawn); the shop has not fetched it yet

git fetch -q
git log --oneline main..origin/pump-schedule
2e26e06 (origin/pump-schedule) Add the pump schedule

And the diff with three dots, main...origin/pump-schedule, shows the changes the branch made since it began, from the merge base to its tip. This is the diff that GitHub's Files changed tab shows:

In the same setup, where the shop has fetched Corin's branch, origin/pump-schedule

git diff main...origin/pump-schedule
diff --git a/pumps.txt b/pumps.txt
new file mode 100644
index 0000000..2352b5c
--- /dev/null
+++ b/pumps.txt
@@ -0,0 +1 @@
+pump 7 at dawn

Two dots or three

The difference matters once main has moved. Here main has a new commit, Open gate 6, that the branch lacks. --stat gives the diffstat of each comparison:

In the same setup, where main has moved on (Open gate 6, pushed) since Corin's branch began

git diff --stat main...origin/pump-schedule
echo ---
git diff --stat main origin/pump-schedule
 pumps.txt | 1 +
 1 file changed, 1 insertion(+)
---
 orders.txt | 1 -
 pumps.txt  | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)

With three dots, only the branch's own change shows. Comparing the two tips directly also shows main's new line, as a line removed, as if the branch took it away. It does not: the branch never had it. That is why a pull request shows the three-dot form.

WrittenComparesShows
git log main..topiccommits on topic that main lackswhat the pull request would add
git diff main...topicthe merge base with topic's tipthe branch's own changes: Files changed
git diff main topicmain's tip with topic's tipevery difference, main's new work included

Worked example 2

Before opening the pull request, Corin wants to see exactly what it will show: its commits, and which files change, and by how much.

What we have: the branch pump-schedule pushed, and fetched in the shop

What we need: the commits and the diffstat that the pull request will show

The plan: the range main..origin/pump-schedule for the commits; git diff --stat with three dots for the files

In the same setup, where the shop has fetched Corin's branch, origin/pump-schedule

git log --oneline main..origin/pump-schedule
git diff --stat main...origin/pump-schedule
2e26e06 (origin/pump-schedule) Add the pump schedule
 pumps.txt | 1 +
 1 file changed, 1 insertion(+)
  1. One commit, and one file with one line added: a small pull request, easy to read.

The pull request brings one commit and one new file, pumps.txt.

Check: git diff main...origin/pump-schedule shows the single line pump 7 at dawn.

A path that fails: git diff main origin/pump-schedule once main has moved. It mixes main's new work into the picture, as removals.

When you are stuck: you are not sure what a pull request shows

Its commits: git log --oneline main..branch.

Its changes, as on Files changed: git diff main...branch, with three dots.

Nothing shows: fetch first, so that origin/branch is up to date.

Your turn

In a pull request, what are the base and the compare branch?Tap to reveal
  1. One is merged into the other.

The base is the branch to merge into, such as main; compare is the topic branch with the changes.

Why does GitHub show a three-dot diff for a pull request?Tap to reveal
  1. main may have moved since the branch began.

Because it shows only what the branch introduces, from the merge base, not main's own newer work.

git diff --stat main...origin/pump-schedule
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
pumps.txt | 1 +
1 file changed, 1 insertion(+)
  1. Three dots compare the merge base with the branch's tip.
  2. The branch added one line, in pumps.txt.

It prints pumps.txt | 1 + and 1 file changed, 1 insertion(+).

Did you know?

Fixes #12 closes issue 12

A pull request can tidy up the to-do list. GitHub's documentation says a pull request can be linked to an issue by a keyword in its description or in a commit message: close, closes, closed, fix, fixes, fixed, resolve, resolves or resolved, followed by the issue's number. When the pull request is merged into the default branch, the linked issue closes by itself.

Why it fits here: It answers this topic's question: a keyword such as Fixes #12 in the description closes the issue when the pull request is merged.

Source: GitHub Docs: Linking a pull request to an issue

Reviewers chosen by a file

A pull request can ask the right people by itself. GitHub's documentation says a file named CODEOWNERS defines the people or teams responsible for code in a repository. Code owners are automatically requested for review when someone opens a pull request that changes code they own, though not for a draft. So a change to the payments folder can reach its owners without anyone naming them.

Why it fits here: It adds to this topic's opening: a CODEOWNERS file asks the owners of the changed code to review automatically.

Source: GitHub Docs: About code owners

In one line

A pull request, opened on GitHub, proposes to merge a compare branch into a base branch; its Files changed tab shows a three-dot diff, which git gives as git diff main...topic: the branch's own changes since it began.

Reviewing a pull request

At a glance

A review is a careful reading of someone's change, line by line, with comments where something is unclear or wrong.

A reviewer submits it as Comment, Approve or Request changes. The author answers with more commits on the same branch, and the pull request takes them in. On a branch that is the author's alone, the author may rewrite the commits instead and push with --force-with-lease, never a plain --force. Reviewing takes time, so small changes are reviewed faster and more carefully.

Take a guess: How fast should a reviewer answer a request for review?

To review a pull request is to read it carefully, try it, and say what you think. Asha reviews Corin's. She can read the diff on GitHub, but she can also take the branch into her own repository and run it. After a fetch, git switch with the branch's name makes a local branch that follows Corin's:

In the same setup, where the shop has fetched Corin's branch, origin/pump-schedule

git switch pump-schedule
cat pumps.txt
branch 'pump-schedule' set up to track 'origin/pump-schedule'.
Switched to a new branch 'pump-schedule'
pump 7 at dawn

There was no local pump-schedule, but origin/pump-schedule existed, so git made one from it, with that upstream. This guess saves a git switch -c with a start point.

Three kinds of review

On the pull request's page, a reviewer leaves notes on the whole change or on single lines, and may suggest an exact change to a line. Then the reviewer submits the review as one of three kinds:

ReviewMeans
Commentnotes, with no verdict yet
Approvethe change is ready to merge
Request changesthe author must answer these notes before the merge

Answering a review

Asha asks for pump 7 at six instead of dawn. Corin makes the change on the same branch, commits and pushes. The new commit joins the same pull request; nothing else is needed:

In the same setup, where Corin has pushed a second commit to pump-schedule, after the review, and the shop has fetched it

git log --oneline main..origin/pump-schedule
git diff main...origin/pump-schedule | tail -n 1
509dac7 (origin/pump-schedule) Pump 7 at six, as the review asked
2e26e06 Add the pump schedule
+pump 7 at six

The pull request now has two commits, and its diff shows only the final result, pump 7 at six. The reviewer reads the new commit, and approves.

Rewriting your own branch

Some teams prefer a tidy branch, and the author changes a commit instead of adding one. git commit --amend replaces the latest commit with a new one. The new commit has a new name, so a plain push is refused, as in lesson 05. --force-with-lease pushes it anyway, but only if the branch on the remote is still where you last saw it:

In the same setup, where Corin has pushed a branch pump-schedule (one commit: pumps.txt, pump 7 at dawn); the shop has not fetched it yet

cd ../corin
git commit -q --amend -m 'Add the pump schedule for spring'
git push 2>&1 | cat
git push --force-with-lease 2>&1 | cat
To /home/asha/shop/../hill.git
 ! [rejected]        pump-schedule -> pump-schedule (non-fast-forward)
error: failed to push some refs to '/home/asha/shop/../hill.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
To /home/asha/shop/../hill.git
 + 2e26e06...c59414c pump-schedule -> pump-schedule (forced update)

The + and the three dots mark a forced update: the old commit 2e26e06 is no longer on the branch. Do this only on your own topic branch, never on main or on a branch that others build on.

Worked example 3

Asha's review asks for pump 7 at six. Corin answers with a new commit on the same branch, and checks what the pull request now holds.

What we have: Corin's branch, pushed, with one commit

What we need: a second commit on the branch, pushed, so that the pull request shows it

The plan: change the file, commit with a message that says what the review asked, push; then the range main..pump-schedule

In the same setup, where Corin has pushed a branch pump-schedule (one commit: pumps.txt, pump 7 at dawn); the shop has not fetched it yet

cd ../corin
printf 'pump 7 at six\n' > pumps.txt
git commit -q -am 'Pump 7 at six, as the review asked'
git push -q
git log --oneline main..pump-schedule
509dac7 (HEAD -> pump-schedule, origin/pump-schedule) Pump 7 at six, as the review asked
2e26e06 Add the pump schedule
  1. Two commits on the branch, both on the hill. The pull request shows them both, and its conversation shows where the second came from.

The branch holds two commits, the second answering the review.

Check: git status -sb in ../corin shows the branch level with origin/pump-schedule.

A path that fails: opening a new pull request for the fix. The open pull request already follows the branch; a push to it is all that is needed.

When you are stuck: a review round goes wrong

The reviewer sees no change: has the author pushed? git status -sb shows ahead if not.

non-fast-forward after an amend: the branch was rewritten; push with --force-with-lease, on your own branch only.

stale info: someone else pushed to the branch since your last fetch; fetch, look, and decide before forcing.

Your turn

Which kind of review says that the change is ready to merge?Tap to reveal
  1. There are three: Comment, Approve and Request changes.

Approve.

git switch -q pump-schedule
git status -sb
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
## pump-schedule...origin/pump-schedule
  1. git switch made pump-schedule from origin/pump-schedule.
  2. It follows that branch, level with it.

It prints ## pump-schedule...origin/pump-schedule.

Why is a push refused after git commit --amend?Tap to reveal
  1. The amended commit replaces the old one, with a new name.

Because the remote's branch has the old commit, which the new branch no longer contains: it is not a fast-forward.

Did you know?

One business day at most

Google's guide for reviewers puts a number on speed. It says that if you are not in the middle of a focused task, you should review a change shortly after it comes in. One business day is the maximum time to respond to a review request, which in practice means first thing the next morning. A slow review holds up the author's whole line of work.

Why it fits here: It answers this topic's question: Google's guide asks reviewers to respond within one business day at most.

Source: Google Engineering Practices: Speed of Code Reviews

What a review is for

Google's guide names the purpose of reviewing plainly. The primary purpose of code review, it says, is to make sure that the overall code health of the code base is improving over time. It warns reviewers not to wait for a perfect change: they should approve one that definitely improves the code, even if it is not perfect.

Why it fits here: It frames this topic's Approve: a reviewer approves a change that definitely improves the code, not only a perfect one.

Source: Google Engineering Practices: The Standard of Code Review

In one line

A review is a reading of the change, submitted as Comment, Approve or Request changes; the author answers with more commits on the same branch, or, on their own branch only, rewrites it and pushes with --force-with-lease.

Keeping a pull request up to date

At a glance

A pull request may wait days for its review, and main does not stand still meanwhile. Other changes are merged into it.

The author brings main's new work into the topic branch: git fetch, then git merge origin/main while on the topic branch. Any conflict is settled there, on the author's own branch, where it disturbs nobody. Then a push updates the pull request. When the time comes to merge it into main, the merge is clean. Nobody has to settle conflicts on main.

Take a guess: Can GitHub update a waiting branch with one click?

While a pull request waits for review, main does not stand still. Other pull requests are merged into it. Here main has gained Open gate 6 since Corin's branch began. The range, with the two names swapped, lists what main has that the branch lacks:

In the same setup, where main has moved on (Open gate 6, pushed) since Corin's branch began

git log --oneline origin/pump-schedule..main
49166e7 (HEAD -> main, origin/main, origin/HEAD) Open gate 6

A branch that is behind main may still merge cleanly. But many teams ask the author to bring main's new work into the branch first, so that the pull request is tested with it, and any conflict is settled by the person who knows the change best. The author fetches and merges origin/main into the topic branch, then pushes:

In the same setup, where main has moved on (Open gate 6, pushed) since Corin's branch began

cd ../corin
git fetch -q
git merge --no-edit origin/main
git push -q
git log --oneline --graph
Merge made by the 'ort' strategy.
 orders.txt | 1 +
 1 file changed, 1 insertion(+)
*   66f0b06 (HEAD -> pump-schedule, origin/pump-schedule) Merge remote-tracking branch 'origin/main' into pump-schedule
|\  
| * 49166e7 (origin/main, origin/HEAD) Open gate 6
* | 2e26e06 Add the pump schedule
|/  
* aba517b (main) First orders

This is lesson 04's merge, with the two lines the other way round: main's work comes into the topic branch. Git's message adds into pump-schedule, as the branch receiving the work is not main. Corin's own main, at aba517b, is behind; he does not need it to work on the branch.

If main and the branch had changed the same lines, the merge would stop with a conflict, which Corin settles on his branch, exactly as in lesson 04. The pull request's merge into main is then clean. Some teams rebase instead of merging here; rebasing rewrites the branch's commits, and this course leaves it for later.

Worked example 4

Main has moved on while Corin's pull request waits. Bring main's work into his branch, push it, and check from the shop that the branch now lacks nothing of main.

What we have: main with Open gate 6; Corin's branch without it

What we need: the branch holding all of main

The plan: in ../corin: fetch, merge origin/main, push; in the shop: fetch, then count the commits that main has and the branch lacks

In the same setup, where main has moved on (Open gate 6, pushed) since Corin's branch began

cd ../corin
git fetch -q
git merge -q --no-edit origin/main
git push -q
cd ../shop
git fetch -q
git log --oneline origin/pump-schedule..main | wc -l
0
  1. No commit of main is missing from the branch now. The pull request will merge into main without surprises.

The branch lacks 0 commits of main.

Check: before the merge, the same count was 1.

A path that fails: merging the branch into main to bring it up to date. That merges the pull request itself, before anyone approved it.

When you are stuck: a pull request falls behind main

To see what it lacks: git log --oneline origin/branch..main, after a fetch.

To bring main in: on the branch, git fetch, then git merge origin/main, then git push.

A conflict: settle it on the branch, as in lesson 04; git add, git commit, git push.

Your turn

git log --oneline origin/pump-schedule..main | wc -l
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
1
  1. main has one commit, Open gate 6, that the branch lacks.

It prints 1.

Who should settle a conflict between main and a pull request's branch, and where?Tap to reveal
  1. The person who knows the change best.

The author, on the topic branch, by merging origin/main into it.

Why does the merge message end with into pump-schedule?Tap to reveal
  1. Git adds the receiving branch when it is not main.

Because the work came into pump-schedule, not into main.

Did you know?

The Update branch button

The merge from main can be done on GitHub too. Its documentation says a pull request page can update the branch when it is behind the base branch and has no merge conflicts. The button Update branch performs a traditional merge, and a menu beside it offers to update with a rebase instead. Conflicts must still be resolved before updating.

Why it fits here: It answers this topic's question: GitHub's Update branch button merges main into a waiting branch with one click.

Source: GitHub Docs: Keeping your pull request in sync with the base branch

Up to date before merging

Some repositories insist on this step. GitHub's documentation describes a branch protection setting, Require branches to be up to date before merging. With it on, a pull request's branch must be up to date with the base branch before it can be merged, so its tests run against the latest main. The documentation notes that this can mean more builds.

Why it fits here: It shows this topic's step enforced: a protected branch can refuse pull requests that are behind main.

Source: GitHub Docs: About protected branches

In one line

While a pull request waits, main may move on; the author brings it in with git fetch and git merge origin/main on the topic branch, settles any conflict there, and pushes, so that the final merge is clean.

Merging the pull request

At a glance

An approved pull request is merged into main. On GitHub that is a button, with three methods to choose from.

In git, the same merge is a merge with --no-ff, which makes a merge commit joining the branch's commits, or a merge with --squash followed by one commit of your own that holds all the branch's changes. Either way, main is then pushed. The pull request shows as merged, and its branch has done its job. The review stays on the pull request's page for later readers.

Take a guess: Does git merge --squash make a merge commit?

When the reviewers approve, someone with the right to change main merges the pull request. On GitHub it is a button on the pull request's page, with three choices:

MethodWhat it does to main's history
Create a merge commitkeeps every commit of the branch, and adds a merge commit; this is the default, a merge with --no-ff
Squash and mergeputs all of the branch's changes into one new commit on main
Rebase and mergecopies each commit of the branch onto main, with no merge commit

The first two are git merges that you already know, and git can do them on your own computer too. A merge with --no-ff, for no fast-forward, makes a merge commit even where a fast-forward is possible, so that the history shows where the pull request joined:

In the same setup, where the shop has fetched Corin's branch, origin/pump-schedule

git merge --no-ff --no-edit origin/pump-schedule
git log --oneline --graph
Merge made by the 'ort' strategy.
 pumps.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 pumps.txt
*   78632b8 (HEAD -> main) Merge remote-tracking branch 'origin/pump-schedule'
|\  
| * 2e26e06 (origin/pump-schedule) Add the pump schedule
|/  
* aba517b (origin/main, origin/HEAD) First orders

Without --no-ff, git would have fast-forwarded main to 2e26e06, and the graph would be one straight line, with no sign of the pull request. A squash puts the changes on the desk and the tray instead, and you make the one commit yourself:

In the same setup, where the shop has fetched Corin's branch, origin/pump-schedule

git merge --squash origin/pump-schedule
git commit -q -m 'Add the pump schedule (#1)'
git log --oneline
Updating aba517b..2e26e06
Fast-forward
Squash commit -- not updating HEAD
 pumps.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 pumps.txt
36ec178 (HEAD -> main) Add the pump schedule (#1)
aba517b (origin/main, origin/HEAD) First orders

Squash commit -- not updating HEAD says that main has not moved: the changes wait on the tray for your commit. The (#1) in the message follows GitHub's custom of naming the pull request's number. The rebase method needs git rebase, which this course leaves for later.

Merge withmain's history afterwards
git merge --no-ffthe branch's commits, joined by a merge commit
git merge --squash, then a commitone new commit, with all the branch's changes

Worked example 5

Corin's pull request is approved. Merge it into main with a merge commit, as GitHub's default button would, and send main up the hill.

What we have: the shop's main, with Corin's branch fetched and approved

What we need: a merge commit on main, pushed to the hill

The plan: git merge with --no-ff and --no-edit; git push; then read the hill's main

In the same setup, where the shop has fetched Corin's branch, origin/pump-schedule

git merge -q --no-ff --no-edit origin/pump-schedule
git push -q
git -C ../hill.git log --oneline -1
78632b8 (HEAD -> main) Merge remote-tracking branch 'origin/pump-schedule'
  1. The hill's main is at the merge commit, so everyone who pulls gets Corin's schedule, and a merge commit that marks where it joined.

main on the hill is at 78632b8, the merge commit.

Check: git status -sb in the shop shows main level with origin/main.

A path that fails: merging without --no-ff. Here git would fast-forward, and the history would not show that this was a pull request.

When you are stuck: merging a pull request

Merged but not on the hill: run git push.

Squash commit -- not updating HEAD: the changes are staged; finish with git commit.

A conflict: the branch was not up to date with main; settle it, or ask the author to bring main in first.

Your turn

Which of GitHub's merge methods keeps every commit of the branch and adds a merge commit?Tap to reveal
  1. It is the default.

Create a merge commit, a merge with --no-ff.

git merge -q --no-ff --no-edit origin/pump-schedule
git log --oneline -1 | cut -d ' ' -f 2-
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
Merge remote-tracking branch 'origin/pump-schedule'
  1. --no-ff makes a merge commit even here.
  2. Merging origin/pump-schedule names it as a remote-tracking branch.

It prints Merge remote-tracking branch 'origin/pump-schedule'.

After git merge --squash, has main moved?Tap to reveal
  1. Read the line Squash commit -- not updating HEAD.

No: the changes are staged, and main moves when you commit them.

Did you know?

A squash leaves no merge behind

A squash merge is a merge in effect only. The git merge manual says --squash produces the desk and the tray as if a real merge happened, but does not make a commit, move HEAD or record MERGE_HEAD. The commit you make next is an ordinary one, with a single parent, whose effect is the same as merging the other branch.

Why it fits here: It answers this topic's question: --squash makes no merge commit; the next commit has one parent but all the branch's changes.

Source: git manual: git-merge

Rebase and merge adds no merge commit

GitHub's third method keeps every commit but no merge commit. Its documentation says that with Rebase and merge, all commits from the topic branch are added onto the base branch one by one, without a merge commit. The history of main then reads as one straight line, with each of the branch's commits in it.

Why it fits here: It completes this topic's three methods: Rebase and merge puts the branch's commits onto main one by one, with no merge commit.

Source: GitHub Docs: About merge methods on GitHub

In one line

An approved pull request is merged into main, on GitHub by a button with three methods. In git, it is a merge with --no-ff (a merge commit), or --squash and a commit of your own; then main is pushed.

After the merge: tidying up

At a glance

After the merge, the topic branch has done its job, but its name is still in three places: on the remote, in your remote-tracking names, and in your own branches.

Delete it on the remote with git push origin --delete and its name. Remove remote-tracking names that point nowhere with git fetch --prune. Then delete your own copy with git branch -d, which agrees once main holds its work. A tidy list of branches shows at a glance what is still in progress.

Take a guess: Can a branch deleted on GitHub be brought back?

Once a pull request is merged, its branch has done its work: every commit of it is on main. GitHub offers a Delete branch button on the merged pull request's page. From git, the same is a push that deletes:

In the same setup, where the shop has merged Corin's branch into main with --no-ff and pushed main

git push origin --delete pump-schedule 2>&1 | cat
To ../hill.git
 - [deleted]         pump-schedule

Other copies still have their origin/pump-schedule, as nobody has told them. A fetch with --prune removes each remote-tracking branch whose branch is gone from the remote. In Corin's clone:

In the same setup, where the shop has merged Corin's branch into main with --no-ff and pushed main

git push -q origin --delete pump-schedule
cd ../corin
git fetch --prune 2>&1 | cat
git branch -vv
From /home/asha/shop/../hill
 - [deleted]         (none)     -> origin/pump-schedule
   aba517b..78632b8  main       -> origin/main
  main          aba517b [origin/main: behind 2] First orders
* pump-schedule 2e26e06 [origin/pump-schedule: gone] Add the pump schedule

Two things to read in git branch -vv. Corin's pump-schedule follows a branch that is gone. And his main is behind by 2: the pull request's commit and the merge commit. Corin switches to main, pulls, and deletes his branch, which git now accepts, as its work is on main:

In the same setup, where the shop has merged Corin's branch into main with --no-ff and pushed main

git push -q origin --delete pump-schedule
cd ../corin
git fetch -q --prune
git switch -q main
git pull -q
git branch -d pump-schedule
git branch -a
Deleted branch pump-schedule (was 2e26e06).
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
To removeRun
the branch on the remotegit push origin --delete name, or GitHub's Delete branch button
remote-tracking branches whose branch is gonegit fetch --prune, or git remote prune origin
your own copy of the branchgit branch -d name, once main has its work

Worked example 6

The pump schedule is merged. Tidy up: remove the branch from the hill, and check that the hill keeps only main.

What we have: main merged and pushed; the branch pump-schedule still on the hill

What we need: the hill with main alone

The plan: push with --delete; then list the hill's branches with git -C

In the same setup, where the shop has merged Corin's branch into main with --no-ff and pushed main

git push -q origin --delete pump-schedule
git -C ../hill.git branch
* main
  1. Only main is left on the hill. The branch's commits are safe: they are part of main's history now.

The hill keeps main alone.

Check: git log --oneline on main still shows Add the pump schedule.

A path that fails: deleting the branch before the pull request is merged. Its work would then be on no branch of the hill, and the pull request would be closed.

When you are stuck: old branches pile up

git branch -vv marks branches whose remote branch is gone with gone.

git fetch --prune removes the stale origin/... names.

If git branch -d refuses, the branch has work that git finds nowhere else: not on its upstream, or, when the upstream is gone, not on your current branch. Pull main first.

Your turn

After a pull request is merged, are the branch's commits lost when the branch is deleted?Tap to reveal
  1. Where are they now?

No: they are part of main's history; only the name goes.

git push -q origin --delete pump-schedule
git -C ../hill.git branch
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
* main
  1. The push deleted the branch on the hill.
  2. main remains.

It prints * main.

Which fetch option removes origin/... names whose branch is gone from the remote?Tap to reveal
  1. It prunes.

--prune, or -p.

Did you know?

Branches that delete themselves

The first tidying step can be left to GitHub. Its documentation says a repository can have head branches automatically deleted after their pull requests are merged, a setting that anyone with admin permission can switch on or off. With it on, only the steps on your own machine are left: git fetch --prune and git branch -d.

Why it fits here: It adds to this topic's tidying: GitHub can delete a merged pull request's branch on the remote by itself.

Source: GitHub Docs: Managing the automatic deletion of branches

Restore branch

A branch deleted on GitHub can come back. Its documentation says you can restore the head branch of a closed pull request. Open the pull request from the list of closed ones, and near the bottom of its page, click Restore branch.

Why it fits here: It answers this topic's question: a closed pull request's page has a Restore branch button.

Source: GitHub Docs: Deleting and restoring branches in a pull request

In one line

After a merge, delete the topic branch on the remote with git push origin --delete, remove stale remote-tracking names with git fetch --prune, and delete your own copy with git branch -d once main has its work.

Where it is used

Pull requests are how most teams, and most open-source projects, change their code. Here is where each idea of this lesson does its work.

What you learnedWhere it is usedA real example
a topic branch for each changeevery change to a shared projecta branch fix-login for one bug
a pull requestasking for a change to be read and mergeda new feature proposed to a team
the three-dot diffwhat reviewers read on Files changedchecking that a pull request holds only its change
reviews and more commitsimproving a change before it landsa reviewer asks for a test; the author adds it
bringing main into the branchkeeping a long pull request currentsettling a conflict before the merge
merge commit or squasha project's rule for its historyone commit per pull request, by squash
deleting merged brancheskeeping the branch list shortGitHub's Delete branch button after each merge

Many teams protect main on GitHub, so that a pull request cannot be merged until enough reviewers approve it and its tests pass. The rule in the story, that nobody changes the shared drawer alone, is then kept by the service itself.

Mistakes and debugging

These are the slips that people meet most often with pull requests. Every output below is the real one, from the code shown.

1. Committing the change to main

In the shop's repository, whose main is pushed to origin, ../hill.git, with Corin's clone of it in ../corin

cd ../corin
printf 'pump 7 at dawn\n' > pumps.txt
git add pumps.txt
git commit -q -m 'Add the pump schedule'
git status -sb
## main...origin/main [ahead 1]

The commit is on Corin's main, which follows the shared main. Pushing it would change main without a review. The fix: do not push main. Make the branch where you are, with git switch -c pump-schedule, which keeps the commit, and push the branch; lesson 07's git reset moves your main back.

2. Reviewing before fetching

In the same setup, where Corin has pushed a branch pump-schedule (one commit: pumps.txt, pump 7 at dawn); the shop has not fetched it yet

git log --oneline main..origin/pump-schedule
echo "[$?]"
fatal: ambiguous argument 'main..origin/pump-schedule': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
[128]

The shop has never heard of the branch: origin/pump-schedule appears only after a fetch. The fix: git fetch first, every time you review.

3. Deleting a branch before its merge

Asha made a local copy of Corin's branch to review it. Before the merge, she deletes it:

In the same setup, where the shop has fetched Corin's branch, origin/pump-schedule

git switch -q pump-schedule
git switch -q main
git branch -d pump-schedule
warning: deleting branch 'pump-schedule' that has been merged to
         'refs/remotes/origin/pump-schedule', but not yet merged to HEAD
Deleted branch pump-schedule (was 2e26e06).

You may expect a refusal, as in lesson 03, since main lacks the work. But git branch -d first checks a branch against its upstream, when it has one. Here the upstream is origin/pump-schedule, and it holds every commit, so nothing is lost. Git deletes the copy, and warns that main does not have the work yet.

Corin's own branch is different when it holds an answer that he has not pushed. Neither the upstream nor main has that commit:

In the same setup, where Corin has pushed a branch pump-schedule (one commit: pumps.txt, pump 7 at dawn); the shop has not fetched it yet

cd ../corin
printf 'pump 7 at six\n' > pumps.txt
git commit -q -am 'Pump 7 at six, as the review asked'
git switch -q main
git branch -d pump-schedule
error: the branch 'pump-schedule' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D pump-schedule'
hint: Disable this message with "git config set advice.forceDeleteBranch false"

Git refuses, because deleting the branch would lose the answer. The fix: push the work first, and delete a topic branch only after its pull request is merged. Never reach for -D here: it would throw the answer away.

4. Merging with a fast-forward

In the same setup, where the shop has fetched Corin's branch, origin/pump-schedule

git merge -q origin/pump-schedule
git log --oneline --graph
* 2e26e06 (HEAD -> main, origin/pump-schedule) Add the pump schedule
* aba517b (origin/main, origin/HEAD) First orders

The merge worked, but the history is one straight line: nothing shows that a pull request joined here. The fix: merge with --no-ff, as GitHub's default button does.

5. Answering a review without pushing

In the same setup, where Corin has pushed a branch pump-schedule (one commit: pumps.txt, pump 7 at dawn); the shop has not fetched it yet

cd ../corin
printf 'pump 7 at six\n' > pumps.txt
git commit -q -am 'Pump 7 at six, as the review asked'
git status -sb
## pump-schedule...origin/pump-schedule [ahead 1]

The answer is committed, but only in Corin's clone: the pull request shows nothing new. The fix: git push. Look for ahead before telling the reviewer that it is done.

The debugging corner: what does the pull request hold?

Fetch first; then git log --oneline main..origin/branch lists its commits, git diff main...origin/branch shows its changes as Files changed does, and git status -sb on the author's side says whether everything is pushed.

Quick card

The whole lesson on one card. Come back to it whenever you need a reminder.

The lesson in code

cd ../corin
git switch -q -c pump-schedule                  # one branch, one change
printf 'pump 7 at dawn\n' > pumps.txt
git add pumps.txt
git commit -q -m 'Add the pump schedule'
git push -q -u origin pump-schedule             # pushed for review
cd ../shop
git fetch -q
git diff --stat main...origin/pump-schedule     # what it introduces
git merge -q --no-ff --no-edit origin/pump-schedule
git push -q                                     # merged into main
git push -q origin --delete pump-schedule       # tidied away
git log --oneline --graph
 pumps.txt | 1 +
 1 file changed, 1 insertion(+)
*   6b66437 (HEAD -> main, origin/main, origin/HEAD) Merge remote-tracking branch 'origin/pump-schedule'
|\  
| * 2e26e06 Add the pump schedule
|/  
* aba517b First orders
When you seeIt meansDo
ambiguous argument 'main..origin/branch'the branch was never fetchedgit fetch
[ahead 1] on a topic branchan answer not pushed yetgit push
non-fast-forward after an amendyour branch was rewrittengit push --force-with-lease, on your own branch
(stale info)someone pushed to the branch since your fetchfetch and look first
Squash commit -- not updating HEADa squash waits on the traygit commit
not fully mergedthe branch has work that is not on its upstream (with no upstream: not on your branch)push or merge first
but not yet merged to HEADthe upstream has the work, your branch does notnothing is lost; the copy was deleted
[origin/branch: gone]the branch was deleted on the remotegit branch -d after pulling main
(none) -> origin/brancha fetch with --prune removed itnothing to fix

The story: Asking before changing

You file Corin's change on a line of its own and send that line up the hill. Then you open a request: these pictures, this reason, please read and join them. Two engineers read it and leave notes on two lines; Corin answers, fixes one, and explains the other. When both approve, the clerk joins the line to the main drawer.

The schedule that goes in is better than the one Corin carried up the hill.

Corin admits it, which surprises everyone. The Keeper writes the request's number in her book.

Reagents gathered: pull request, review, approve, fork, merge, comment.

Sources