How to Merge Two Branches on Git?

Git is a version control software which is used to analyze changes in the source code during software development. It can be used to track changes in any set of files and it provides speed, integrity, and support for non-linear workflows. In this article, we will teach you the way to merge two branches or to merge a forked repository and an Upstream repository.

Git logo

How to Merge Two Branches on Git?

Git provides an option to form a copy of any upstream repository and make changes to it, without having any effect on the main repository. This creates a branch of the upstream repository and separates it from the original one. However, once the code has been typed and practiced, it can be merged with the original repository and the changes made can be saved. Therefore, in the steps below, we will indicate the method to merge two branches on Git. For that:

  1. Open the Terminal if you are on Linux or Mac and open the GitBash if you are on Windows.
    Opening the GitBash on Windows
  2. Change the current directory of working to your local project.
  3. In this step, we will be “checking out” the branch to which we want the other branch to merge. In your case, it should be the “master” branch. In order to check it out, type in the following command and press “Enter“.
    $ git checkout master
    performing Git Checkout Command on the branch to be merged to
  4. It is important that we pull the desired branch from the upstream repository. We will be doing so while retaining the commit history without any modifications. In order to do that, type in the following command and press “Enter“.
    $ git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git BRANCH_NAME
    Executing Git Pull Command in the terminal
  5. If there are any conflicts during the merge, consult this page to resolve them.
  6. Commit the merge and review the changes that have been made to ensure that they are satisfactory.
  7. In order to push the merge to your GitHub repository, type in the following command and press “Enter” to execute.
    $ git push origin master
Kevin Arrows
Kevin is a dynamic and self-motivated information technology professional, with a Thorough knowledge of all facets pertaining to network infrastructure design, implementation and administration. Superior record of delivering simultaneous large-scale mission critical projects on time and under budget.