Resolving Conflicts
What Causes Conflicts?
Conflicts occur when: - Two branches modify the same line in the same file - A file is deleted in one branch but modified in another - Different changes are made to the same file
Identifying Conflicts
When a merge or rebase fails due to conflicts:
git status
Shows files with conflicts.
Conflict Markers
Git marks conflicts with:
<<<<<<< HEAD
Your changes
=======
Incoming changes
>>>>>>> branch-name
Resolving Process
- Edit the conflicted files
- Remove conflict markers
- Choose which changes to keep
- Stage the resolved files
- Commit the resolution
# Edit file and resolve conflicts
# Then stage
git add resolved-file.txt
# Commit
git commit
Tools for Resolving Conflicts
Command Line
# See differences
git diff
# See what you changed
git diff HEAD
# See what they changed
git diff MERGE_HEAD
GUI Tools
# Use mergetool
git mergetool
# Configure mergetool
git config --global merge.tool vimdiff
Abort Merge/Rebase
# Abort merge
git merge --abort
# Abort rebase
git rebase --abort
Conflict Resolution Strategies
Accept Yours
git checkout --ours file.txt
git add file.txt
Accept Theirs
git checkout --theirs file.txt
git add file.txt
Manual Resolution
Edit the file manually and remove markers.
Preventing Conflicts
- Communicate with team
- Pull frequently
- Use feature branches
- Keep commits small and focused
Binary Files
For binary files, you must choose one version:
# Keep your version
git checkout --ours binary-file.jpg
# Keep their version
git checkout --theirs binary-file.jpg
