The basic way to merge a branch

Merging a branch means taking the code changes from one branch and combining them into another branch, usually your main branch. The most common way to do this on GitHub is through a pull request, which lets you review changes before they go live.

To start, go to your repository on GitHub.com and click the "Pull requests" tab. Click "New pull request". Choose the branch you want to merge from (the "compare" branch) and the branch you want to merge into (the "base" branch). GitHub will show you what changed. Click "Create pull request", add a title and description, then click "Create pull request" again.

Once the pull request is open, you can review the code changes, add comments, and request changes from other people. When everything looks good, click the green "Merge pull request" button. GitHub will combine the branches and close the pull request automatically.

Key Takeaways

  • A pull request is the standard way to merge branches on GitHub because it lets you review changes before combining code.
  • You choose a base branch (where changes go) and a compare branch (where changes come from) when you create a pull request.
  • GitHub shows you exactly what code changed and lets you add comments or request changes before merging.
  • You can merge directly from the command line using git merge if you are working locally, but pull requests are safer for team projects.
  • After merging, you can delete the old branch to keep your repository clean.

What happens when you merge

When you click "Merge pull request", GitHub combines all the commits from your compare branch into your base branch. The commits stay in the history, so you can see exactly what changed and when. GitHub creates a new merge commit that records the merge itself.

If the two branches changed the same lines of code in different ways, GitHub will flag a merge conflict. This means you have to decide which version of the code to keep. GitHub shows you the conflicting lines and lets you choose right in the browser, or you can fix conflicts on your computer using the command line.

Merging from the command line

If you are working on your computer, you can merge branches without using GitHub's website. First, make sure you are on the branch you want to merge into by typing git checkout main (or whatever your base branch is called). Then type git merge branch-name, replacing "branch-name" with the name of the branch you want to merge.

Git will combine the branches locally on your computer. If there are no conflicts, the merge is done. If there are conflicts, Git will tell you which files have problems. Open those files, find the conflicting sections (Git marks them with <<<<<<< and >>>>>>>), choose which code to keep, and save the file. Then type git add . and git commit -m "Merge branch-name" to finish.

After merging locally, type git push origin main to send your merged code back to GitHub.

Different merge strategies

GitHub offers three ways to merge a pull request, and each one creates a different history. The default is "Create a merge commit", which keeps all the individual commits from your branch and adds a new commit that records the merge. This is the clearest way to see what happened.

"Squash and merge" combines all the commits from your branch into a single commit before merging. This keeps your main branch history cleaner if you made many small commits. "Rebase and merge" replays your commits on top of the base branch without creating a merge commit. This creates a straight line of history but is harder to understand if you are new to Git.

For most projects, "Create a merge commit" is the safest choice. Ask your team which strategy they prefer before you start merging.

Handling merge conflicts

A merge conflict happens when the same lines of code changed differently in two branches. GitHub shows you the conflicting files in the pull request. Click "Resolve conflicts" to fix them in the browser.

GitHub highlights the conflicting sections. You will see your code, the incoming code, and options to choose one or the other, or to edit the section manually. Delete the conflict markers (<<<<<<<, =======, >>>>>>>) and keep only the code you want. Click "Mark as resolved" when you are done, then commit the resolution.

If the conflict is complicated, you can also fix it on your computer. Close the pull request, merge locally, resolve the conflicts in your text editor, and push the fixed code back to GitHub. The pull request will update automatically.

Deleting a branch after merging

After you merge a branch, you usually do not need it anymore. GitHub offers to delete the branch right after you merge. Click the "Delete branch" button to remove it from your repository.

If you did not delete it right away, you can delete it later. Go to the "Code" tab, find the branch in the list, and click the trash icon next to it. Deleting a branch does not erase the commits — they stay in the merged branch's history. You can always restore a deleted branch if you need to.

Protecting your main branch

Most teams set up branch protection rules so that nobody can merge code into main without approval. Go to your repository settings, click "Branches", and click "Add rule". Type "main" as the branch name pattern. Check "Require a pull request before merging" and "Require approvals". Now every merge to main needs a pull request and at least one person to review it.

You can also require that all tests pass before merging, or that your branch is up to date with main. These rules prevent broken code from reaching your main branch.

Frequently Asked Questions

What is the difference between merge and rebase?

Merge combines two branches and keeps both histories visible. Rebase replays one branch's commits on top of another, creating a cleaner but less detailed history. For most teams, merge is safer because it preserves the full story of what happened.

Can I undo a merge after I have already done it?

Yes. On GitHub, click the "Revert" button on the merged pull request to create a new commit that undoes the merge. Or use git revert -m 1 commit-hash on the command line, replacing "commit-hash" with the merge commit's ID. This creates a new commit that reverses the changes instead of deleting history.

What does "fast-forward merge" mean?

A fast-forward merge happens when the base branch has not changed since you created your compare branch. Git simply moves the base branch forward to match the compare branch without creating a merge commit. GitHub's "Rebase and merge" option does this automatically.

Do I have to use pull requests, or can I merge directly?

You can merge directly using the command line, but pull requests are better for team projects because they let people review code first. If you are working alone, direct merging is faster. Most teams require pull requests for the main branch to catch mistakes early.

What if my branch is behind the main branch?

If main has new commits that your branch does not have, GitHub will tell you your branch is "behind". Click "Update branch" in the pull request to merge the latest main code into your branch. This prevents conflicts and makes sure your code works with the newest version.