Merge Conflicts: Don't Panic - Understanding and Resolving Them
Conflicts can be intimidating for beginners, but after this section, you'll face them with confidence. We'll explore why conflicts occur (Git can't read minds - it doesn't know which change is more important), how to read conflict markers, and how to choose the right solution. We'll demonstrate two approaches: manual resolution and using merge tools.
- You'll understand the nature of merge conflicts and stop fearing them - they'll become predictable.
- You'll be able to independently resolve conflicts manually by correctly reading Git markers.
- You'll learn how to set up a merge tool in your editor for visual conflict resolution.
จุดเริ่มต้นของบทเรียน
Three Types of Merge Conflicts
- Content conflicts: changes in the same lines
- Renaming conflicts: file modified and renamed
- Deletion conflicts: file deleted in one branch, modified in another
- Renaming conflicts are the trickiest for Git
- Always check file history when conflicts seem suspicious
Imagine you're working on an important project, and suddenly Git reports a conflict. The first step to resolution is understanding its type. Content conflicts occur when changes affect the same lines in different branches. Renaming conflicts are more complex: one developer changes a file's name while another edits its content. The third type involves deletions, when a file disappears in one branch but continues to exist in another. Renaming conflicts are particularly challenging because Git doesn't always track file history accurately after a name change. That's why it's important to always check file history when you encounter suspicious conflicts.
Anatomy of a Complex Conflict
- <<<<<<< markers show your changes
- ======= separates your version from theirs
- >>>>>>> marks the end of the conflict block
- git status shows all problematic files
- git diff provides detailed comparison of changes
Let's say you're merging a feature branch into main and see a conflict message. Open the problematic files - you'll notice special markers. Everything between <<<<<<< HEAD and ======= is your changes. Between ======= and >>>>>>> feature are the changes from the branch you're merging. Use git status to see all conflicting files, and git diff for a detailed view of the differences. Remember: these markers aren't errors, but guides showing where you need to make decisions.
#!/bin/bash
# conflict_analysis.sh
echo "Creating test repository"
mkdir conflict-demo && cd conflict-demo
git init
echo "Creating file and first commit in main"
echo 'Line 1' > file.txt
echo 'Line 2' >> file.txt
git add . && git commit -m 'Initial commit'
echo "Creating feature branch and modifying first line"
git checkout -b feature
sed -i 's/Line 1/Line 1 modified in feature/' file.txt
git commit -am 'Modify line 1 in feature'
echo "Returning to main and modifying the same line"
git checkout main
sed -i 's/Line 1/Line 1 modified in main/' file.txt
git commit -am 'Modify line 1 in main'
echo "Attempting to merge branches"
git merge feature || echo 'Conflict!'
echo "\nChecking status:"
git status --short
echo "\nShowing conflicted file:"
cat file.txt
Conflict Resolution Strategies
- Manual editing: remove conflict markers, keep desired content
- GUI tools: meld, kdiff3 for visual comparison
- --ours/--theirs: keep your version or accept theirs
- During rebase: resolve conflicts for each commit
- After resolution — don't forget git add and git commit
When you encounter a conflict, you have several options. You can open the file manually and edit it by removing the conflict markers and keeping the desired code. For visual learners, tools like meld or kdiff3 are excellent as they show three versions of the file simultaneously. If you're confident in your changes, use git checkout --ours to keep your version, or --theirs to accept the incoming changes. Be particularly careful during rebase operations, as conflicts may arise for each commit being rebased.
#!/bin/bash
# resolve_strategies.sh
echo "Method 1: Manual resolution"
echo 'Edit the file, removing conflict markers'
echo 'Then run: git add file.txt'
echo "\nMethod 2: Accept our version"
git checkout --ours file.txt
echo "\nMethod 3: Accept their version"
git checkout --theirs file.txt
echo "\nMethod 4: Use merge tool"
echo 'git mergetool --tool=meld'
echo "\nAfter choosing a method, complete the merge:"
echo 'git commit -m "Resolve merge conflict"'
บทเรียนฉบับเต็มใช้ได้เมื่อสมัครสมาชิก
ฉบับเต็มมีสไลด์เพิ่มอีก 34 สไลด์
วิดีโอ สไลด์ทั้งหมด โน้ต แบบทดสอบ AI ติวเตอร์ และการสัมภาษณ์จะเปิดหลังเข้าสู่ระบบและสมัครสมาชิก
เข้าสู่ระบบหรือลงทะเบียน สมัครสมาชิกเนื้อหาบทเรียน
- Three Types of Merge Conflicts
- Anatomy of a Complex Conflict
- Conflict Resolution Strategies
- Prevention and Best Practices
- Reading Multi-line Conflicts in Git
- Understanding Conflict Markers
- Nested Conflicts: Resolution Strategies
- Automation and Advanced Tools
- Strategy Choice: HEAD or Branch?
- The 'Principle of Least Surprise'
- Semantic Conflicts: The Invisible Enemies
- Tools and Automation
- Tool Selection: When to Use What
- VS Code: Configuration and Advanced Features
- Beyond Compare: Advanced Conflict Resolution Scenarios
- Git Flow Integration and Automation
- Automating Merges with .gitattributes
- Writing Your Own Merge Driver
- Git Rerere — Your Personal Assistant
- Advanced Automation Strategies
- Designing Workflow for Large Teams
- Code Review as a Conflict Prevention Tool
- Strategies for Working with Long-Lived Feature Branches
- Tools and Automation for Conflict Resolution
- Dangers of Blindly Accepting Changes
- Strategies for Safe Conflict Resolution
- Post-Merge Testing: What to Check
- CI/CD Integration and Preventative Measures
- How Rebase Conflicts Differ from Merge Conflicts
- Working with --rebase-merges
- Strategies for Resolving Sequential Conflicts
- Advanced Techniques and Rebase Debugging
- CI/CD as a Quality Control System
- GitHub Actions: Conflict Checking
- Danger-JS: Smart Check Automation
- Auto-Conflict Resolution in CI/CD
- From Conflict Panic to Confidence
บทความเกี่ยวกับบทเรียนนี้
- git merge: วิธีรวมบรานช์และแก้ความขัดแย้ง
- Git และ GitHub: ต่างกันอย่างไร และเชื่อมโยงกันอย่างไร
- วิธีติดตั้ง Git บน Windows, macOS และ Linux
- Git คืออะไร และนักพัฒนาต้องใช้เพื่ออะไร
- Git Bash: คืออะไร และใช้งานบน Windows อย่างไร
บทเรียนทั้งหมดในคอร์สนี้
- Introduction: Git from Scratch to Confident Developer
- Why Git is Essential - And Why It's a Job Requirement
- Installation and Setup: Getting Git Ready for First Use
- First Repository: init, add, commit, and the Magic of the Index
- Reading History: log, diff, show and Navigating the Past
- Branches: The Parallel Reality of Your Code
- Undoing Mistakes: restore, reset, revert — Doing It Right and the Dangers
- GitHub and Remote Repositories: Your Code in the Cloud
- Team Collaboration: Forks, Feature Branches, and Pull Requests
- Merge Conflicts: Don't Panic - Understanding and Resolving Them
- Git Workflows in Real Teams: Choosing the Right Approach
- Advanced Tools: Rebase, Stash, Cherry-pick and Tags
- Final Project: Simulating Real Team Development