Skip to content

Git Code Conflict Resolution Complete Guide (Git Clash)

In team collaboration development, Git Conflict is almost inevitable. When two developers modify the same line of code in the same file at the same time, or one deletes a file while the other modifies it, Git cannot automatically decide which version to use, resulting in a Conflict (Clash).

This article provides an in-depth explanation of the causes of conflicts, identification methods, resolution steps, and how to use modern tools like VS Code to handle conflicts efficiently.

1. Why Do Conflicts Occur?

Git's auto-merge strategy is powerful and can handle most parallel modifications. However, in the following scenarios, Git requires human intervention:

  • Same Line Modification: Branch A and Branch B both modified line 10 of index.html.
  • File Deletion vs Modification: Branch A deleted config.js, while Branch B modified the content of config.js.
  • Rebase: When using git rebase to organize commit history, the old base overlaps with new changes.

2. Identifying Conflicts

When you execute git merge, git pull, or git rebase, if a conflict occurs, the terminal will prompt:

bash
Auto-merging src/App.js
CONFLICT (content): Merge conflict in src/App.js
Automatic merge failed; fix conflicts and then commit the result.

At this point, use git status to view exactly which files are in a conflict state:

bash
$ git status
On branch feature-login
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/App.js

3. Understanding Conflict Markers

Open the conflicted file, and you will see special markers inserted by Git, dividing the conflicting content into three parts:

javascript
<<<<<<< HEAD
const apiUrl = 'https://dev.api.com';  // Current Change
=======
const apiUrl = 'https://prod.api.com'; // Incoming Change
>>>>>>> feature/new-api
  • <<<<<<< HEAD: Start of conflict. Below is the content of the current branch (or target base during Rebase).
  • =======: Separator. Above is the current branch content, below is the content of the branch being merged in.
  • >>>>>>> feature/new-api: End of conflict. Marks the name or Commit ID of the source branch.

4. Practical: Steps to Resolve Conflicts

This is the most basic general resolution process, applicable to all editors.

Step 1: Manual Editing

Open the conflicted file and decide whether to keep or discard code based on requirements. You have three choices:

  1. Keep Current Change: Delete content below =======.
  2. Keep Incoming Change: Delete content above =======.
  3. Keep Both/Mix: Manually rewrite the code to combine the logic of both.

After Processing (Ensure the remaining code is syntactically correct and remove <<<, ===, >>> markers):

javascript
// Assuming we need to judge dynamically based on environment
const apiUrl = process.env.NODE_ENV === 'production' 
  ? 'https://prod.api.com' 
  : 'https://dev.api.com';

Step 2: Mark as Resolved

After saving the file, run in the terminal:

bash
git add src/App.js

All resources are from the open-source community. Disclaimer