Conflict Resolution

Merge conflicts happen when you merge branches that have competing commits, and Git needs your help to decide which changes to incorporate in the final merge.

Think of Git merge conflicts like two chefs trying to modify the same recipe simultaneously. One chef adds more salt, while the other removes salt entirely - Git doesn't know which version to keep!

Git can often resolve differences between branches and merge them automatically. Usually, the changes are on different lines, or even in different files, which makes the merge simple for computers to understand. However, sometimes there are competing changes that Git can't resolve without your help.

Use Cases:
Preventing Conflicts:
Step-by-Step Conflict Resolution:
1. First, identify which files are in conflict. VS Code streamlines the process automatically indicating where there are conflicts using conflict markers. You can also use the git status command to see which files have conflicts:
git status
Files marked as "both modified" are the ones you need to fix
2. Open the conflicted file and locate the conflict markers:
<<<<<<< HEAD
your local changes
=======
incoming changes
>>>>>>> branch-name
These markers show you both versions of the conflicting code.
Conflict Markers shown in screenshots
You can directly edit the file in the screenshot above, and remove the markers, keeping the changes you desire. Alternatively you can use the buttons to Accept Incoming (Changes on the branch you are merging into), or Accept Current (Changes on your feature branch (HEAD)). Accepting a Combination will take both Current and HEAD changes (often stacking them on top of each other).
3. Useful CLI commands during conflict resolution:
git diff # Show current conflicts
git log --merge # Show commits that caused the conflict
git checkout --ours fileName # Keep your changes
git checkout --theirs fileName # Keep their changes
4. After deciding which changes to keep, mark file as resolved:
git add [resolved-file-name]
This tells Git "I've fixed this file, trust me!"
5. If things get too messy, you can always start over:
git merge --abort # Abort the merge
git reset --hard HEAD # Reset to last commit (careful!)
These are your escape hatches - they return everything to pre-merge state
6. Once all conflicts are resolved, complete the merge:
git commit -m "Resolved merge conflicts"

Congratulations! You've successfully handled a merge conflict! 🎉