Resolving Merge Conflicts
You'll learn: How to manually fix merge conflicts by editing files, choosing which changes to keep, and completing the merge successfully.
When a merge conflict occurs (as you learned previously), Git pauses the merge and marks the conflicting sections in your files. Now it's your job to resolve them manually.
What Does a Conflict Look Like?
Git inserts special markers directly into the conflicting file:
<<<<<<< HEAD
Your current branch's code here
=======
The other branch's code here
>>>>>>> other-branch-name
Everything between <<<<<<< HEAD and ======= is what exists in your current branch. Everything between ======= and >>>>>>> other-branch-name is what's coming from the branch you're merging in.
The Resolution Process
Step 1: Open the conflicted file in your text editor and locate the conflict markers.
Step 2: Decide what to keep. You have three options:
- Keep only your changes (delete the other branch's lines and all markers)
- Keep only the incoming changes (delete your lines and all markers)
- Keep both, or combine them in a new way (edit as needed, remove all markers)
Step 3: Remove all conflict markers (<<<<<<<, =======, >>>>>>>). The file must be clean, valid code.
Step 4: Stage the resolved file with git add filename.
Step 5: Complete the merge with git commit (Git will provide a default merge commit message).
Think of It Like This
Imagine two editors marking up the same document with red pens. Git shows you both sets of edits side-by-side and asks, "Which version is correct?" You make the final call, clean up the markup, and declare it resolved.
Key Takeaway: Resolving merge conflicts means manually editing files to choose or combine changes, removing Git's conflict markers, staging the fixed files, and committing to complete the merge.