How To Merge 2 Branches (Step by Step) Git Tutorial


![](https://i.imgur.com/lk1s7V8.png)

In this Git tutorial we will talk about how to merge two branches. We will discuss what merging is, the different types of merge that are possible and the steps you need to take in order to merge your branches in Git. After reading this tutorial you should be able to:

• Understand what a merge is;

• Know which types of merge are possible;

• Be able to perform a merge;

• Deal with conflicts if they occur.

In this blog post, we will learn how to merge 2 branches in Git.

Merging is Git’s way of putting a forked history back together again. The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch. Note that all of the commands presented below merge into the current branch.

Merging is a safe process because Git always creates a new commit that contains the result of the merge. This prevents Git from destroying any information.

To create a new commit object that merges two existing branches, execute git merge in the following way:

git checkout

git merge

In this tutorial, we’re going to merge two branches.

In fact, when you merge one branch into another, Git combines the work of both branches and creates a new commit in the process.

Let’s see how to do it!

First, make sure that you’re on the master branch. You can use git checkout master.

Then call git merge from the command line to merge a different branch (let’s say develop) into the current one:

git checkout master

git merge develop

I will explain merging and merging conflicts step-by-step on a practical example. This way, you’ll see the all steps you have to go through when merging, and all the corner cases that can arise from it.

We’ll start by creating a new branch called “feature” where we’ll add a fictional feature.

When we’re done implementing our feature, we want to merge it into master. We want to merge into master because master is our main working branch where all changes are eventually merged:

git checkout -b feature master

vi hello.txt

git add hello.txt

git commit -m “Added hello file”

Now that our feature branch is ready, we switch back to master and merge our changes:

git checkout master

git merge –no-ff feature

We now have two branches: master and feature.

When working with Git, you sometimes end up with many branches.

If the branches are a one off for a particular feature, this is fine. But sometimes, you have multiple long-running branches that you want to merge together.

Perhaps these are branches for different customers, or maybe they represent different feature areas in your code base.

In this article, I’ll show you how to merge multiple branches into a single branch using Git’s interactive rebase feature (which is also available in GitHub).

When working on a project, programmers often need to make modifications to the codebase. However, they shouldn’t make those changes directly in the master branch. Instead, they should create their own branch and then merge it with the master branch once the changes are ready to be implemented.

In this blog post, we’ll show you how to merge branches using Git. We’ll also explain what a fast-forward merge is and how it differs from a recursive merge. So let’s get started!

If you’re interested in learning more about Git and GitHub, check out our How To Use GitHub course here at Treehouse.

What Is Branching?

GitHub branching is used to develop new features of a project without affecting other branches. This allows developers to easily switch between different versions of a project and merge their work without interrupting other team members.

1. Ensure that your git branch is up to date with the remote branch:

git checkout master

git pull origin master

2. You can now merge the changes from the feature branch without any troublesome merge conflicts:

git merge –no-ff develop

3. After you have resolved any conflicts, you should complete the merge and push it to the remote repository.

git push origin master


Leave a Reply

Your email address will not be published. Required fields are marked *