Conflict Resolution
此内容尚不支持你的语言。
Intuition
Section titled “Intuition”When two people edit the same sentence: Merge conflicts are like two people editing the same paragraph in a document simultaneously, Git can not automatically decide which version is correct, so it asks you to choose or combine them manually.
Why it matters: Conflicts are inevitable in team projects. Understanding how to resolve them quickly and correctly prevents lost work, corrupted merges, and frustrated developers.
The key insight: The best way to handle conflicts is to prevent them, small, frequent merges reduce the likelihood of conflicts, and good communication about who is working on what avoids overlapping changes.
Understanding Conflicts
Section titled “Understanding Conflicts”A merge conflict occurs when Git’s three-way merge algorithm cannot automatically reconcile changes from two branches. This happens when both branches modify the same region of the same file in different ways.
When Conflicts Occur
Section titled “When Conflicts Occur”flowchart TD
A["Both branches modify<br/>different files"] --> B["Auto-merge: no conflict"]
C["Both branches modify<br/>different regions of the same file"] --> B
D["One branch modifies,<br/>other deletes the same file"] --> E["Conflict"]
F["Both branches modify<br/>the same region of the same file"] --> E
G["Both branches add<br/>a file with the same name<br/>(different content)"] --> E
H["One branch renames,<br/>other modifies the original"] --> I["Possible auto-resolve<br/>(rename detection)"]
style B fill:#e8f5e9
style E fill:#ffcdd2
style I fill:#fff3e0Conflict Markers
Section titled “Conflict Markers”When a conflict occurs, Git writes conflict markers into the file:
<<<<<<< HEADint authenticate(User *user) { return verify_password(user->password, user->stored_hash);}=======int authenticate(User *user) { Token *token = jwt_sign(user, SECRET_KEY); return token->value;}>>>>>>> feature-auth| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Start of conflict region. Our version begins. |
======= | Separator between our version and their version. |
>>>>>>> feature-auth | End of conflict region. Their version ends. |
The text after >>>>>>> identifies the branch being merged.
Multi-Base Conflicts
Section titled “Multi-Base Conflicts”When both sides of a conflict have been merged from a common branch, Git may produce multi-base conflict markers (with the ort merge strategy):
<<<<<<< HEADour version||||||| merged common ancestorsbase version=======their version>>>>>>> feature-authThe middle section (between ||||||| and =======) shows the common ancestor’s version, which can be helpful for understanding what changed on both sides.
Resolution Strategies
Section titled “Resolution Strategies”Strategy 1: Manual Resolution
Section titled “Strategy 1: Manual Resolution”The most common approach, edit the file to produce the correct result:
// After manual resolution:int authenticate(User *user) { // Verify password first (from HEAD) if (!verify_password(user->password, user->stored_hash)) return NULL;
// Generate JWT token (from feature-auth) Token *token = jwt_sign(user, SECRET_KEY); return token->value;}Then stage and commit:
git add src/auth.cgit commit # (or git merge --continue / git rebase --continue)