Git Merge Master into Branch

March 22, 2023

Introduction

Git is a version control system that allows users to work in several development lines called branches. The master (or main) branch is usually the default branch in a repository, created by default by the git init command.

Although it is not usually the practice to merge the master branch into other branches, there are some exceptions to the practice.

In this tutorial, you will learn when and how to merge the master branch into another branch.

Merge the master branch into a different one in Git - a tutorial.

Prerequisites

Merging Master into Branch in Git

The master branch is just like any other branch in Git. It is not special in any way other than the fact it is created by default when initializing the repository. However, since all development starts from the master branch, be cautious when merging it into other branches.

The exceptions for merging the master into another branch are when you need to merge a hotfix or a new release branch. There are two methods you can use:

  • git merge. The git merge command creates a new merge commit and preserves the development history.
  • git rebase. The git rebase command rewrites and creates a linear development history.

Although both commands work, git rebase should be used when you are working alone on the project, while git merge is more appropriate for teams.

The sections below show different methods for merging the master branch into another branch.

Method 1: Merge with git rebase

Use git rebase when you are working only in the local repository, or when working with a remote repository that is not publicly visible. Rebasing when you are collaborating with a team can cause confusion because the commits are rearranged and the history is rewritten.

Follow the steps below to rebase the master branch:

1. Open a Git bash window or terminal in Linux and navigate to the directory with your Git repository.

2. Switch to the branch you want the master branch to merge into. Use the git checkout or git switch command.

For example:

Switching branches in Git.

3. Run the following command to rebase the branch:

git rebase master
Rebasing the master branch in Git.

Resolve any conflicts that arise and the rebase is complete.

Note: Learn how to merge a Git branch into master.

Method 2: Merge with git merge

Use git merge when you are collaborating on a project and want to keep the development history intact. Merging is also recommended when the repository is publicly available.

Follow the steps below:

1. Open Git bash or a new terminal window and navigate to the repository directory.

2. Switch to the master branch and pull to download any changes from the remote repository. Use these two commands:

git switch master
git pull

3. Switch to the target branch and perform git pull again to update the commits:

git pull

4. Execute the git merge command to merge the master branch into the target branch:

git merge master
Merging the master branch in Git.

Resolve any merge conflicts that arise and complete the merge.

5. Push the changes to the remote repository:

git push origin
Pushing changes from local repository to a remote one.

The changes are now visible to other developers in the remote repository.

Note: Comparing two branches is a good practice before merging. Learn how to do it by referring to our article How to Compare Two Git Branches.

Conclusion

This tutorial showed how to merge the master branch into another branch using git merge and git rebase. Both commands perform the same action, but in different ways, making them suitable for different situations.

For more Git tutorials, check out our Git beginner's guide, or learn how to secure your Git installation with SSH or HTTPS.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Use Git Stash
September 13, 2022

Git stash is a way of storing unfinished work locally, without having to commit the changes. This tutorial shows how to create and use Git stash.
Read more
How to Restore a Git Repository
August 1, 2022

Deleted or overwritten a repository? No backup? Don't worry just yet, this guide covers some steps to try and restore the repository.
Read more
Git Tag: An Overview of the Basic Functions
September 6, 2022

This guide shows an overview of basic functions to perform with Git tags. See how to create, delete, push, replace, or checkout a Git tag.
Read more
Git Commands Cheat Sheet
March 10, 2020

Git, the popular version control system, has a plethora of commands for managing your repository. Download a cheat sheet and save it for future use.
Read more