Hit enter after type your search item

What is git merge command?

As the name suggests, the merge command is used to join the histories of two or more developments. For example, if your main branch is the master branch and you started working on another branch called feature branch. You added a new feature e.g. footer for the website in that feature branch.

After completing the work, you want to combine the development into the master branch. This is where git merge command plays its role in letting you join feature branch work into the master branch.

In terms of executing the command, this is how it works at its basic:

$ git checkout master

Create a new branch based on master:

$ git branch feature

$ git checkout feature

Adding the footer file:

$ git add footer.php

Now, commit the changes:

$ git commit –m “added footer component”

After the work is done for adding the footer component, you may merge it into the master branch as follows:

$ git checkout master

$ git merge feature

The above command should merge the feature branch commits into the master branch, so both branches are now at the same level.

Still need for explanation? No problems. The next section explains with some screenshots and more details on how to merge a command, resolve the conflicts and reverting back the merge.

Did you know? As you run a pull command e.g. $ git pull origin master, the Git performs fetch and merge operations. The fetch downloads the content from remote repo which is followed by the merge operation.

The example of merge git command – step by step

For demonstrating how merge command works, I have created an online repository on Github website. I have also set up a local repository in a directory with the name of m-repo-2.

The local repository is synchronized with the online repo and the master branch on both repositories contains the same files/commits as shown below:

On the remote repository:

Git online repo

For checking the files on local repo, I ran the $ ls command and see what is displayed:

Git local repo

From this point, you got a task of adding the footer in your project. The file name for the footer is “footer.php”.

For that, we are creating a new branch footer-feature which is based on the master branch by running this command:

$ git checkout -b footer-feature

The result:


Basically, this command is the shorthand for these two commands:

$ git branch footer-feature

$ git checkout footer-feature

Assuming that the work on footer.php is completed, let us add this file in the footer-feature branch.

$ git add footer.php

This is followed by running the commit command:

$ git commit -a -m “New footer component # 1”

The output of commit and after running the $ ls command again gives the following output:

Git branch commit

The changes are added in the new branch and this is time to merge this with the master branch – first locally and then pushing in the remote repository.

Merging with the master branch

For merging the new changes with the master branch so it is updated, checkout to master branch again:

$ git checkout master

Just for the sake of confirming things, I ran the $ ls command for master branch:

Git branch master merge

You can see, there is no footer.php file.

Executing the merge command

$ git merge footer-feature

The result:


Again running ls command:

$ ls

6.0_5-Git-revert-commit.png  footer.php  README.md  test.php

You see, our new footer component is added in the master branch.

Merging remotely

Run the push command as follows for updating the remote repository:

$ git push origin master

Or, if simple push command does not work, you may run:


Note: Generally, you should run push with –force option very carefully.  Otherwise, this is considered dangerous. Only use –force option if you know what are doing.

You can see, the new feature file is also visible on the remote repo:

Git merge

Use case – What if master and feature branch conflicts while merging?

In this section, we will look into resolving the conflicts occurred due to the multi-developers environment. Suppose, two developers are working on the same project pointing to the same remote repository.

A developer is working on the footer component as in the above case while the other developer is given a task to add a sidebar. We will use following names for active branches and files:

Developer 1:

  • Active branch = master
  • Feature branch = footer-feature
  • File name  = footer.php

Developer 2:

  • Active branch = Master
  • Feature branch = sidebar-component
  • File Name = sidebar.php

Both developers have updated their local repositories with the remote Github repo by this command:

$ git pull origin master

The developer 1 completes the task by creating a new footer-feature branch and merged it with the master branch (please see the above section for commands of creating to merging the branches).

Finally, he pushes the changes to the remote repository so it is available to the other team members.

Meanwhile, the developer 2 is working on the sidebar.php file under sidebar-component branch in his local system.

He also completes the work and merges the sidebar-component branch with the master branch in local repository as follows:

$ git checkout -b sidebar-component

This should create a new branch and checked out as well.

After creating the sidebar.php in the working directory, now add it to the branch:

$ git add sidebar.php

Performing a commit:

$ git commit -a -m “sidebar added – Commit 1”

The output:


Check out to master branch and perform a merge so that remote repository can also be updated with sidebar component based on the master branch.

$ git checkout master

Merging two branches:

$ git merge sidebar-component

The result:


As this developer tries to push the work by this command:

$ git push origin master

The git returns an error message as shown below:


This error can easily be resolved by running the pull command and update your local repository with the remote as Developer 1 has added the footer.php file whereas your local repository does not.

$ git pull origin master

As you run this command, Git simply asks to enter a message that why this merge is necessary.

Enter a message in the editor and press: Esc : wq from the keyboard.

The developer 2 local repository should be updated and the local directory should show the footer.php file as well.

Now, if you run the push command again:

$ git push origin master

It should upload the sidebar.php file in the remote repository as shown below:

Git merge remote

Until now, the work is smooth. Only a simple merging issue raised and we easily resolved this.

Scenario # 2 – working on the same file

The conflicts rise particularly if developers working on the same file and you try to fetch and merge an updated version into your local repository. This section goes through working on the same file by two developers on separate systems. See how conflict can be solved while trying to merge file.

Consider both developers have the same file: footer.php on their local repository.

The file contains the following simple code for the demo only:


The first developer starts changing the file and adds his code. For example:

He commits the changes and pushes to origin master:

$ git commit -a -m “Developer 1 changes – Commit 2”

$ git push origin master

The operation is successful and remote repository is updated without any issues.

Meanwhile, the developer 2 also started working on that file and after completing the changes has the following code:


He also commits it to the local branch (master) and then runs the push command for making it online in the remote repository. However, the following error is raised:

Git merge conflict

The option is to first download the remote branch again by pull command:

$ git pull origin master

And this is the output:


That is, it resulted in a conflict of code in the footer.php file.

If you open the file from local directory to your editor, you will see code like this:


So, it contains the code of both developers and Git adds markers to distinguish it wherever conflict occurred.

How to resolve this conflict and merge?

One of the ways for resolving this conflict is to open the filer in the editor or within Git. Find out the markers that Git identified as conflict and remove those after carefully looking at the code.

Git merge conflict fix

You can see three markers in the above graphic. After removing these, add the file and then commit:

$ git add footer.php

$ git commit -a -m “Their and Our changes are fixed – Commit 3”

After that, run the status command:

$ git status

This should return:

On branch master

nothing to commit, working tree clean

Now, execute the push command for making changes on the remote repo:

$ git push origin master

After the push is successful, check the online repo. This should display the commit message with the updated file as shown below:

Git merge two

How to revert a merge?

After a merge is done by using the git merge command, you may undo this by using the reset command. For example:


This command will undo the last commit.

You may learn more about how to undo commits in Git in detail.

This div height required for enabling the sticky sidebar