Git Merge Conflict
Git cannot automatically merge changes from two branches because they modified the same lines.
Explanation
Git merge conflict occurs when Git cannot automatically combine changes from two branches because both branches modified the same lines in the same file. Git marks the conflicting sections with conflict markers (<<<<<<<, =======, >>>>>>>). Common causes include two developers editing the same file, rebasing branches that have diverged, cherry-picking commits with overlapping changes, and merging feature branches that have been rebased. Merge conflicts must be manually resolved before the merge can complete.
Common Causes
- Two branches modified same lines
- Long-lived feature branches
- Rebasing diverged branches
- Multiple developers editing same file
- Cherry-picking overlapping commits
Solution
Open the conflicted files and look for conflict markers. Decide which changes to keep: keep yours, keep theirs, or combine both. Remove the conflict markers after resolving. Use git status to see which files have conflicts. Use git mergetool for a visual merge tool. After resolving all conflicts, git add the files and git commit. Use smaller, more frequent merges to reduce conflict scope.
Code Example
# See conflicted files
git status
# both modified: app/Http/Controllers/UserController.php
# View conflict markers in file
<<<<<<< HEAD
public function index() {
return User::all(); // your changes
}
=======
public function index(Request $request) {
return User::paginate(10); // their changes
}
>>>>>>> feature-branch
# After resolving
git add app/Http/Controllers/UserController.php
git commit -m "Merge feature-branch, resolve conflict"
Error Information
Language
bash
Difficulty
Beginner
Views
9
Related Errors
ENOSPC
DevOps
Out of Disk Space
The system or application ran out of disk space.
DevOps
Kubernetes OOMKilled
A container was killed by Kubernetes because it exceeded its memory limit.
DevOps
Terraform State Locked
Another Terraform operation is already running and has locked the state file.
CrashLoopBackOff
DevOps
Kubernetes CrashLoopBackOff
A container is repeatedly crashing and restarting in Kubernetes.