How to Merge a Git Branch into Master

October 2, 2023


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.

How to merge a Git branch into master - a tutorial.


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
Listing all the local branches in Git.

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
Switching to the master branch in Git.

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:

Merging a branch into master and specifying a commit message.

The command merges the examplebranch branch into the master branch and automatically sets the commit message to the one specified in the double quotes.

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:

Specifying a commit message in a text editor.

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:

Merging a branch without specifying a commit message.

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
Pushing local changes to a remote Git repository.

The command pushes the changes to the remote repository, where they become available to everyone working on the project.


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.

Was this article helpful?
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 Overwrite Local Branch with Remote in Git
August 31, 2023

Overwriting a local Git branch with a remote one is useful when synchronizing changes with a remote repo. See two different methods for overwriting a branch in Git.
Read more
How to Compare Two Git Branches
August 23, 2023

See several methods for comparing two Git branches. Comparing branches is important before merging to ensure all conflicts and bugs are eliminated before changing the main codebase.
Read more
Git: Checkout a File from Another Branch
April 6, 2023

This tutorial shows three different methods for checking out a file from a separate branch in Git. See how to check out a file using git checkout, git restore, and git show.
Read more
How to Squash Commits in Git
March 23, 2023

Squashing commits in Git means combining multiple smaller commits into a large single one. This step-by-step tutorial provides four methods for squashing commits in Git.
Read more