Skip to content
DeveloperMemos

How to Merge Git Branches

Git, Version Control1 min read

Git, a powerful distributed version control system, allows developers to work on separate branches to develop new features or make changes without affecting the main codebase. However, at some point, these separate branches need to be merged back together. In this article, we will discuss various techniques for merging Git branches and provide examples that demonstrate how to handle different merging scenarios.

Scenario 1: Merging a Feature Branch into the Main Branch

Let's consider a typical scenario where you have been working on a feature branch called feature/add-login and you are ready to merge it into the main branch, usually referred to as master or main. Follow these steps:

  1. Ensure your feature branch is up to date with the latest changes from the main branch by executing:

    1git checkout feature/add-login
    2git pull origin main
  2. Switch to the main branch using:

    1git checkout main
  3. Merge the feature branch into the main branch:

    1git merge feature/add-login
  4. Resolve any merge conflicts that might arise. Git will indicate the conflicting files, and you can manually edit them to incorporate both sets of changes.

  5. Once conflicts are resolved, commit the merge changes:

    1git commit -m "Merge feature/add-login into main"
  6. Finally, push the merged changes to the remote repository:

    1git push origin main

Scenario 2: Merging Branches with Linear History

In some cases, you may encounter branches with linear history, meaning there are no conflicts or diverging changes. Merging such branches is straightforward:

  1. Checkout the branch you want to merge into (e.g., main):

    1git checkout main
  2. Merge the other branch into the current branch:

    1git merge feature/linear-history
  3. Commit the merge changes:

    1git commit -m "Merge feature/linear-history into main"
  4. Push the merged changes to the remote repository:

    1git push origin main

Scenario 3: Merging Branches with Diverging Changes

At times, two branches might have diverged, resulting in conflicting changes. Git provides tools to help resolve these conflicts. Let's see an example:

  1. Checkout the branch you want to merge into (e.g., main):

    1git checkout main
  2. Merge the branch with diverging changes into the current branch:

    1git merge feature/diverging-changes
  3. If conflicts occur, Git marks the conflicting files. Open each conflicted file and manually resolve the conflicts, keeping the desired changes.

  4. After resolving conflicts, stage the changes:

    1git add <resolved-file>
  5. Commit the merge changes:

    1git commit -m "Merge feature/diverging-changes into main"
  6. Push the merged changes to the remote repository:

    1git push origin main

In closing - merging Git branches is a critical aspect of collaborative development. By following the appropriate steps and resolving conflicts diligently, you can seamlessly merge changes from separate branches into a cohesive codebase. Understanding different merging scenarios and their corresponding techniques will empower you to handle complex branching situations efficiently.