Git 3c 🧩 Resolving Merge Conflicts
3c Resolving Merge Conflicts
Understanding What Causes Conflicts
Merge conflicts occur when Git cannot automatically merge changes from two different branches because the same lines of code have been modified in both branches. This typically happens when:
- Same file, same lines modified: Both branches have changed the same portion of a file
- Different files with similar content: When different branches modify related functionality
- Branches that have diverged significantly: When there are many changes across multiple files
Git detects these conflicts automatically and marks them in the affected files, preventing automatic merging to avoid data loss or incorrect code behavior.
Identifying Conflict Markers
When Git encounters a conflict, it inserts special markers in the conflicted file:
<<<<<<< HEAD
console.log("Hello World");
var message = "Welcome";
=======
console.log("Hello Universe");
var message = "Greetings";
>>>>>>> branch-name
The markers work as follows:
- <<<<<<< HEAD: Beginning of changes from your current branch
- =======: Separator between conflicting changes
>>>>>>>branch-name: End of changes from the branch being merged
Strategies for Resolving Merge Conflicts
Manual Resolution Process
- Open the conflicted file in your text editor
- Examine the conflict markers and decide which changes to keep or combine
- Edit the file to remove conflict markers and create the desired final code
- Stage the resolved file using git add
- Complete the merge with git commit
Example Resolution:
Before:
<<<<<<< HEAD
function greetUser() {
console.log("Hello World");
var message = "Welcome";
return message;
}
=======
function greetUser() {
console.log("Hello Universe");
var message = "Greetings";
return message;
}
>>>>>>> feature-branch
After resolution:
function greetUser() {
console.log("Hello Universe");
var message = "Greetings";
return message;
}
Complete Example with Commands
Let’s walk through a full merge conflict scenario:
- Create and switch to main branch
git checkout main
git pull origin main
- Make changes in main branch
// main.js
function calculateTotal(items) {
var total = 0;
for (var i = 0; i < items.length; i++) {
total += items[i].price;
}
return total;
}
- Create and switch to feature branch
git checkout -b feature/payment - Make conflicting changes in feature branch
// main.js
function calculateTotal(items) {
var total = 0;
for (var i = 0; i < items.length; i++) {
total += items[i].price * 1.1; // Added tax calculation
}
return total;
}
- Switch back to main and make different changes:
git checkout main - Modify the same function in main
// main.js
function calculateTotal(items) {
var total = 0;
for (var i = 0; i < items.length; i++) {
total += items[i].price;
total += items[i].tax; // Added tax addition
}
return total;
}
- Try to merge feature branch into main
git merge feature/payment - Git will report conflict and show file with markers
Auto-merging main.js
CONFLICT (content): Merge conflict in main.js
Automatic merge failed; fix conflicts and then commit the result.
- Open the conflicted file and resolve
// main.js
function calculateTotal(items) {
var total = 0;
for (var i = 0; i < items.length; i++) {
total += items[i].price;
total += items[i].tax;
}
return total;
}
- Stage and commit the resolution
git add main.js
git commit -m "Resolved merge conflict in calculateTotal function"
Best Practices for Merge Conflicts
- Communication: Discuss conflicts with team members before resolving
- Test thoroughly: Ensure resolved code works correctly after merging
- Keep changes minimal: Only resolve what’s necessary, not everything
- Use tools: Leverage Git GUI tools or editors with merge conflict resolution capabilities
- Document decisions: Add comments explaining why certain conflicts were resolved a particular way
Common Commands for Conflict Management
- git status: Shows conflicted files
- git diff: Displays differences in conflicted files
- git checkout –theirs filename: Accept changes from the branch being merged
- git checkout –ours filename: Keep changes from current branch
- git mergetool: Opens a visual merge tool
- git reset –merge: Aborts the merge process
Can you answer these questions
- What are the three main conflict markers that Git inserts when there’s a merge conflict?
- Why does Git prevent automatic merging when conflicts occur?
- How can you identify which files have merge conflicts after attempting a merge?
- What happens if you don’t resolve all conflicts before committing a merge?
- What is the difference between git checkout –theirs and git checkout –ours?
- Can you use git add on only some of the conflicted files, or must you add them all at once?
- How can you abort a merge operation if you want to start over?
- What are the advantages of using a visual merge tool like git mergetool over manual editing?
- Why might someone choose to resolve conflicts by accepting changes from their current branch instead of the merged branch?
- How does Git determine which lines in a file have conflicts when there are multiple conflicting changes?
- What is the proper sequence of commands to complete a merge after resolving all conflicts?
- What are some strategies for minimizing merge conflicts during collaborative development?
- How can you use git diff to better understand what changed in conflicted files?
- When would you prefer to resolve a conflict using git checkout –theirs over manual editing?
- What is the purpose of the git reset –merge command?