Git Notes
Learn how to identify, understand, and resolve merge conflicts in Git. Master conflict resolution strategies, tools, and prevention techniques.
Merge conflicts occur when Git cannot automatically combine changes from different branches because both branches modified the same lines in a file. Understanding how to resolve conflicts efficiently is essential for team collaboration.
When Do Conflicts Happen?
Conflicts arise when:
- Two branches edit the same line(s) in the same file
- One branch deletes a file that another branch modified
- Both branches add a file with the same name but different content
| main | line 1: "Hello World" ← Changed to "Hello Everyone" |
| feature | line 1: "Hello World" ← Changed to "Hi World" |
| Git cannot decide which change to keep | CONFLICT |
Conflict Markers Explained
When a conflict occurs, Git marks the conflicting sections in the file:
<<<<<<< HEAD
const greeting = "Hello Everyone";
=======
const greeting = "Hi World";
>>>>>>> feature/update-greeting| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Start of YOUR branch's version (current branch) |
======= | Separator between the two versions |
>>>>>>> branch-name | End of THEIR branch's version (incoming branch) |
Creating a Conflict (Tutorial)
# Setup
git init conflict-demo && cd conflict-demo
echo "Hello World" > greeting.txt
git add . && git commit -m "Initial greeting"
# Create a feature branch and modify the file
git checkout -b feature/update
echo "Hi Universe" > greeting.txt
git add . && git commit -m "Update greeting to Universe"
# Go back to main and make a different change
git checkout main
echo "Hello Everyone" > greeting.txt
git add . && git commit -m "Update greeting to Everyone"
# Try to merge - CONFLICT!
git merge feature/updateAuto-merging greeting.txt CONFLICT (content): Merge conflict in greeting.txt Automatic merge failed; fix conflicts and then commit the result.
Resolving Conflicts Step by Step
Step 1: Identify Conflicted Files
git statusOn branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: greeting.txt
no changes added to commit (use "git add" to track)Step 2: Open and Edit the Conflicted File
cat greeting.txt<<<<<<< HEAD Hello Everyone ======= Hi Universe >>>>>>> feature/update
Edit the file to resolve — choose one version, combine them, or write something new:
# Option A: Keep current branch (HEAD)
echo "Hello Everyone" > greeting.txt
# Option B: Keep incoming branch
echo "Hi Universe" > greeting.txt
# Option C: Combine both changes
echo "Hello Everyone in the Universe" > greeting.txtStep 3: Mark as Resolved
git add greeting.txtStep 4: Complete the Merge
git commit
# Or with a message:
git commit -m "Resolve merge conflict in greeting.txt"[main f8e7d6c] Resolve merge conflict in greeting.txt
Complex Conflict Example
Resolved version (incorporating both changes):
Using Merge Tools
Configure a Merge Tool
# Set VS Code as merge tool
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait --merge $REMOTE $LOCAL $BASE $MERGED"
# Use the merge tool
git mergetoolBuilt-in Conflict Resolution
# Accept current branch version for all conflicts
git checkout --ours filename.txt
# Accept incoming branch version for all conflicts
git checkout --theirs filename.txt
# After choosing, mark as resolved
git add filename.txtResolve All Files at Once
# Accept all changes from current branch
git checkout --ours .
git add .
# Accept all changes from incoming branch
git checkout --theirs .
git add .Aborting a Merge
If you want to cancel the merge entirely:
# Before committing
git merge --abort
# After committing (revert the merge)
git revert -m 1 HEADPreventing Conflicts
Keep Branches Short-Lived
# Regularly update your feature branch
git checkout feature/my-work
git rebase main # or git merge mainCommunicate with Your Team
- Coordinate who works on which files
- Break large files into smaller modules
- Use feature flags instead of long-lived branches
Use .gitattributes for Binary Files
# .gitattributes
*.png binary
*.jpg binary
*.pdf binary
# These files use "ours" strategy - keep current version
database.sqlite merge=oursConflict During Rebase
Conflicts during rebase are resolved differently:
git rebase mainCONFLICT (content): Merge conflict in app.js error: could not apply a1b2c3d... Add feature
# Resolve the conflict in app.js
git add app.js
# Continue rebasing (NOT git commit)
git rebase --continue
# Or abort
git rebase --abortCommon Conflict Scenarios
Deleted vs Modified
CONFLICT (modify/delete): file.txt deleted in feature and modified in HEAD.# Keep the file
git add file.txt
# Accept deletion
git rm file.txtBoth Added (Same Filename)
CONFLICT (add/add): Merge conflict in newfile.txtBoth branches created a file with the same name — edit it to combine content.
Renamed vs Modified
CONFLICT (rename/delete): oldname.txt renamed to newname.txt in feature,
but deleted in HEAD.Interview Questions
- What causes a merge conflict in Git?
A merge conflict occurs when two branches modify the same line(s) in the same file, when one branch deletes a file another modified, or when both branches add a file with the same name but different content.
- How do you resolve a merge conflict?
Open the conflicted file, find the conflict markers (<<<, ===, >>>), decide which changes to keep, remove the markers, save the file, stage it with git add, and complete with git commit.
- What is the difference between
--oursand--theirs?
--ours keeps the version from the current branch (the one you are ON). --theirs keeps the version from the branch being merged in. During rebase, these are reversed.
- How can you prevent merge conflicts?
Keep branches short-lived, regularly rebase/merge from main, communicate with team about file ownership, break large files into smaller ones, and use feature flags.
- What happens when you run
git merge --abort?
It cancels the in-progress merge and restores the branch to its state before the merge was attempted. All conflict markers and partially merged changes are discarded.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Merge Conflicts - Resolve Git Conflicts Like a Pro.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, branching, and, merging, merge
Related Git & GitHub Topics