One of the most powerful Git’s features is the branch creation and merge operation. Git allows users to create a new branch and merge them into the development code. This feature improves the workflow of the development process for various projects by encouraging more focused, smaller, and granular commits.

In most of the legacy version control systems such as CVS in which the difficulty of merging restricted it to advance users. The modern centralized version control system like subversion requires commits to be made on the central repository.  When talking about Git, we have to create a new branch code to add a new feature or bug fix.

In this article, we will show you how to create a new branch, add commits to new features, and merge master with a new branch.

Let’s start the demo of merging two branches. We have executed all commands on CentOS 8 Linux distribution which are mentioned below in detail:

Git branch command

If you want to view the list of all existing branches in a particular repository, then using the ‘git branch’ command, you can easily do this task. An asterisk sign will have appeared on the currently active branch. Type the following command to show all branches list:

The above command will only list branches. Use the following command to create a new branch in a repository.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/branch-merge-operation-git-01.png" data-lazy- height="138" src="data:image/svg xml,” width=”757″>

The ‘git branch new_branch’ will create a new branch in your current Git repository.

You should know that when git creates a new branch, it does not create a new commit set to represent this new branch. In Git a branch behaves like just a tag or a label that you can use to point to a particular commits string. Therefore, using the Git repo, you can create multiple sets of commits from a single base.

Git checkout command

We have created a new branch above by using ‘git branch new_branch’. But, the active branch is the ‘master branch’. To activate the ‘new_branch’, execute the following command on the terminal:

$ git checkout new-branch

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/branch-merge-operation-git-02.png" data-lazy- height="204" src="data:image/svg xml,” width=”766″>

The above-given command will switch from master to the new_branch. Now, you can work on this newly created branch.

Now, you will add some commits or implement a new feature on the ‘new_branch’. In your case, you will add a function or code to a newly-created branch and merge it back into the master or main code branch.

# …develop some function code…


$ git add –A


$ git commit –m “Some commit message for display.”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/branch-merge-operation-git-03.png" data-lazy- height="520" src="data:image/svg xml,” width=”1101″>

Now, you will run the following command to activate the master branch.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/branch-merge-operation-git-04.png" data-lazy- height="84" src="data:image/svg xml,” width=”1068″>

Git merge command

Now, use the following command to merge the new feature master branch.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/branch-merge-operation-git-05.png" data-lazy- height="238" src="data:image/svg xml,” width=”1103″>

Using the ‘git merge new-branch’ command, you can merge the specified branch into the currently active master branch. The new feature now has been added with the master branch. Using the following command, you can check commits and details:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/branch-merge-operation-git-06.png" data-lazy- height="195" src="data:image/svg xml,” width=”1110″>

Conclusion

To summarize all the above details, we have created a new branch ‘new_branch’, activate it, and add some new commits or new features to it. Once you have done all changes, merge this ‘new_branch’ back into the master branch. We have learned how to merge one branch with another branch in Git in this article.

About the author

<img alt="Karim Buzdar" data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/karim-150×150.png5fe707abe2870.jpg" height="112" src="data:image/svg xml,” width=”112″>

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.