Git: Merge Branch into Master

Quick Answer - Merge Branch into Master

If you're looking for a quick answer, to merge a branch into the master branch - you checkout master and merge some_branch:

$ git checkout new-branch
# ...develop some code...

$ git add .
$ git commit –m "Some commit message"
$ git checkout master
Switched to branch 'master'
$ git merge new-branch

You always checkout the branch you're merging into, and merge a branch that already has changes.

Understanding Branch Merging with Git

If you're newer to Git, though, it's worth taking a few minutes to understand these commands and how they work. It's surprisingly simple and will take you a long way.

Note: In 2020, Git (alongside major repository hosting platforms like GitHub, GitLab, etc.) adopted a change in official terminology, and the default branch name was changed to main, due to the negative connotations the word master may entail. Many projects haven't migrated or renamed their main branches, so for the foreseeable future - the terms master and main will likely be used interchangeably.

One of Git's most powerful features is the ability to easily create and merge branches and changes to the codebase.

Git's distributed nature encourages users to create new branches often and to merge them regularly as a part of the development process - and certain Git workflows exploit this extensively.

This fundamentally improves the development workflow for most projects by encouraging smaller, more focused, granular commits, subject to rigorous peer review.

In legacy Version Control Systems (like CVS) the difficulty of merging restricted it to advanced users. Other modern but centralized version control systems like Subversion require commits to be made to a central repository, so a nimble workflow with local branching and merging is atypical.

A commonly used branching workflow in Git is to create a new code branch for each new feature, bug fix, or enhancement.

These are called Feature Branches.

Each branch compartmentalizes the commits related to a particular feature. Once the new feature is complete – i.e. a set of changes has been committed on the feature branch – it is ready to be merged back into the master branch (or other main code line branch depending on the workflow in use).

Note: Feature branching isn't the only branching workflow you can go with, but it's a widely adopted one, and is enabled by the simplicity of Git's merging functionalities.

Merge Branch into Another with Git

The git branch command is used to list all existing branches in a repository. An asterisk will appear next to the currently active branch:

$ git branch
* master

To create a new branch, we can use the git branch new-branch command. This will create a new branch mirroring the commits on the currently active branch:

$ git branch new-branch
$ git branch
* master
new-branch

Note: Behind the scenes, Git does not actually create a new set of commits to represent the new branch. A branch is like a tag, and the commits are shared. You're branching out a new set of changes from the main branch. Once a feature branch is finished and merged into the main branch, the changes in it become the main branch, until you merge a new feature branch into the main branch.

At this point we have created a new branch, but are still located on the source branch. To start working on the new branch we first need to run the command git checkout new-branch. This will change the active branch to the new branch:

$ git checkout new-branch
Switched to branch ‘new-branch'
$ git branch
master
* new-branch

At this point, commits can be made on the new branch to implement the new feature. Once the feature is complete, the branch can be merged back into the main code branch.

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch.

Note: git merge merges the specified branch into the currently active branch. So we need to be on the branch that we are merging into.

If you're merging a new feature into the main branch, you first want to switch to the main branch and then merge into it:

# ...develop some code...

$ git add .
$ git commit –m "Some commit message"
$ git checkout master
Switched to branch 'master'
$ git merge new-branch
Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

If all goes well then our job is done. The new feature commits now appear in the main branch. However, it is possible that Git won't be able to complete the merge due to a conflict change in the source branch. This is called a merge conflict.

Advice: If you'd like to read more about Merge Conflicts, how they can arise and how to resolve them - read our "Git: Guide to Resolving Merge Conflicts"!

To summarize, here are the commands to create a new branch, make some commits, and merge it back into master:

$ git checkout master
$ git branch new-branch
$ git checkout new-branch

# ...develop some code...

$ git add .
$ git commit –m "Some commit message"
$ git checkout master
$ git merge new-branch

About the Author

This article was written by Jacob Stopak, a software consultant and developer with passion for helping others improve their lives through code. Jacob is the creator of Initial Commit - a site dedicated to helping curious developers learn how their favorite programs are coded. Its featured project helps people learn Git at the code level.

Last Updated: April 20th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Jacob StopakAuthor

Jacob Stopak is a software developer and creator of InitialCommit.io - a site dedicated to teaching people how popular programs are coded. Its main project helps people learn Git at the code level.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms