Merging


Scope

This article introduces the concept of merging branches in Git. At a high level, it explains how merging fits into the basic development process, and how users can perform merges either via GitHub or the Command Line Interface (CLI).

Introduction to Merging in Git

Once you are finishing working on that new feature or fixing a critical bug and all your code has been tested and verified, it is time to merge it back onto the default branch (usually that is either the master or main branch) so that everyone can benefit from it.

Merging involves taking all the commits from the branch you were working on, unifying them, and then combining them with the commits of the branch you are merging to. This incorporates commits from both branches into the receiving branch via a special commit referred to as a merge commit.

NOTE: Originally, the name of the initial default branch for a repository was master, but that was recently changed to main (as shown here in GitHub's documentation). This document refers to both, in case you are exposed to some older repositories still utilizing the master branch as the repository's default branch.

Branches Gif

Conducting a Merge in Git

Below are two ways you can complete a merge in Git, via GitHub and via the CLI.

Merging Using Pull Requests on Github

A pull request is the recommended way of merging two branches together because it allows other users to view, verify, test, and comment on your changes before any merges takes places.

Opening up a pull request allows the developers to get a better insight into the changes you made and can even add more details on the specific set of changes through the assignment of labels, milestones, and specific reviewers.

To open a pull request, simply navigate to the pull requests tab on your repository home page on GitHub.

Opening a pull request on Github {alt="Opening a pull request on Github"}

Then, to configure your pull request

  • Select the base branch you want to merge into (i.e. your master branch)
  • Select the branch you want to merge from (i.e. your feature branch)
  • Then click "Create pull request"

Selecting the branches for the pull request {alt="Selecting the branches for the pull request"}

Once a pull request is created, you and your team will be able to collaborate and discuss the proposed changes by making comments, suggesting edits, and assigning others to conduct a formal code review.

Merging via the CLI

Both branches should be up to date before merging (both the new branch with the feature you are adding and the branch you are merging that into).

To make sure those branches are up to date, switch to the desired branch via git checkout and then run git pull to grab the latest set of changes for that branch. For example:

git checkout <feature_branch_name>

git pull

Switch to the branch you want to merge into (in this example, the master branch):

git checkout master

Then, to merge your changes from your featured branch into your selected branch:

git merge <feature_branch_name>

If everything goes well without any conflicts, you will now have successfully merged both branches!