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:
- Open the files with conflicts.
- Look for markers like:
<<<<<<< HEAD // code from main ======= // code from feature/login >>>>>>> feature/login
- Edit the file to keep the correct version.
- After resolving, mark the file as resolved:
git add <filename>
- 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
Action | Command |
---|---|
Switch to target branch | git checkout main |
Merge source branch | git merge feature/login |
Push merged code | git push origin main |
Delete local branch | git branch -d feature/login |
Delete remote branch | git 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.