ตัวอย่างบทเรียน

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.

merge conflict conflict markers conflict resolution
คุณจะเรียนรู้อะไรในบทเรียนนี้
  • 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 ติวเตอร์ และการสัมภาษณ์จะเปิดหลังเข้าสู่ระบบและสมัครสมาชิก

เข้าสู่ระบบหรือลงทะเบียน สมัครสมาชิก

เนื้อหาบทเรียน

  1. Three Types of Merge Conflicts
  2. Anatomy of a Complex Conflict
  3. Conflict Resolution Strategies
  4. Prevention and Best Practices
  5. Reading Multi-line Conflicts in Git
  6. Understanding Conflict Markers
  7. Nested Conflicts: Resolution Strategies
  8. Automation and Advanced Tools
  9. Strategy Choice: HEAD or Branch?
  10. The 'Principle of Least Surprise'
  11. Semantic Conflicts: The Invisible Enemies
  12. Tools and Automation
  13. Tool Selection: When to Use What
  14. VS Code: Configuration and Advanced Features
  15. Beyond Compare: Advanced Conflict Resolution Scenarios
  16. Git Flow Integration and Automation
  17. Automating Merges with .gitattributes
  18. Writing Your Own Merge Driver
  19. Git Rerere — Your Personal Assistant
  20. Advanced Automation Strategies
  21. Designing Workflow for Large Teams
  22. Code Review as a Conflict Prevention Tool
  23. Strategies for Working with Long-Lived Feature Branches
  24. Tools and Automation for Conflict Resolution
  25. Dangers of Blindly Accepting Changes
  26. Strategies for Safe Conflict Resolution
  27. Post-Merge Testing: What to Check
  28. CI/CD Integration and Preventative Measures
  29. How Rebase Conflicts Differ from Merge Conflicts
  30. Working with --rebase-merges
  31. Strategies for Resolving Sequential Conflicts
  32. Advanced Techniques and Rebase Debugging
  33. CI/CD as a Quality Control System
  34. GitHub Actions: Conflict Checking
  35. Danger-JS: Smart Check Automation
  36. Auto-Conflict Resolution in CI/CD
  37. From Conflict Panic to Confidence

บทความเกี่ยวกับบทเรียนนี้

บทเรียนทั้งหมดในคอร์สนี้

  1. Introduction: Git from Scratch to Confident Developer
  2. Why Git is Essential - And Why It's a Job Requirement
  3. Installation and Setup: Getting Git Ready for First Use
  4. First Repository: init, add, commit, and the Magic of the Index
  5. Reading History: log, diff, show and Navigating the Past
  6. Branches: The Parallel Reality of Your Code
  7. Undoing Mistakes: restore, reset, revert — Doing It Right and the Dangers
  8. GitHub and Remote Repositories: Your Code in the Cloud
  9. Team Collaboration: Forks, Feature Branches, and Pull Requests
  10. Merge Conflicts: Don't Panic - Understanding and Resolving Them
  11. Git Workflows in Real Teams: Choosing the Right Approach
  12. Advanced Tools: Rebase, Stash, Cherry-pick and Tags
  13. Final Project: Simulating Real Team Development