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.
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:
- Open the Terminal if you are on Linux or Mac and open the GitBash if you are on Windows.
- Change the current directory of working to your local project.
- 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
- 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
- If there are any conflicts during the merge, consult this page to resolve them.
- Commit the merge and review the changes that have been made to ensure that they are satisfactory.
- In order to push the merge to your GitHub repository, type in the following command and press “Enter” to execute.
$ git push origin master