Git

Merge One Local Branch into Another Local Branch

Git is a powerful DevOps tool utilized for maintaining and managing DevOps projects and their source code. Git developers manage their code in different Git branches. More specifically, Git branches are the main component of Git used to test code, implement code in different contexts, define features in multiple ways, and many more. However, branches are further categorized into two categories: remote branches and local branches.

This blog will elaborate on the procedure for merging one local branch into another.

How to Merge One Branch into Another Local Branch?

Local branches exist on the local directory and are accessible through local machines only. To merge one local branch into another local branch, first, open the Git local repository and create a new branch. Next, merge that branch into any existing branch.

To merge a branch into another, look at the provided steps.

Step 1: Open Git Terminal

First, launch the β€œGit Bash” terminal:

Step 2: Go to Git Local Repository

Go to the Git repository using the β€œcd” command:

$cd "C:\Git"

Step 3: Create New Git Branch

Utilizing the β€œgit branch” command along with the new branch name for creating a new Git branch:

$ git branch First-branch

To verify whether the branch is created or not, simply execute the β€œgit branch” command:

$ git branch

You can see that we have successfully created a new branch:

Step 4: Switch Branch

In order to switch to a newly created branch, check out the provided command:

$ git checkout First-branch

Step 5: Switch to Existing Branch

Switch to any existing local branch in which you want to merge the newly created local branch. For instance, we have switched to the β€œfeatures” branch:

$ git checkout features

Step 6: Merge Branch

Lastly, merge the local branch into the currently opened local branch by utilizing the β€œgit merge” command:

$ git merge First-branch

The output displayed an β€œAlready up to date” message because we had already merged the local branch:

We have taught you how to merge one local branch into another local branch.

Conclusion

For merging a local branch into another, first, open the Git local repository. Next, create a new branch utilizing the β€œ$ git branch <branch name>” command. Next, switch to that branch in which you want to the new branch using the Git β€œcheckout” command. After that, merge the branch using the β€œ$ git merge <branch name>” command. In this write-up, we have illustrated the method to merge one branch into another local branch.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.