How to Merge Branches in GitHub

How to Merge Branches in GitHub

When working on open source projects in GitHub, developers must be able to test out new code changes without affecting the main project.

To do so, they can create a new branch and implement their code changes. They can then commit each change to their local repository and include a commit message to convey what each change does.

When they’re done, they’ll submit a pull request to the project maintainer on GitHub. This is essentially a request to implement changes into the main branch.

Once you look over the code and determine it's good to go, you’ll have to merge their branch into the master branch.

Below, we’ll explain how to merge branches in GitHub. But first, let’s talk a little more about pull requests.

About pull requests

As mentioned, a pull request occurs when a developer proposes that the changes they made to the code on their local branch are ready to be implemented into the main branch.

All the developer has to do is navigate to the local branch in GitHub they want to merge, then click New pull request. From there, they can enter a few details before submitting the request.

You’ll then look over the code changes and ask the contributor any questions you have. If you need them to make changes, you can send it back to them.

Once you and the contributor have ironed things out, you can proceed with merging the pull request into your main branch.

How to merge branches in GitHub

Now, let’s discuss how to merge GitHub branches. It’s relatively simple to do:

  1. Navigate to your repository, then find and click the Pull requests button. It should be in between the Issues and Actions buttons. You’ll see a summary of all pull requests you have pending.
  2. Navigate to and click on the pull request you’d like to merge into the main branch.
  3. Now, you have a few choices for initiating the merge, depending on your repository’s merge options:
    - Merge every commit into your main branch:
    To do this, click Merge pull request. If this button does not appear, open the dropdown menu and click on Create a merge commit.
    - Combine the commits into one large commit:
    You can “squash” your commits together by clicking the dropdown menu, then choosing Squash and merge and clicking the new Squash and merge button. Doing this helps you create a cleaner, more streamlined Git history for your repository.
    - Rebase each commit onto the main branch:
    To do this, click the dropdown menu, choose Rebase and merge, and click the new Rebase and merge button. This also creates a cleaner project history.
  4. You can now leave a comment if you’d like, or you can accept the default message GitHub provides you.
  5. Scroll below the commit message field to choose a Git author email address.
  6. Click on Confirm merge, Confirm squash and merge, or Confirm rebase and merge, depending on which option you choose in Step 3.
  7. If the merge occurs successfully, GitHub will display a note stating so. This helps confirm that you’re good to go.
  8. If you’d like, you can now delete the branch by clicking Delete branch to keep things nice and neat. Alternatively, you can set it up so that the branch deletes automatically once you complete a merge to save some time.
The merge button

Merge branches faster and more efficiently

Merging individual pull requests doesn’t take all that long.

But those pull requests add up when you have to manage several every day. Some might be good to go, others may need a little extra review, and still others might necessitate sending them back to the contributor for fixes.

Fortunately, you can save plenty of time by automating many of your pull request merges.

That’s where Mergify comes in.

Mergify lets you prioritize your pull requests based on whatever criteria you desire by writing a few rules. Once you do that, Mergify’s bot simply checks the new code against your rules and merges it into the main repository if it looks good.

Open-source projects are 100% free on Mergify. Sign up today, connect your GitHub account, and automate your pull request merging.

Ready to automate your GitHub workflow?
We save your time by prioritizing and automatically merging, commenting, rebasing, updating, labeling, backporting, closing, assigning, your pull requests.
We don't send spams nor sell your email address. We're good guys. One-click unsubscribe at any time.