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 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 Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.