Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to merge branches using git merge branchname in git

Dian Us Suqlain

A git branch is essentially an independent line of development. Branches are the different realities of a master branch.

We can create several branches and merge them with our primary working branch, called the master branch. The primary objective of git is to allow parallel work in software projects.

Why do we want to merge branches in GitHub?

We merge different realities/branches in order to integrate them in the master branch. In this way, they become a single identity.

Suppose we have more than one developer working on a single project. Each developer can create and work on their branches independently.

After the final review from a tester, we merge those branches with the master branch to become a final product.

Different realities created from a single branch

How to merge branches in git

GitHub contains fixed commands to perform various tasks and actions.

Each command in git has its specific role. To merge a branch in git, we follow the syntax below.


git branch // name your branch
git merge branchname

The merge command integrates changes from another branch.

The branch that receives changes is always the HEAD branch, which is currently checked out.

The git merge branch command has the following characteristics:

  • It joins branches.
  • It combines multiple sequences of commits into one.

We usually use git merge to combine two branches.

Different realities merged into the master branch

What happens when a merge command executes?

There is a chance that a git merge conflict will arise if we execute a merge command and git is unable to resolve inconsistency of code between two changes on its own.

These conflicts occur due to two main reasons:

1. While we conduct the process of merging

This type of conflict occurs due to incomplete changes in a branch that must be maintained using Git commands.

The process of merging will not initialize if there are still modifications in the current working project’s stage area.

2. Throughout the process of merging

This indicates that there’s a conflict between the local and merged branches. This conflict then fails to merge those branches.

Although git tries to resolve such conflicts, there are still places where we need a developer interaction.

How to resolve git merge conflicts

Resolving these conflicts is not a complicated task. All we need to do stick to the following:

  1. Stay calm: Patience is a trait that programmers must exhibit. It is best to begin by observing how the conflict arose.

    We can even reverse the merge back into its original state. It is not much of a hassle to resolve conflicts. Try to create a new branch or fix errors in the current one.

  2. Stay focused and try to determine when and why the conflict occurs: Although git tries to address a few conflicts itself, manual code review is still required in some areas.

    Suppose two developers work on the same code file and try to merge it. Conflict arises because changes have occurred in duplicate files.

    A particular scenario can occur where the same line of code in a single code file is changed. This type of conflict requires manual review, where we look at the changes and finalize a single statement.




Dian Us Suqlain
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring