How to Merge Two Branches in Git: A Complete Guide

Branching is one of Git’s most powerful features. It allows developers to work independently on features, fixes, or experiments. Once your work on a branch is complete, you’ll often want to merge it back into another branch—typically the main development line like main or develop.

In this guide, you’ll learn how to merge two branches in Git, step by step, using the command line.


🔍 What Is Git Merge?

Merging in Git means taking the contents of one branch and integrating them into another. Git tries to automatically combine the changes. If the branches have modified the same part of the code, you may have to resolve a merge conflict.


🛠 Prerequisites

  • Git installed and configured
  • A repository with at least two branches
  • A clean working directory (commit or stash changes before merging)

✅ Step-by-Step: How to Merge Two Branches

Let’s say you want to merge a feature branch (feature/login) into the main branch (main).

🔹 Step 1: Switch to the Target Branch

This is the branch you want to merge into (usually main):

git checkout main

Or, if you’re using newer Git versions:

git switch main

🔹 Step 2: Pull the Latest Changes (Optional but Recommended)

To ensure you’re working with the latest version of the target branch:

git pull origin main

🔹 Step 3: Merge the Source Branch into the Target Branch

Now, merge the branch you want to bring in:

git merge feature/login

If there are no conflicts, Git will merge the branches and create a new merge commit.


🔹 Step 4: Push the Changes to the Remote Repository

If the merge was successful, push the updated target branch:

git push origin main

⚠️ What If You Encounter Merge Conflicts?

If Git can’t automatically merge the branches, you’ll see a conflict message. Git will mark the conflicting files and pause the merge.

To resolve conflicts:

  1. Open the files with conflicts.
  2. Look for markers like: <<<<<<< HEAD // code from main ======= // code from feature/login >>>>>>> feature/login
  3. Edit the file to keep the correct version.
  4. After resolving, mark the file as resolved: git add <filename>
  5. Complete the merge: git commit

🔁 Optional: Fast-Forward Merge

If the target branch hasn’t diverged from the source, Git may use a fast-forward merge:

git merge --ff-only feature/login

This avoids a merge commit if possible.


🧠 Best Practices

  • Always test before merging: Especially for feature branches.
  • Use pull requests or merge requests on platforms like GitHub/GitLab for code review.
  • Delete the source branch after merging if it’s no longer needed: git branch -d feature/login git push origin --delete feature/login

✅ Summary

ActionCommand
Switch to target branchgit checkout main
Merge source branchgit merge feature/login
Push merged codegit push origin main
Delete local branchgit branch -d feature/login
Delete remote branchgit push origin --delete feature/login

🚀 Final Thoughts

Merging branches in Git is a crucial part of collaborative development. With a clear understanding of how to merge and handle conflicts, you’ll be able to maintain a clean and stable codebase across multiple features and teams.

Sharing Is Caring:

Leave a Comment