How to Make the Current Git Branch a Master Branch

Sometimes it is necessary to make your current branch a master branch. Though there is no direct way of doing it, you can achieve it using a merge strategy.

Watch a course Git & GitHub - The Practical Guide

Steps to making the current branch a master

In this snippet, we will go through an example of making your current git branch to a master branch. For meeting this goal you should follow the steps below:

Checkout to the right branch

Make sure that, currently, you are on the branch you want to make a master. Otherwise, it is necessary to checkout to that branch:

git checkout <better-branch>

Merge master with the current branch

Run the command below to merge the master with our branch, by keeping the content of our branch:

git merge --strategy=ours master

The ours option forces conflicting hunks to be auto-resolved cleanly by favoring our version. Changes from the other tree that do not conflict with our side are reflected in the merge result. For a binary file, the entire contents are taken from our side.

To get more information about merge strategies, you can refer to this source.

Checkout to master

Next step is to switch to the master branch:

git checkout master

Merge the current branch with master

The final step is doing a fast forward merge of the current branch:

git merge <better-branch>

In case you want to make your history clearer, it is recommended to add some information to the merge commit message. You should change your second line to:

git merge --strategy=ours --no-commit master 
git commit # adding information to the message of the template merge

The git branch Command

The git branch command is a go-to command for managing all the aspects of your branches. No matter it's in the local git repository or the remote. Generally, git branch helps you create, list, or delete branches.

Each new branch is created for encapsulating the changes when you wish to add new features or fix current bugs. It makes your history clearer before merging it. Branches can be described as an isolated line of development. They represent a way of requesting a new working directory, staging area, and project history.

Any time you create a new branch, Git will make a new pointer. It doesn’t change your repository’s history.

The git merge Command

The git merge command is used for integrating independent lines of development to a single branch. It works along with the git checkout command for selecting the current branch and the git branch command with the -d option for deleting the obsolete target branch.

Primarily, the git branch command is used for combining two branches. You can also use it for merging multiple commits in one history.

The git checkout Command

This command is primarily used for navigating between the created branches. When you run the git checkout command it updates the files in your working directory to correspond with the version that is stored in the given branch. It also orders Git to record new the overall new commits on that branch.

Git checkout should not be confused with the git clone command. The latter works to fetch code from a remote repository.