Merging Pull Requests
Learn the different ways to merge PRs and when to use each method.
Merge Options
GitHub offers three merge methods:
1. Create a Merge Commit
Before:
main: A ─ B ─ C
\
feature: D ─ E
After merge commit:
main: A ─ B ─ C ─────── M
\ /
feature: D ─ E ────
Characteristics:
- Preserves complete history
- Creates merge commit (M)
- Shows branch existed
When to use:
- Want full history
- Team prefers seeing branch structure
2. Squash and Merge
Before:
main: A ─ B ─ C
\
feature: D ─ E ─ F
After squash:
main: A ─ B ─ C ─ DEF
Characteristics:
- Combines all commits into one
- Clean, linear history
- Loses individual commit details
When to use:
- Feature had many WIP commits
- Want clean history
- Individual commits aren't important
3. Rebase and Merge
Before:
main: A ─ B ─ C
\
feature: D ─ E
After rebase:
main: A ─ B ─ C ─ D' ─ E'
Characteristics:
- Linear history
- Preserves individual commits
- Rewrites commit hashes
When to use:
- Want linear history
- Each commit is meaningful
- No merge commits needed
How to Merge
Basic Merge
- Open PR
- Verify all checks pass (green ✓)
- Click Merge pull request
- Choose merge method
- Click Confirm merge
- Click Delete branch (optional but recommended)
Merge Requirements
Configure in Settings → Branches:
Branch protection rules:
☑ Require pull request reviews (1 approval)
☑ Require status checks to pass
☑ Require linear history
☑ Include administrators
Handling Merge Conflicts
When Conflicts Occur
GitHub shows: "This branch has conflicts that must be resolved"
Resolve on GitHub
- Click Resolve conflicts
- Edit the file in the web editor
- Remove conflict markers
- Click Mark as resolved
- Click Commit merge
Resolve Locally
bash
1# Update your branch
2git checkout feature-branch
3git pull origin main
4
5# Resolve conflicts in your editor
6# Edit files, remove conflict markers
7
8# Stage resolved files
9git add .
10
11# Complete the merge
12git commit -m "Resolve merge conflicts with main"
13
14# Push updated branch
15git push origin feature-branchAfter Merging
Delete Branch
bash
1# On GitHub: Click "Delete branch" button
2
3# Locally:
4git checkout main
5git pull origin main
6git branch -d feature-branch
7git fetch --pruneVerify Merge
- Check main branch has the changes
- Verify deployment (if auto-deploy enabled)
- Confirm issue is closed (if linked)
Merge Best Practices
1. Always Review Before Merging
Even if you're the only reviewer:
- Check the diff
- Ensure tests pass
- Verify it works
2. Use Consistent Merge Strategy
Pick a strategy and stick with it:
| Team Preference | Strategy |
|---|---|
| Full history | Merge commit |
| Clean history | Squash |
| Linear history | Rebase |
3. Delete Branches After Merge
Keep repository clean:
bash
1# Configure auto-delete on GitHub
2Settings → General → Automatically delete head branches ☑4. Write Good Merge Commit Messages
For squash merges:
Add user authentication (#42)
- Implement login/logout
- Add session management
- Create login form UI
Co-authored-by: Partner <partner@email.com>
5. Don't Force Merge
If checks fail, fix them:
# Bad: Bypassing required checks
# Good: Fix the issue, then merge
Reverting a Merge
If something goes wrong:
Via GitHub
- Go to PR (even after merged)
- Click Revert
- Creates new PR to undo changes
- Merge the revert PR
Via Command Line
bash
1git checkout main
2git revert -m 1 <merge-commit-hash>
3git push origin mainSummary
| Method | History | When to Use |
|---|---|---|
| Merge commit | Complete | Want to see branches |
| Squash | Clean | Many WIP commits |
| Rebase | Linear | Each commit matters |
Choose based on your team's preferences and project needs!
