Merging on GitHub
Last updated
A pull request has been reviewed and approved. Now someone presses Merge, and sometimes GitHub says it can't. In this lesson you'll read GitHub's merge box, settle a simple conflict in GitHub's own editor, tidy up the branch afterwards, and undo a merged pull request with Revert.
By the end of this lesson you can
- Read the merge box: reviews, checks, and whether the branch has conflicts
- Settle a simple conflict in GitHub's conflict editor, and know when to do it on your computer instead
- Delete a merged branch, and undo a merged pull request with Revert
The merge box
At the bottom of a pull request's Conversation tab is the merge box. It says what's standing between the pull request and main:
- Reviews. Who approved or asked for changes (lesson 5.3). A branch rule can require approvals before merging.
- Checks. Automated checks, such as tests, that ran on the branch. Simple Software Development covers setting them up.
- Conflicts. Whether the branch can be merged into main without a clash. Step 2 is what to do when it can’t.
- Merge button. The arrow picks one of the three ways to merge from lesson 5.2: a merge commit, squash, or rebase.
Merging happens on GitHub. Your computer's main doesn't have the merge until you git switch main and git pull.
This branch has conflicts
If main changed the same lines as the pull request since the branch started, the merge box says This branch has conflicts that must be resolved. For simple conflicts in text files, the Resolve conflicts button opens GitHub's editor, showing the same markers as lesson 3.4:
- File and count. Each file with conflicts, and how many.
- Markers. The pull request’s version above
=======, main’s below. Edit the text to what it should be, and delete all three marker lines. - Mark as resolved, then Commit merge. GitHub makes a merge commit on the pull request’s branch, merging main into it. The pull request can then be merged.
The editor only handles simple cases. Settle the conflict on your computer instead (fetch, switch to the branch, merge main, settle, commit, push, as in lesson 5.P) when:
- the conflict is big, or in many files;
- a file was deleted on one side, or renamed;
- you need to test the result before it goes in, such as running a script.
If you can't push to the branch, as with someone else's fork, ask its author to settle it.
After merging
Once a pull request is merged, its branch has done its job. GitHub shows a Delete branch button: press it. If you need it back, Restore branch appears in the same place.
On your computer:
git switch main
git pull
git branch -d adobo-salt
git fetch --prunegit branch -d deletes your local copy of the branch (it refuses if the work isn't merged); --prune removes origin/adobo-salt, which no longer exists on GitHub. Start the next piece of work on a new branch from the updated main, as lesson 5.2's trap explained.
Undoing a merge
A merged pull request turns out to be wrong: Carla's kare-kare used the wrong oxtail weight, and the family cooked it. A merged pull request has a Revert button. It doesn't erase anything. It makes a new branch and a new pull request containing a commit that undoes the merge's changes, the same as git revert (lesson 2.5), and you review and merge that like any other.
History keeps both: the original merge and its revert. That's what you want on a shared branch, because others have already pulled the merge. Rewriting main to pretend it never happened would break their copies (lesson 6.3).
To fix the recipe properly, open a new pull request with the right amount. Reverting the revert is possible, but confusing to read later.
Spot the trap: merging your own
Joaquin has write access to the family’s Kusina. He opens a pull request changing the lumpia, and, since nobody has replied after ten minutes, merges it himself.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- GitHub Docs: Merging a pull request: the merge box and its buttons
- GitHub Docs: Resolving a merge conflict on GitHub: the conflict editor, and which conflicts it can handle
- GitHub Docs: Deleting and restoring branches in a pull request: Delete branch, and Restore branch
- GitHub Docs: Reverting a pull request: the Revert button makes a new pull request
- Git documentation: git-revert: what Revert does underneath, including -m for merge commits