GitHub

How to Merge Branches in GitHub

How to Merge Branches in GitHub

Your feature branch is ready. The code works. Now what?

Learning how to merge branches in GitHub is the skill that separates solo coders from effective team contributors.

Every pull request, every code integration, every production deployment depends on understanding what git merge does and when to use it.

This guide walks you through the complete merge process in 5 steps.

You will learn how to create pull requests, review changes, choose the right merge strategy, and handle conflicts when they appear.

Whether you are managing a Git workflow for a small project or coordinating branch management across distributed teams, these methods apply.

How to Merge Branches in GitHub

maxresdefault How to Merge Branches in GitHub

Merging branches in GitHub is the process of combining code changes from one branch into another using pull requests or Git commands.

Teams need this when finishing feature development, fixing bugs, or integrating updates into the main codebase.

This guide covers 5 steps requiring 2-5 minutes and basic familiarity with Git and repository navigation.

Prerequisites

Before you start, confirm you have these requirements ready:

Why is GitHub the heart of open source?

Uncover GitHub statistics: developer community growth, repository trends, collaboration patterns, and the platform that powers modern software development.

Explore GitHub Data →
  • GitHub account with repository access (free or paid tier)
  • Git version 2.40+ installed locally (for command line method)
  • A repository containing at least two branches
  • Write permissions to the target branch
  • Committed changes on your feature branch

Time estimate: 2-5 minutes depending on merge complexity and method chosen.

Skill level: Beginner to Intermediate.

Step 1: How Do You Access the Repository and Navigate to Branches?

Open your GitHub repository in a browser, click the branch dropdown above the file list, then select “View all branches” to see every available branch and its status.

Action

  1. Repository page: Navigate to github.com/username/repository-name
  2. Branch selector: Click the dropdown showing “main” or your current branch name
  3. Branch list: Select “View all branches” at the bottom of the dropdown

Purpose

This step confirms your source and target branches exist, shows their last commit dates, and reveals any existing pull requests.

The branch overview displays ahead/behind counts, helping you understand how much divergence exists before merging.

Step 2: How Do You Create a Pull Request for Your Branch?

Click “New pull request” or the “Compare & pull request” button next to your branch, select the base branch (where changes go) and compare branch (your feature branch), then fill in the title and description.

Action

  1. Pull Requests tab: Click “Pull requests” in the repository navigation
  2. New pull request: Click the green “New pull request” button
  3. Branch selection: Set base to “main” and compare to your feature branch
  4. Create: Click “Create pull request” after adding title and description

Purpose

Pull requests enable code review before integration.

They document what changes are being merged and why, creating a permanent record in your commit history.

Step 3: How Do You Review the Changes Before Merging?

Click the “Files changed” tab to view the diff, check each modified file for accuracy, and verify the changes match your intended updates before approving the merge.

Action

  1. Files changed tab: Click to see all modifications in diff format
  2. Line-by-line review: Green highlights show additions; red shows deletions
  3. Comments: Click the “+” icon on any line to add review comments

Purpose

Reviewing changes catches errors before they reach the main branch.

This step prevents bugs, maintains code quality, and ensures the merge aligns with version control best practices.

Step 4: How Do You Complete the Merge Process?

Click the green “Merge pull request” button at the bottom of the pull request page, confirm the merge commit message, then click “Confirm merge” to integrate all changes into the base branch.

Action

  1. Merge button: Scroll to the bottom of the pull request conversation tab
  2. Merge type dropdown: Click the arrow next to “Merge pull request” to select merge strategy
  3. Confirm merge: Click the confirmation button after reviewing the commit message

Purpose

Completing the merge writes all feature branch changes into your target branch permanently.

The commit hash created serves as a reference point in your revision history.

Step 5: How Do You Delete the Merged Branch?

After merging, click the “Delete branch” button that appears on the pull request page, or navigate to the branches list and click the trash icon next to the merged branch name.

Action

  1. Post-merge prompt: Click “Delete branch” on the merged pull request page
  2. Manual deletion: Go to Code > Branches > click trash icon next to branch name
  3. Restore option: Click “Restore branch” if deleted accidentally

Purpose

Removing merged branches keeps your repository clean and reduces confusion during team collaboration.

Learn more about deleting branches in GitHub for advanced cleanup options.

Alternative Methods

Command Line Merge

Open your terminal, run git checkout main to switch branches, then execute git merge feature-branch to combine changes locally before pushing.

