How Do I Merge One Branch to Another with Git?

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:

$ git branch

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

$ git branch new_branch

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

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."

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

$ git checkout master

Git merge command

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

$ git merge new-branch

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:

$ git log -1

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.



from Linux Hint https://ift.tt/34GqZIY

Post a Comment

0 Comments