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.
Prerequisites
- Git installed (see how to install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
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
. Thegit merge
command creates a new merge commit and preserves the development history.git rebase
. Thegit rebase
command rewrites and creates a linear development history.
Note: Learn more about the difference between git rebase and git merge.
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:
3. Run the following command to rebase the branch:
git rebase master
Resolve any conflicts that arise and the rebase is complete.
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
Resolve any merge conflicts that arise and complete the merge.
5. Push the changes to the remote repository:
git push origin
The changes are now visible to other developers in the remote repository.
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.