Resolving merge conflicts in Git is a common task that occurs when there are conflicting changes in different branches.
Step 1: Identify the Conflict
Check Status:
git status
This command will show you which files are conflicted.
Step 2: Open the Conflicted Files
- Use an Editor: Open the conflicted file(s) in your code editor. Git will mark the conflicting sections within the file.
Step 3: Understand the Conflict Markers
Conflict Markers: Git uses special markers to highlight conflicting sections.
<<<<<<< HEAD // Changes from your branch ======= // Changes from the incoming branch >>>>>>> branch-name
The content between
<<<<<<< HEAD
and=======
is your local change and the content between=======
and>>>>>>> branch-name
is the incoming change.
Step 4: Resolve the Conflict
Manually Edit:
Modify the conflicting sections manually to include the changes you want to keep.
Remove the conflict markers.
Choose Changes: Decide which changes to keep. You might want to keep both sets of changes or discard one in favor of the other.
Step 5: Mark as Resolved
Mark as Resolved: After manually resolving the conflicts, mark the files as resolved.
git add path/to/conflicted/file
Step 6: Verify Resolution
Check Status:
git status
Ensure that the conflicted file is now in the staging area.
Step 7: Complete the Merge
Complete Merge:
git merge --continue
If using
git pull
, it might automatically complete the merge.
Step 8: Commit the Merge
Commit Changes:
git commit
This opens your configured editor to write a merge commit message.
Step 9: Abort Merge (Optional)
Abort Merge: If something goes wrong and you want to start over, you can abort the merge.
git merge --abort
Additional Tips:
Visual Tools: Consider using visual tools like Git GUI or integrated tools in your code editor for a more graphical representation of conflicts.
Review Changes: Before committing, review the changes to ensure that you've resolved the conflicts correctly.
Communication: If you're collaborating with others, communicate any changes you make to ensure everyone is on the same page.