Introduction
Merging is an essential Git operation that combines changes from two branches. Its primary purpose is to integrate changes made in one branch (the source branch) into another (the target branch) and share those changes with other developers.
This tutorial shows how to merge a Git branch into the master
(main
) branch.
Prerequisites
- Git installed (follow our tutorials to install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
Merge a Git Branch into Master
The master
(or main
) branch in Git is a repository's default and primary branch. It usually represents the latest stable version of the project's code. Merging another branch into master
allows multiple developers to work on different features or fixes concurrently and then bring those changes together into a single branch.
Note: Two terms describe the main branch - master
and main
. The more neutral term main
has been more prevalent in recent years due to discussions about inclusivity. Previously, the only term for the main branch was master
.
Follow the steps below to merge a branch into master
:
Step 1: List All Git Branches
List all the branches in your local Git repository using the git branch
command:
git branch
The output shows we are currently on the master
branch and lists all the other branches in the repository.
Note: See how to list remote branches in Git.
Step 2: Switch to Master
Ensure you are on the branch you want to merge into. In our case, the master
branch. Use the git switch
or git checkout
command to switch to the master branch if you are not already on it:
git checkout master
The command switches to the master
branch.
Step 3: Merge Branch into Master
After switching, use the git merge
command to merge another branch into master
. The merge creates a merge commit, bringing together multiple lines of development while preserving the history of the source branch.
Since merging is a type of commit, it also requires a commit message. There are two ways to specify the commit message:
- Using the
merge
command. - Specifying the commit message in a text editor.
1. Specify Commit Message Right Away
To specify the merge
message right away, use the following syntax:
git merge -m "Your merge commit message" [source_branch]
- The
-m
option is used to specify a commit message. - Replace
"Your merge commit message"
with the message you want to use for the merge commit. Enclose the message in double quotes. [source_branch]
is the name of the branch you want to merge into your current branch.
For example:
The command merges the examplebranch
branch into the master
branch and automatically sets the commit message to the one specified in the double quotes.
Note: See how to resolve potential merge conflicts or change a commit message later.
2. Specify Commit Message Separately
Alternatively, use the following syntax to specify the commit message separately:
git merge [source_branch]
For example:
git merge examplebranch
The command starts the merge process and opens the default text editor, prompting you to enter a commit message for the merge:
In Windows, the Notepad++ editor opens, or whichever is the default one on your system. Specify the merge message, save and close the file, and the merge process completes:
Step 4: Push Changes
The final step is to push the local changes to the remote repository so everyone working on the project can fetch the latest version. The syntax is:
git push [remote_name]
Replace [remote_name]
with the name of the remote repository. For example, if your remote repository is origin
, run the following command:
git push origin
The command pushes the changes to the remote repository, where they become available to everyone working on the project.
Conclusion
This tutorial showed how to merge a Git branch into the master
branch. Merging is an essential Git procedure that allows users to bring together multiple lines of development.
Next, see how to merge a master branch into another one or learn the difference between git rebase and git merge.