W3docs

How to Make the Current Git Branch a Master Branch

In this snippet, we will go through an example of making the current git branch to a master. Follow the steps below to do it in an easy and fast way.

Sometimes it is necessary to make your current branch a master branch. While you can rename it directly with git branch -m master, you can also achieve it using a merge strategy.

Steps to make the current branch a master

In this snippet, we will go through an example of making your current git branch a master branch. To achieve this, follow the steps below:

Checkout to the correct branch

Make sure that you are currently on the <kbd class="highlighted">branch</kbd> you want to make a master. Otherwise, you need to checkout to that branch:

git checkout &lt;better-branch&gt;

Merge master with the current branch

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

git merge -s ours master

The <kbd class="highlighted">ours</kbd> merge strategy 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

The next step is to switch to the master branch:

git checkout master

Merge the current branch with master

The final step is to perform a fast-forward merge of the current branch:

git merge &lt;better-branch&gt;

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

git merge -s ours --no-commit master
git commit # add information to the merge commit message

The git branch Command

The git branch command is a go-to command for managing all the aspects of your branches, whether they are in a local git repository or remote. Generally, <kbd class="highlighted">git branch</kbd> 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 into a single branch. It works along with the git checkout command for selecting the current branch and the <kbd class="highlighted">git branch</kbd> command with the <kbd class="highlighted">-d</kbd> option for deleting the obsolete target branch.

Primarily, the <kbd class="highlighted">git merge</kbd> 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 tells Git to record new commits on that branch.

<kbd class="highlighted">Git checkout</kbd> should not be confused with the git clone command. The latter works to fetch code from a remote repository.