Steps:

  1. git checkout main
  2. git pull origin main
  3. git merge feature-branch
  4. git push origin main

Merge with Rebase

Git rebase rewrites commit history by placing your changes on top of the target branch, creating a linear history without merge commits.

Use rebase for cleaner history; use standard merge when preserving the exact branch timeline matters.

Merge Type Options

GitHub offers three merge strategies. Each affects your commit history differently.

Create a Merge Commit

Preserves all commits from both branches plus adds a new merge commit.

Best for: tracking complete feature development history.

Squash and Merge

Combines all feature branch commits into one commit on the target branch using git squash.

Best for: keeping main branch history clean and readable.

Rebase and Merge

Replays each commit from the feature branch onto the base branch individually.

Best for: linear commit history without merge commits.

Verification

Confirm your merge succeeded with these checks:

  • Pull request status: Shows “Merged” with purple icon
  • Branch comparison: Target branch shows 0 commits behind source
  • Commit history: Check git log or GitHub’s commit tab for merge record
  • File verification: Browse target branch files to confirm changes appear

Run git status locally after pulling to verify your local copy matches the remote repository.

Troubleshooting

Merge Conflicts Prevent Completion

Issue: GitHub displays “This branch has conflicts that must be resolved.”

Solution: Click “Resolve conflicts” in the web editor, or pull locally and run git merge to fix conflicts in your IDE.

Detailed steps available in resolving GitHub conflicts.

Permission Denied Error

Issue: “You don’t have permission to merge this pull request.”

Solution: Check Settings > Branches > Branch protection rules; request write access from repository admin.

Branch Not Visible in Dropdown

Issue: Your feature branch doesn’t appear in the branch selector.

Solution: Verify you committed changes and pushed to origin; refresh the page; check branch name spelling.

Next Steps

After completing your merge:

Understanding source control management principles helps teams maintain cleaner repositories and faster development workflows.

FAQ on How To Merge Branches In GitHub

What is the difference between merge and rebase in GitHub?

Merge combines branches by creating a new merge commit that preserves both histories.

Rebase rewrites commit history by moving your changes on top of the target branch, creating a linear timeline without merge commits.

Can I merge branches without a pull request?

Yes. Use git checkout to switch to your target branch, then run the merge command directly.

Pull requests add code review and documentation but are not required for merging.

How do I undo a merge in GitHub?

Use git revert to create a new commit that reverses the merge changes.

For unpushed merges, git reset can remove the merge commit entirely from your local history.

What causes merge conflicts?

Conflicts occur when two branches modify the same lines in a file differently.

Git cannot automatically determine which version to keep, so it requires manual resolution before completing the merge.

Should I delete a branch after merging?

Yes. Deleting merged branches reduces clutter and prevents confusion about which branches are active.

GitHub offers a restore option if you delete a branch accidentally.

What is a fast-forward merge?

A fast-forward merge occurs when the target branch has no new commits since branching.

Git simply moves the branch pointer forward without creating a merge commit, resulting in linear history.

How do I merge a specific commit instead of a whole branch?

Use git cherry-pick followed by the specific commit hash to apply individual commits to your current branch.

This copies the commit without merging the entire branch history.

Can I merge branches from different repositories?

Yes. Add the other repository as a remote, fetch its branches, then merge using standard commands.

This is common when integrating changes from upstream repositories into forks.

What happens to commit history after merging?

Standard merge preserves all commits from both branches plus adds a merge commit.

Squash merge combines all feature commits into one. Rebase replays commits individually onto the target branch.

How do I prevent unauthorized merges to the main branch?

Configure branch protection rules in repository Settings > Branches.

Require pull request reviews, status checks, and restrict who can push directly to protected branches.

Conclusion

Knowing how to merge branches in GitHub is a core skill for any developer working with distributed teams or managing code integration.

You now have the steps to create pull requests, choose merge strategies, and resolve conflicts when they happen.

The difference between squash, rebase, and standard merge commits matters. Pick the one that fits your team’s workflow.

Clean branch management keeps your repository organized. Delete merged branches. Use protection rules on main.

These practices connect directly to DevOps workflows and continuous deployment pipelines.

Start with the GitHub web interface. Move to command line when you need more control.

Master this process and collaboration between development teams becomes seamless.

50218a090dd169a5399b03ee399b27df17d94bb940d98ae3f8daff6c978743c5?s=250&d=mm&r=g How to Merge Branches in GitHub

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.