Git: Merge Branch into Master – Stack Abuse

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.

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).

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

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.

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

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.

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.

Related Articles

Back to top button