The creation of the top branch happened upon completion of three commits in the master branch ( the green circles). It is of high importance to note that when a new branch creates in Git, it inherits the commits from that branch. In this example, to demonstrate, we have used the master branch. Once done, we can now successfully delete the branch ( Refer Deleting Branches in Git).Ī branch can merge into any branch that the user wants. If there were, the head pointer would move to that commit. By the term same position, it means that no further commits happen after the merging. The name of these two branches depicts that the second branch merges into the first branch, i.e., the dev branch merges into the master branch, and both of them are in the same position. In this case, it is at two branches simultaneously. The Head is a pointer that tells us where the HEAD currently is. The highlighted line starts with ( HEAD -> master, dev). The output will show a successful merge along with the file name, i.e., toolsqa.txt. Now switch to the branch to which we will merge the changes ( master in this example).Įxecute the following command to merge the branch dev to the branch master. Perform Git Log operation to check the commit using oneline flag ( Refer Git Log).
( Create a file if there is none).Ĭlose the file and commit these changes ( After adding the file to the staging area). Now open a file using notepad and make some changes to the file. For this, let's switch to the branch that we will merge by the following command: Before merging, we need to log some changes into the branch. In this section, we will follow step by step process to merge a branch called " dev" into another branch called the " master" branch.
How to merge branch in Git to another branch? These commits that happened since the branch diverged from the master branch will then log to a single branch, i.e., the top branch in our case. So what does Git do behind the scenes when you type this command? When the command executes, git replays all the commits from the time this branch got diverted, i.e., from time marked as " 1" in the image to the time the branch is merged into the master, i.e., the time marked as " 2". Syntax to merge a branch in GitĪ merge operation can be executed by typing the command A simple merge of two branches is represented pictorially as below:Īs seen in the above image, the top branch merges into the master branch after three commits. Finally, merge it with the master.Ī single repository generally contains multiple branches in Git. The straightforward steps for this would be to create a new branch from production or master or any other branch. It is very urgent and should execute as soon as possible. Let say you are currently working as usual on your software, and suddenly, a new requirement comes up. Merging a branch into the master branch symbolizes that the feature you were developing into that branch is complete and tested and is now ready to be a part of the software.Ī small example will make this clear. We create them so that they can merge after finishing work on them. But, the fate of branches is to get merged. We create Branches to work separately on a feature without disturbing the main stable branch, also known as the master branch. Once you have gone through these prerequisites, you can go ahead and explore the following topics in this tutorial: But, there are a few things you should know before proceeding. This tutorial revolves around the same operation in Git. It is the part we perform before deleting the branches. We merge branch in Git with the master branch ( or any other branch) to depict that the feature addition is successful and ready for release. We do not leave the branches hanging once their work finishes. But can you recall why did we create the branches in the first place? Well, if you think that we create branches to work on different features at different times, you are correct. We learned the basics of branches, how to create them on our local repository, and tried a few operations on them, such as deleting the branches, checking them out, etc. In this section up till now, we have learned a lot about branches.