Git merge conflicts occur when competing changes are made to the same lines of a file across different branches. To resolve this, you must manually edit the conflicted files to choose the correct changes, then stage and commit the result.
This error occurs when Git cannot automatically combine changes from two branches because both branches have modified the same lines in a file. Git is unable to determine which changes should take precedence, so it marks the file as conflicted and requires manual intervention. When you see "CONFLICT (content): Merge conflict in file.txt", Git has identified that the same section of code was changed in both the branch you're on and the branch you're trying to merge. The "Automatic merge failed" message confirms that Git couldn't resolve this on its own. Git uses a three-way merge algorithm that compares the two branch heads with their common ancestor. When changes on both sides affect the same lines, Git cannot determine the correct resolution and instead inserts conflict markers into the file, leaving the final decision to you.
First, identify all files with merge conflicts:
# Show the current status with conflict information
git status
# You'll see output like:
# On branch main
# You have unmerged paths.
# (fix conflicts and run "git commit")
# (use "git merge --abort" to abort the merge)
#
# Unmerged paths:
# both modified: file.txt
# both modified: src/component.jsYou can also use:
# List only conflicted files
git diff --name-only --diff-filter=UOpen each conflicted file in your editor. Git inserts special markers to show the conflicting changes:
<<<<<<< HEAD
This is the content from your current branch (HEAD)
=======
This is the content from the branch being merged
>>>>>>> feature-branchUnderstanding the markers:
- <<<<<<< HEAD: Start of your current branch's version
- =======: Separator between the two versions
- >>>>>>> feature-branch: End of the incoming branch's version
With diff3 style (recommended):
git config merge.conflictstyle diff3This shows three sections including the common ancestor:
<<<<<<< HEAD
Your changes
||||||| merged common ancestor
Original content before both changes
=======
Their changes
>>>>>>> feature-branchEdit the file to resolve the conflict. You have several options:
Option 1: Keep your version
Remove the markers and the other version:
This is the content from your current branch (HEAD)Option 2: Keep their version
Remove the markers and your version:
This is the content from the branch being mergedOption 3: Combine both versions
Merge the changes manually:
Combined content from both branchesImportant: Remove ALL conflict markers (<<<<<<<, =======, >>>>>>>). Any remaining markers will cause syntax errors in your code.
Verify the resolution:
# Search for any remaining conflict markers
grep -rn "<<<<<<" .
grep -rn "=======" .
grep -rn ">>>>>>" .After editing all conflicted files, stage them and complete the merge:
# Stage the resolved files
git add file.txt
git add src/component.js
# Or stage all resolved files at once
git add .
# Verify the resolution
git status
# Should show: "All conflicts fixed but you are still merging"
# Complete the merge with a commit
git commit
# Git will open your editor with a default merge commit message
# You can modify it or save as-isAlternatively, commit with a custom message:
git commit -m "Merge feature-branch into main, resolved conflicts in file.txt"For complex conflicts, use a visual merge tool:
# Launch the configured merge tool
git mergetool
# Git will open each conflicted file in your merge tool
# Resolve conflicts visually, save, and closeConfigure a merge tool:
# Set your preferred merge tool
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
# Other popular options:
git config --global merge.tool meld
git config --global merge.tool kdiff3
git config --global merge.tool vimdiffAvailable built-in tools:
- vimdiff, gvimdiff
- kdiff3
- meld
- tortoisemerge
- opendiff (macOS)
- emerge
Remove .orig backup files after merging:
git config --global mergetool.keepBackup false
# Or delete manually
find . -name "*.orig" -deleteIf you know you want to keep all changes from one side, use strategy options:
During merge:
# Accept all changes from the current branch (ours)
git merge -X ours feature-branch
# Accept all changes from the incoming branch (theirs)
git merge -X theirs feature-branchFor individual files during conflict resolution:
# Keep the current branch's version of a file
git checkout --ours file.txt
git add file.txt
# Keep the incoming branch's version of a file
git checkout --theirs file.txt
git add file.txtWarning: Be careful with these options. They discard changes from one side without review. Only use when you're certain which version is correct.
If you want to cancel the merge and return to the state before the merge started:
# Abort the merge and restore pre-merge state
git merge --abortThis is useful when:
- You started a merge by mistake
- The conflicts are too complex and you need to prepare first
- You want to try a different merge strategy
After aborting, you might want to:
# Update your branch first, then try again
git pull origin main
git merge feature-branch
# Or rebase instead of merge
git rebase mainNote: git merge --abort only works if you haven't committed yet. If you've already committed the merge, use:
# Undo the merge commit
git reset --hard HEAD~1### Understanding Git's Three-Way Merge
Git uses a three-way merge algorithm that compares:
1. Base (common ancestor): The commit where both branches diverged
2. Ours (HEAD): The current branch's version
3. Theirs: The incoming branch's version
Conflicts only occur when both sides change the same lines relative to the base. If only one side changed a section, Git automatically takes that change.
### Using diff3 Conflict Style
Enable diff3 to see the original (base) version in conflicts:
git config --global merge.conflictstyle diff3This helps understand what was changed on each side by showing the original content.
### Conflict Types
| Type | Message | Meaning |
|------|---------|---------|
| content | CONFLICT (content) | Same lines modified |
| add/add | CONFLICT (add/add) | Both sides added a file |
| modify/delete | CONFLICT (modify/delete) | One modified, one deleted |
| rename/rename | CONFLICT (rename/rename) | Both renamed differently |
| rename/delete | CONFLICT (rename/delete) | One renamed, one deleted |
### Rerere: Reuse Recorded Resolution
Git can remember how you resolved conflicts and apply the same resolution automatically:
# Enable rerere globally
git config --global rerere.enabled true
# Git will now record conflict resolutions
# Future identical conflicts are resolved automatically### Avoiding Merge Conflicts
Best practices:
- Pull/rebase frequently to stay in sync
- Keep feature branches short-lived
- Communicate with team about files being modified
- Use smaller, focused commits
- Consider using trunk-based development
### Resolving During Rebase
Rebase conflicts work similarly but need different commands:
# After resolving conflicts during rebase
git add <resolved-files>
git rebase --continue
# Or abort the rebase
git rebase --abort
# Skip the current commit
git rebase --skip### The ORT Merge Strategy
Git 2.33+ uses the "ORT" (Ostensibly Recursive's Twin) strategy by default, which is faster and handles some edge cases better than the older "recursive" strategy.
### Checking for Conflicts Before Merging
# Do a dry-run merge to see if conflicts would occur
git merge --no-commit --no-ff feature-branch
git diff --cached
git merge --abort### Common IDE Integration
Most IDEs have built-in merge conflict resolution:
- VS Code: Click on conflict markers for options
- IntelliJ/WebStorm: VCS > Git > Resolve Conflicts
- Sublime Merge: Visual conflict resolution
- GitKraken: Interactive conflict editor
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git