Merge Branches and Resolve Merge Conflicts
Summary:
Understand git merge
and how to handle conflicts.
Merging branches is a fundamental operation in Git, frequently encountered during collaborative software development. This article covers how to use git merge
to combine branches, explains why merge conflicts occur, and provides step-by-step guidance for resolving conflicts.
Table of Contents
- What is a Branch in Git?
- Introduction to
git merge
- Merging Branches: Step-by-Step
- What Causes Merge Conflicts?
- How to Identify and Resolve Merge Conflicts
- Best Practices for Avoiding Merge Conflicts
- Final Words
What is a Branch in Git?
A branch in Git is a lightweight movable pointer to a commit. Developers use branches to work on features, fixes, or experiments in isolation from the main codebase (main
or master
branch).
# Create and switch to a new branch called 'feature'
git checkout -b feature
Introduction to git merge
The git merge
command lets you integrate changes from one branch into another. For example, after finishing work on a feature branch, you merge it into the main
branch to make your changes part of the main codebase.
Example:
# Switch to the main branch
git checkout main
# Merge the feature branch into main
git merge feature
This tries to automatically combine the histories and contents of two branches.
Merging Branches: Step-by-Step
-
Ensure Your Branches Are Up-to-Date
First, fetch the latest changes:git fetch origin
-
Switch to the Target Branch
Typically, you merge intomain
ordevelop
:git checkout main
-
Merge the Source Branch
Usegit merge
followed by the branch name:git merge feature
If there are no conflicts, Git creates a new "merge commit" combining the histories.
What Causes Merge Conflicts?
A merge conflict happens when Git is unable to automatically combine file changes from different branches. Common causes include:
- Two branches modify the same line in a file differently.
- A file is deleted in one branch but modified in another.
- Changes overlap in such a way that Git cannot decide which content to keep.
How to Identify and Resolve Merge Conflicts
When a conflict happens:
-
Git Halts the Merge Process
You'll see messages like:
CONFLICT (content): Merge conflict in <filename>
-
Check Conflict Status
git status
This lists conflicted files.
-
Open Conflicted Files
In the conflicted files, you'll see conflict markers:<<<<<<< HEAD Code from the current branch ======= Code from the branch being merged >>>>>>> feature
-
Manually Edit the Files
Decide which parts to keep, modify, or combine. Remove markers (<<<<<<<
,=======
,>>>>>>>
) after resolving. -
Mark Conflict as Resolved
git add <filename>
-
Finish the Merge
git commit
If you used
git merge
(not with--no-commit
), Git may automatically create a merge commit after all conflicts are resolved and staged.
Example
Suppose both branches changed the same line in app.py
. You'll see:
<<<<<<< HEAD
print("Hello from main branch")
=======
print("Hello from feature branch")
>>>>>>> feature
You might resolve it like this:
print("Hello from main and feature branches")
Then save, add, and commit.
Best Practices for Avoiding Merge Conflicts
- Pull Regularly: Frequently synchronize your branches with the remote repository.
- Small, Frequent Merges: Merge changes incrementally rather than in large batches.
- Clear Communication: Coordinate with team members to prevent overlapping work on the same files.
- Rebase When Appropriate: Use
git rebase
to streamline your commit history before merging (advanced users).
Final Words
Merging branches and resolving conflicts are central to effective collaboration in Git. While merge conflicts can be frustrating, they're an opportunity to ensure the integrity and intent of everyone’s contributions. Practice makes perfect: with experience, resolving conflicts becomes a routine part of your development workflow.
Happy coding!