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
11
Related Errors
General
Circular Dependency Detected
Module A depends on B which depends on A, creating an unresolvable import cycle.
DevOps
Docker Permission Denied
The current user doesn't have permission to run Docker commands.
DevOps
Kubernetes OOMKilled
A container was killed by Kubernetes because it exceeded its memory limit.
DevOps
Container Name Already in Use
A Docker container with the specified name already exists.