What's Actually Happening
A merge conflict means Git can't automatically combine changes because both branches modified the same part of a file. Git pauses the merge and marks the conflicting sections, waiting for you to decide which changes to keep.
The Error You'll See
$ git merge feature-branch
Auto-merging src/config.js
CONFLICT (content): Merge conflict in src/config.js
Automatic merge failed; fix conflicts and then commit the result.Or during pull:
$ git pull origin main
CONFLICT (content): Merge conflict in src/app.js
CONFLICT (content): Merge conflict in package.json
Automatic merge failed; fix conflicts and then commit the result.Understanding Conflict Markers
Open a conflicted file and you'll see:
// src/config.js
const config = {
<<<<<<< HEAD
port: 3000,
debug: true,
=======
port: 8080,
production: true,
>>>>>>> feature-branch
};The markers mean:
- <<<<<<< HEAD - Your current branch changes start here
- ======= - Divider between your changes and incoming changes
- >>>>>>> feature-branch - Incoming branch changes end here
Step 1: Identify All Conflicted Files
git statusLook for "both modified" files:
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: src/config.js
both modified: package.jsonGet a concise list:
git diff --name-only --diff-filter=UStep 2: Understand Each Conflict
For each conflicted file, examine what changed:
# See detailed conflict view
git diff src/config.jsOr use a visual diff tool:
```bash # VS Code opens merge conflict editor git mergetool --tool=vscode
# Or use default tool git mergetool ```
Step 3: Resolve Conflicts - Choose Your Approach
Option A: Keep Your Changes
// Remove the markers and keep HEAD version
const config = {
port: 3000,
debug: true,
};Option B: Keep Incoming Changes
// Remove the markers and keep feature-branch version
const config = {
port: 8080,
production: true,
};Option C: Combine Both Changes
// Manually merge the changes
const config = {
port: 8080,
debug: false,
production: true,
};Step 4: Mark Files as Resolved
After fixing each file:
git add src/config.js
git add package.jsonGit removes the conflict markers when you add the file.
Step 5: Complete the Merge
Once all conflicts are resolved and added:
git commitGit creates a merge commit with a default message. Edit if needed:
git commit -m "Merge feature-branch: resolve config port conflict"Step 6: Quick Resolution Commands
For simple conflicts, use checkout to pick one side:
```bash # Keep your version for a specific file git checkout --ours src/config.js
# Keep incoming version for a specific file git checkout --theirs src/config.js
# Then add to mark resolved git add src/config.js ```
Step 7: Abort If Merge Gets Messy
If conflicts are too complex and you want to start fresh:
git merge --abortThis resets everything to pre-merge state. You can then try a different approach.
Step 8: Use Git Rerere for Repeated Conflicts
If you encounter the same conflict repeatedly:
# Enable rerere (reuse recorded resolution)
git config --global rerere.enabled trueGit remembers how you resolved conflicts and auto-applies the same resolution next time.
Verify the Fix
After completing the merge:
```bash # Check merge completed successfully git status
# Should show clean state # View merge commit git log --oneline -1
# Verify file content cat src/config.js ```
Prevention Tips
- 1.Reduce conflicts by:
- 2.Small commits - Commit often, merge frequently
- 3.Coordinate with team - Don't edit same files simultaneously
- 4.Pull before pushing - Keep your branch updated
```bash # Before starting work on shared files git pull origin main
# Regularly rebase your feature branch git fetch origin git rebase origin/main ```