Git is a powerful tool for collaboration—but when multiple people (or even just you on different branches) modify the same part of a file, merge conflicts can occur. While conflicts are a normal part of software development, resolving them effectively is crucial to maintaining a smooth workflow.
In this guide, you’ll learn what Git conflicts are, why they happen, and how to resolve them step-by-step.
🔍 What is a Git Conflict?
A Git conflict occurs when Git can’t automatically merge changes from different branches because two or more people have edited the same lines of a file—or one has deleted a file the other modified.
For example:
- You change
line 10
inmain.py
on one branch. - Someone else changes the same
line 10
on another branch. - When you try to merge or rebase, Git doesn’t know which change to keep—and raises a conflict.
⚠️ When Do Conflicts Happen?
Common scenarios include:
- Merging branches:
git merge feature-branch
- Rebasing a branch:
git rebase main
- Cherry-picking commits:
git cherry-pick <commit>
🛠️ How to Resolve Git Conflicts (Step-by-Step)
1. Trigger the Conflict (Accidentally or Intentionally)
When you run a command like:
git merge feature-branch
And Git finds conflicting changes, you’ll see:
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
2. Identify the Conflict
Git will mark the conflicts in your files using this format:
<<<<<<< HEAD
Your changes (from the current branch)
=======
Incoming changes (from the branch you're merging)
>>>>>>> feature-branch
Now it’s your job to manually edit the file and choose what to keep.
3. Resolve the Conflict
You can:
- Keep your changes
- Keep incoming changes
- Combine both changes
- Rewrite the section entirely
Example after resolution:
print("Hello from both branches!")
Then, remove the conflict markers (<<<<<<<
, =======
, >>>>>>>
).
4. Mark the File as Resolved
Once the conflicts are fixed and saved:
git add file.txt
Repeat for every conflicted file.
5. Commit the Merge
If you’re resolving a merge conflict:
git commit
Git will generate a default merge message. You can edit it or accept as is.
If you’re rebasing:
git rebase --continue
6. Push Changes
If you’ve completed the merge locally and everything looks good:
git push
💡 Note: If you force-pushed earlier or rebased a remote branch, use:
git push --force
🧪 Optional: Use Visual Merge Tools
Git integrates with tools like:
- VS Code: Detects and highlights conflicts
- GitKraken: Visual conflict resolution
- meld, kdiff3, or Beyond Compare
To configure a merge tool:
git config --global merge.tool meld
git mergetool
🧠 Best Practices to Avoid Conflicts
- Pull frequently: Stay updated with the latest changes.
- Communicate: Coordinate with team members on shared files.
- Break work into smaller commits: Smaller changes reduce overlap.
- Use feature flags: Avoid editing the same sections concurrently.
✅ Summary
Step | Command / Action |
---|---|
Merge or rebase | git merge branch-name |
Find conflicts | Git marks files with <<<<<<< markers |
Resolve manually | Edit files to resolve conflicts |
Mark as resolved | git add file.txt |
Commit merge | git commit |
Finish rebase | git rebase --continue |