Have you ever encountered this situation: you excitedly modify your code and are ready to commit, but Git suddenly throws an error saying “Conflict”? Don’t panic—this is actually a Git conflict. Today, we’ll discuss this common Git issue, from “why it happens” to “how to resolve it quickly,” so you can handle it with ease.
What is a Git Conflict?¶
In simple terms, a Git conflict occurs when two different versions of code modify the same part of the same file. Git cannot automatically determine which content to keep, so you need to manually “decide” the outcome.
Imagine: you and a colleague are editing the same document. You modify the first line of the first paragraph, and your colleague also modifies the first line of the first paragraph. When you try to pull your colleague’s changes into your version, Git gets stuck: “Which line should I keep?”—this is a conflict.
Why Do Conflicts Occur?¶
The core reason for conflicts is “modifications to the same location”. Here are the common scenarios:
-
Multiple people modifying the same file
For example, you modify line 3 of file A in your local branch (main), while a colleague modifies line 3 of file A in the remote branch (dev). When you rungit pullorgit merge dev, Git detects the conflict. -
Version differences during branch merging
If you create a new branch (feature) from main, modify line 5 of file B in feature, and the main branch is also modified at line 5 of file B by someone else, merging main into feature may cause a conflict. -
Conflicts from deletion and addition
For example, you delete a line from file C, while someone else adds content below that line. Git is confused: “Should I delete this line or keep it?”
How to Resolve Conflicts Quickly?¶
Resolving conflicts is straightforward—just remember the three steps: “Find the markers → Edit the content → Mark as resolved”. Let’s go through it with an example:
Step 1: Detect the conflict and open the conflicting file¶
When you run git pull or git merge, Git will output “Automatic merge failed” (自动合并失败). Open the problematic file (e.g., test.txt), and you’ll see conflict markers like this:
<<<<<<< HEAD
// This is your local branch (HEAD) modification
Hello World
=======
// This is the modification from the branch being merged (e.g., dev)
Hi Git
>>>>>>> dev
These symbols are added by Git to separate code versions:
- <<<<<<< HEAD: Your branch’s changes (“your version”).
- =======: A separator—above is your changes, below is the other person’s.
- >>>>>>> dev: Changes from the merged branch (“their version”).
Step 2: Edit the conflict content to resolve the discrepancy¶
The content between the conflict markers is what you need to manually choose or merge. For example, in the above case, you need to decide whether to keep “Hello World”, “Hi Git”, or combine them (e.g., “Hello Hi Git”).
Operation suggestions:
- If you only need to keep “your version”: Delete ======= and the >>>>>>> dev section, leaving only the content above <<<<<<< HEAD.
- If you only need to keep “their version”: Delete the <<<<<<< HEAD section and the content above, leaving only the content below =======.
- If you need both: Merge the content (e.g., “Hello Hi Git”) and remove all conflict markers.
After modification, the file should contain your desired final content:
Hello Hi Git
Step 3: Mark as resolved and continue¶
Once the conflict is resolved, tell Git “the conflict is fixed” by running:
git add test.txt # Mark the resolved file (add multiple files one by one or use `git add .` for all)
Then continue with the merge or pull operation:
- For merge conflicts: Run git merge --continue (or git rebase --continue if using rebase).
- For pull conflicts: Run git pull --continue.
Git will now create a new commit, and the conflict is resolved!
Pro Tip: Use Tools for Faster Resolution¶
For complex conflicts (e.g., multi-line code changes), manual editing is error-prone. Try a visual tool:
- VS Code: When opening a conflicted file, buttons like “Accept Current Change” (keep yours), “Accept Incoming Change” (keep theirs), and “Accept Both Changes” (merge both) appear on the right—just click to resolve.
- Beyond VS Code: Use command-line tools like meld (install first) or git mergetool to automatically open a comparison interface.
How to Avoid Conflicts?¶
Prevention is better than cure! Follow these habits:
1. Pull code frequently: Before starting work each day, run git pull to get the latest code and avoid large version gaps.
2. Small, focused commits: Don’t wait to commit until all code is done—split into small, functional commits. Team members should also work on different files to reduce overlap.
3. Use branches: Develop each new feature in a separate branch (git checkout -b feature/xxx). Ensure features are complete before merging to reduce conflict risks.
4. Communicate before changes: If multiple people modify the same file, sync progress in a group chat to avoid duplicate work.
Summary¶
Git conflicts are not scary—they simply mean Git needs your decision on which version to keep. Remember the three steps: “Find markers → Edit content → Mark as resolved.” Equally important is preventing conflicts with habits like “frequent pulls, small commits, clear分工 (clear division of labor).”
Next time you face a conflict, stay calm and follow these steps—you’ll be the team’s “conflict resolution expert” in no time!