Git

How do I Safely Merge a Git Branch into Master?

Git is a well-known, open-source, and free distributive version control program that enables programmers to work on projects, manage source code, and test their different code versions through Git branches. More specifically, Git branches are a useful component of Git that play a vital role in code management and code testing. It can also be utilized to merge different features through merging.

This write-up will illustrate the method to merge a Git branch into Master safely. So, let’s start!

How to Merge a Git Branch into a Master?

The master or main branch is the default branch of Git. However, users can create new branches and utilize them. To merge a Git branch safely, you can utilize the β€œ$ git branch merge <branch-name>” command. For this purpose, check out the provided procedure.

Step 1: Launch Git Terminal
From the Start menu, launch the Git Bash terminal:

Step 2: Open Git Local Repository
Next, navigate to the Git local repository using the β€œcd” command:

$ cd "C:\Git"

Step 3: Initialize Git repository
Initialize the Git repository through the provided command:

$ git init

Step 4: List Down Git Branches
List down all local repository Git branches by utilizing the β€œgit branch” command:

$ git branch

It can be seen that currently we are using the β€œmain” branch, and we have a master branch that is next to the main branch in the given list:

Step 5: Switch to Master
From the main branch, switch to the master branch through the given command:

$ git checkout master

The below output indicates that we have switched to the Git master branch:

Step 6: Make a New Branch
After that, create a new branch that you need to merge later:

$ git branch New-branch

To verify whether the branch is created or not, again list down the local branches:

$ git branch

From the below output, you can see we have created a β€œNew-branch” successfully:

Step 7: Merge Branch
Lastly, merge the newly created branch into the master branch:

$ git merge New-branch

We got the β€œAlready up to date” message as we have already merged the required branch:

Note: To merge any branch into the master branch, you need to switch to the master branch.

Conclusion

To merge the Git branch safely into the master branch, first, open the Git repository and initialize it. Next, switch to the master branch through the β€œ$ git checkout master” command. Then, create a new branch and merge it to the master branch using β€œ$ git merge <branch name>”. In this blog, we have illustrated how to merge a Git branch safely into a master 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.