How to Merge Two Branches in Git

Introduction

Merging branches in Git is an essential feature that allows developers to integrate changes from one branch into another. Whether you’re working on new features, fixing bugs, or collaborating with a team, Git merge ensures a smooth development process.

This guide will cover how to merge two branches in Git, explain different merging strategies, highlight common challenges like merge conflicts, and provide step-by-step instructions with examples.


What Are Branches in Git?

In Git, branches serve as pointers to specific commits within a repository’s history. They allow developers to work on different parts of a project independently, preventing conflicts and enabling a seamless workflow.

Each branch maintains its own history and changes until it is merged back into the main branch or another target branch.

Example:

  • The main branch contains stable code.
  • A feature-branch is created to develop a new feature without affecting main.

Types of Branch Merging

Git provides two primary methods for merging branches: Fast-forward merge and Recursive merge.

1. Fast-forward Merge

A fast-forward merge occurs when there are no new commits on the target branch since the branch being merged was created. In this case, Git simply moves the branch pointer forward.

Example:

git checkout main
git merge feature-branch

Here, Git updates main to point to the latest commit of feature-branch, maintaining a linear history.

Advantages:

✅ Keeps history simple and clean. ✅ No unnecessary merge commits.

Disadvantages:

⚠ Only works when the target branch has not diverged.


2. Recursive Merge

A recursive merge happens when two branches have diverged, meaning they both contain unique commits. In this case, Git analyzes both histories and creates a new merge commit.

Example:

git checkout main
git merge feature-branch

Git automatically resolves differences or highlights conflicts that require manual resolution.

Advantages:

✅ Maintains history integrity. ✅ Allows for tracking changes from both branches.

Disadvantages:

⚠ Can create merge conflicts that require manual resolution.


What Are Merge Conflicts?

A merge conflict occurs when Git cannot automatically combine changes because the same part of a file was modified in both branches. Git marks the conflict and requires manual intervention.

Example Conflict Message:

Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and commit the result.

Resolving a Merge Conflict

  1. Open the conflicted file.
  2. Look for conflict markers (<<<<<<<, =======, >>>>>>>).
  3. Edit the file to resolve differences.
  4. Stage the resolved file:
    git add file.txt
    
  5. Complete the merge with:
    git commit -m "Resolved merge conflict"
    

Steps to Merge Two Branches in Git

Step 1: Clone the Repository

If you haven’t already cloned your repository, use:

git clone <repository-url>
cd <repository-folder>

Step 2: Create a New Branch

To work on a new feature, create and switch to a new branch:

git checkout -b feature-branch

Step 3: Make Changes and Commit

Modify files and commit changes:

echo "New Feature" >> file.txt
git add file.txt
git commit -m "Added new feature"

Step 4: Switch to the Main Branch

Before merging, switch back to the main branch:

git checkout main
git pull origin main  # Ensure it's up-to-date

Step 5: Merge the Branch

Use the merge command:

git merge feature-branch

If there are no conflicts, Git will automatically merge the changes.

Step 6: Push the Merged Changes

After merging, push the changes to the remote repository:

git push origin main

Alternative Merge Strategies

1. Using git merge --no-ff

Forces Git to create a merge commit even if a fast-forward merge is possible.

git merge --no-ff feature-branch

✅ Preserves history of feature branches.

2. Using git rebase Instead of Merge

Rebasing rewrites history to make it linear.

git checkout feature-branch
git rebase main

✅ Creates a cleaner commit history. ⚠ Can cause issues if already pushed to a remote.


Advantages and Disadvantages of Merging

Advantages:

✅ Maintains collaborative workflow. ✅ Ensures changes are integrated correctly. ✅ Allows multiple developers to work independently.

Disadvantages:

⚠ Merge conflicts require manual resolution. ⚠ Complex merge history if not handled properly. ⚠ Incorrect merges can introduce bugs.


Best Practices for Merging in Git

✔ Always pull the latest changes before merging. ✔ Resolve conflicts carefully to avoid overwriting important changes. ✔ Use feature branches to isolate development work. ✔ Keep commit messages clear to document merge actions. ✔ Test merged changes before pushing to production.


Conclusion

Merging branches in Git is a crucial part of software development. Whether using fast-forward, recursive merges, or rebase, understanding these techniques helps maintain a clean and efficient project history.

By following best practices and resolving conflicts correctly, you can streamline collaboration and ensure a smooth development workflow.

For further details, refer to the official Git documentation.

Learn about how to commit merge in git​

Related articles

Managing Git Repositories with GitLab

Managing Git Repositories with GitLab GitLab is one of the most powerful Git repository management platforms, providing a wide...

GCP Cost Optimization Strategies for 2026

GCP Cost Optimization Strategies for 2026 Google Cloud continues to grow rapidly in 2026 as companies adopt more AI...

AWS IAM Interview Questions​ | Top 50 IAM Interview Questions 2025

AWS IAM Interview Questions​ ✅ 1. What is AWS IAM? Answer: AWS IAM (Identity and Access Management) is a service that...

Management with Ansible, Chef, and Puppet

Management with Ansible, Chef, and Puppet Configuration management is a critical practice in DevOps workflows, enabling teams to manage...