Merging Branches with git #Part 2.
Tags: git, beginners, merging
Published: 2020-03-29 08:35:19

---
title: Merging Branches with git #Part 2.
published: true
description: This tutorials deals with merging more than one branches with the help of git.
tags: git, beginner, merging
---

So here we are with part 2 of our merging branches with GIT. in this tutorial we will discuss what happens when you try to merge a branch which is way too ahead of its original branch(in our case it is the master branch). 

So the setup is the same as in the previous tutorial. You can have a look at [](https://gitlab.com/manish169/gitlab-example-for-dev.to) for reference.

Let's continue our journey on merging the branches.

For this tutorial, I have already made three commits in the branch 'gaurang'. You can see in the image below:

![](https://dev-to-uploads.s3.amazonaws.com/i/fwl2i1kj0xzojfqu236y.PNG)

If you look carefully, you can see the current tip of our branch 'gaurang' is three steps/commits ahead of the master branch.

So, what will happen now? We will merge 'gaurang' with 'master'. But before merging let's have to compare the code of both branches.


Let's compare the code of two branches before merging:

branch 'master' :

```
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
  <!-- This is the change made by gaurang -->
<body >
  <!-- This is the change made by gaurang -->
  <p style="background-color: blue;">Blue is my favorite color</p>
  Just changed some Background Color of the Body
</body>
</html>

```

branch 'gaurang':

```
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body >
  <p style="background-color: blue;">Blue is my favorite color</p>
  Just changed some Background Color of the Body
  <p style="background-color: brown;">I hate Brown</p>
  <p style="background-color: chocolate;">I love Messi</p>

  <!-- This is the change made by gaurang -->
  <p style="background-color:crimson;">And I love Argetina</p>
  <!-- This is the change made by gaurang -->
</body>
</html>

```

So as you can see we have added three paragraphs in the body in branch 'gaurang'.

Before merging let's make sure to fetch the changes in the local copy of the 'master' branch. If you don't fetch the latest changes, git will ask you to do so. Let's fetch:

```
git fetch
```

![](https://dev-to-uploads.s3.amazonaws.com/i/khdirckbav7tk2g00kw9.PNG)

As you can see we have the latest changes in our branch. Now we can continue merging the branches. Let's run the merge command:

```
git merge <branch-name>
```

where the <branch-name> is the name of the branch which is to be merged. In our case, it will be 'origin/gaurang'. So our command will look something like this:

```
git merge origin/gaurang
```

When you run the merge command the editor will ask for **MERGE_MSG** by opening the file with the same name on your Editor. You can type whatever **MERGE_MSG** you like. Once you have typed the command and closed the file the branches will be merged and you can see the changes in your code.

As you can see we didn't see any merge conflicts in this merge because both developers didn't make the changes in the same file. If there were changes in the same file then we would have/had received merge conflicts and we had to resolve the conflicts manually.

After that you have to run the git add and commit command subsequently as the tip of your master branch is still behind branch 'gaurang'.

```
git add.
git commit -m "Commit Message"
```

Then you need to push the changes as you would normally do:

```
git push origin master
```

Now you can see the tip of your master branch is in Sync with the branch 'gaurang'

![After Merging](https://dev-to-uploads.s3.amazonaws.com/i/5jdyr8m9cz1mf9u07p9f.PNG)

That's it for this tutorial. We will talk about merging two branches and then syncing it with the master branch in the next tutorial.