DEV Community

Manish
Manish

Posted on

Merging Branches with git #Part 2.

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 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:

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>

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

where the 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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

git push origin master
Enter fullscreen mode Exit fullscreen mode

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

After Merging

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.

Top comments (0)