In collaborative software development, Git acts like a coordinator. However, when two or more people modify the same part of a file simultaneously, Git may find itself “at a loss” — this is what we commonly refer to as a code conflict. Conflicts are not Git’s fault but an inevitable “磨合” (adjustment) in collaboration. With the right resolution process, you can handle them easily.

1. Why Do Code Conflicts Occur?

Imagine you and your colleague Xiaoming are both modifying the greet() function in a Python file:
- You changed the greeting from “Hello” to “Hi”
- Xiaoming simultaneously changed the greeting variable from name to username

When you and Xiaoming finish your code and attempt to merge your changes into the main branch, Git detects that the same file was modified by multiple people and cannot automatically determine which content to retain. This triggers a conflict.

2. 5 Key Steps to Resolve Conflicts

1. Before Collaboration: “Sync” Before Starting (Prevent Conflicts)

Before modifying code, ensure your local code is up-to-date to avoid conflicts caused by “outdated local files.”
Action:

# Pull the latest remote code (e.g., from the main branch)
git pull origin main

Why: If your local branch lags far behind the remote, the probability of conflicts during subsequent merges increases.

2. When Conflicts Occur: Git “Stops” the Process

When you run git merge or git pull and conflicts arise, Git will automatically prompt:

Automatic merge failed; fix conflicts and then commit the result.

First, check which files have conflicts:

git status

Git will mark the conflicting files (e.g., greet.py) and prompt you to resolve them manually.

3. View Conflicts: Locate the “Battlefield”

Open the conflicting file (e.g., greet.py). Git marks the conflicting regions with special symbols:

<<<<<<< HEAD  # Your local branch code (HEAD points to the current branch)
print("Hi, " + name)
=======  # Separator
print("Hello, " + username)
>>>>>>> feature/xxx  # Code from another branch (e.g., feature/xxx branch)
  • <<<<<<< HEAD to =======: Your local changes
  • ======= to >>>>>>> feature/xxx: Changes from another branch

The core of the conflict is that these two parts need manual judgment for merging.

4. Manually Resolve Conflicts: “Coordinate” Between Versions

Conflicts do not resolve automatically. You must decide which code to retain or merge based on business logic.
Example:
If Xiaoming wants to change the variable name back to name and you changed the greeting, you need to decide:
- Keep “Hi, ” + name (your greeting) and correct the variable name to username?
- Or keep “Hello, ” + username (Xiaoming’s variable name)?
Correct Action:
1. Remove the conflict markers (<<<<<<<, =======, >>>>>>>)
2. Merge the changes according to business logic (e.g., retain “Hi, ” + username)
3. Ensure the code is syntactically correct and logically consistent

5. Mark as Resolved and Commit

After resolving the conflict, inform Git that the conflict is handled, then complete the merge:

# 1. Mark the conflicted file as "resolved"
git add greet.py  

# 2. Commit the merge result (automatically generates a merge commit message)
git commit  

# 3. Push to the remote (others should confirm it’s okay if it’s a collaborative repo)
git push origin main

Note: After merging, double-check the code to ensure no syntax errors or logical issues.

3. 3 Practical Tips to Avoid Conflicts

Prevent conflicts rather than resolving them:
1. Small, Frequent Commits: Modify one feature at a time and commit early. Avoid bundling changes.
2. Frequent Syncs: Pull remote code at the start of the day and sync periodically (e.g., git pull every hour).
3. Clear Division of Labor: Agree with the team on “who modifies which module” to avoid overlapping file edits.

4. Summary

Code conflicts are a “normal part of collaborative development.” The core resolution logic is:
“Prevent first, manually judge conflicts, and confirm after resolution”

It may feel cumbersome initially, but remembering “sync, check, and small steps” will make collaboration smoother!

Final Note: Git can merge code, but it cannot decide “right vs. wrong” code. Conflict resolution requires team communication.

Xiaoye