Introduction
Merge conflicts in Git occur when two branches have made edits to the same part of a file and Git is unable to automatically resolve these changes. Understanding how to address these conflicts is essential for smooth collaboration and maintaining a clean project history.
Context
Scenario 1: Merge
When merging branches (e.g., merging featureA
into main
), conflicts may arise:
- Switch to the main branch:
- Merge the feature branch:
Scenario 2: Rebase
Conflicts can also occur during rebase operations:
- Rebase a feature branch onto the main branch:
Conflict Example
Conflicts are indicated in the file with markers:
<<<<<<<< HEAD
if x == 0:
return false
========
if y == 6:
return true
elif x == 0:
return false
>>>>>>> d123456
Resolving the Conflict
Manually
- Open the file in a text editor:
- Resolve the conflict by choosing one of the options:
- Option 1: Keep “OUR” change (current branch)
- Option 2: Keep “THEIR” change (other branch)
- Option 3: Write a new resolution
Using GUI
- Run
git mergetool
for a GUI-based conflict resolution tool like Meld.
Using vim’s Fugitive
- Write how to resolve merge conflicts using Fugitive.
Using Lazygit
- Write how to resolve merge conflicts using Lazygit.
Continuing After Resolution
- Stage the resolved files:
- Check for remaining conflicts:
- Continue the merge or rebase:
- For merge:
- For rebase:
Additional Notes
- The
--ours
and--theirs
options can be used in bothmerge
andrebase
scenarios to choose changes from the current branch or the incoming branch, respectively. - Always ensure conflicts are fully resolved and the code is functional before continuing with merges or rebases.
Understanding and resolving merge conflicts is a critical skill in Git, enabling smoother integration of changes from different branches.