Today we will learn about Git Branch and Merge between different branches. Let’s start by understanding Git first. Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Branching is a very powerful feature in Git. Git branches are effectively a pointer to a snapshot of our changes. Every time we commit, the pointer to a snapshot moves forward automatically.
The main advantage of Git branching is that we can work on an independent feature without affecting the core system. After completion of the feature, we can then test the feature and merge it with the core system.
The most common uses of branching are to add a new feature, fix bugs, or code refactoring. First of all, a new branch is created from master; let’s say to create a new feature. Inside the branch, a new feature is added. When the addition of a new feature is complete, it is then committed and merged to the master. The process is the same for fixing bugs or refactoring.
First of all, let’s check the available branches on our system. For this, simply run the following command inside your project root directory. The
master indicates the current branch we are in.
$ git branch * master
Please note that Git treats everything as a branch. When Git is initialized, git a master branch is automatically from where other branches are created.
Suppose, we are working on a large system and we need to add a new feature. For this, we can create a new branch with the following command and start working on the new feature.
// create new branch $ git branch new-feature // check available branches $ git branch * master new-feature // switch to new branch $ git checkout new-feature Switched to branch 'new-feature'
Alternatively, we can create and switched to new branch at the same time. For this, run the following command.
$ git checkout -b new-feature Switched to a new branch 'new-feature'
Now, it’s time to code. Yes, code. We work on new feature making some changes. After you finished working on the new feature, we need to commit them to save the changes. For this, run the following command:
$ git commit -a -m 'New feature added'
After finishing adding new feature, we can then merge the branch to master. At this point of time, we are one commit ahead of our master branch if no one has merge the commit on the master branch. Now, we will merge our latest change about new feature with the master branch. For this, we need to checkout to master and merge the branch. Simply, run the following code:
$ git merge new-feature Merge made by the 'recursive' strategy. index.html | 1 + 1 file changed, 1 insertion(+)
We need to be careful when merging branches with the master. When the master contains updated code after our last branching, we may encounter conflicts. For this, each conflict should be solved by going through each file.
After successful merge of the branch with master, we can delete the new-feature branch by running the following code:
$ git branch -d new-feature
This is a cycle of git branching. If you have any queries or face any issue regarding this topic, don’t forget to drop a comment below. Till then, happy coding 🙂