SoFunction
Updated on 2025-04-14

Detailed guide to resolving Git code conflict issues

introduction

In team collaboration development, Git is the most commonly used version control tool, but when multiple people modify the same file at the same time, they will inevitably encounter code conflicts (Conflict). How to resolve conflicts efficiently is a skill that every developer must master.

This article will systematically explain the causes, solutions, and prevention techniques of Git code conflicts, and help you thoroughly master the conflict handling process through code examples and practical demonstrations.

1. The causes of Git code conflicts

1. What is code conflict

When multiple developers modify the same part of the code in the same file and try to merge (merge/rebase/pull), Git cannot automatically decide which version to retain, and it will prompt a conflict and need to be resolved manually.

2. Typical conflict scenarios

git merge: Conflicts when merging branches.

git rebase: time-to-base conflict.

git pull: Conflicts when pulling remote code (essentially git fetch + git merge).

3. The underlying mechanism of conflict

Git uses three-way merge algorithm to compare file differences:

  • Local Version (HEAD)
  • Remote version (target branch)
  • Common ancestor version (Base)

If the same line is modified in both HEAD and remote versions, Git cannot be merged automatically, and a conflict will be triggered.

2. The entire process of Git conflict resolution

1. Confirm conflict files

Run git status and the conflicting file will appear as Unmerged paths:

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

2. View conflicting content

When opening a conflicting file (such as src/), Git will mark the conflicting part with a special tag:

&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD
("This is a local modification");
=======
("This is a remote modification");
&gt;&gt;&gt;&gt;&gt;&gt;&gt; feature-branch
  • <<<<<<<<< HEAD to ======: Local code
  • ======= To >>>>>>> feature-branch: Remote code

3. Manually resolve conflicts

Choose one of the following methods according to your needs:

(1) Keep the local code

("This is a local modification");

(2) Keep the remote code

("This is a remote modification");

(3) Manually merge the two

("This is the merged code");

4. Tag conflict resolved

git add src/  # Tag conflict resolvedgit commit          # Submit merge results

Git will automatically generate merge commit information, such as:

Merge branch 'feature-branch' into main

5. Special circumstances handling

(1) Give up merge (return conflict)

git merge --abort   # Terminate mergegit rebase --abort  # Terminate rebase

(2) Use graphical tools (such as VS Code)

git mergetool       # Call the configuration difference comparison tool

3. Advanced conflict resolution skills

1. Use git diff to view conflicting differences

git diff            # View unstaged conflictsgit diff --cached   # View temporary conflicts

2. Use git checkout --ours/theirs to quickly select a version

git checkout --ours src/    # Keep the local versiongit checkout --theirs src/  # Keep the remote version

3. Automatically log conflict resolution using git rerere

git config --global  true  # Turn on rerere

Git will remember how to resolve conflicts and will automatically apply the same conflict next time you encounter it.

4. How to prevent code conflicts

1. Submit small steps to reduce the probability of conflict

Avoid submitting large amounts of code at once.

Use git commit -m "Description" to submit small function points.

2. Frequently pull the latest code

git pull origin main --rebase  # Use rebase instead of merge to reduce conflicts

3. Use branch policies

Main branch (main/master): is only used for publication, and direct modification is prohibited.

Feature-xxx: Create independent branches when developing new functions.

Pull Request (PR): Pre-merge code review to find conflicts in advance.

4. Teamwork norms

Communicate before modifying public documents.

Use .gitattributes to define a merge policy (such as binaries forbidden merge).

5. Practical demonstration: From conflict to resolution

Scene simulation

You modified it in the main branch:

# Project Introduction
This is a local modification

A colleague modified the same line in feature-branch and pushed:

# Project Introduction
This is a remote modification

The conflict is triggered when you try to merge.

Solution steps

git pull origin feature-branch
# Discover conflicts and modify them manuallygit add 
git commit -m "Resolve conflicts"
git push origin main

6. Summary

Key points illustrate
Causes of conflict Multiple people modify the same file and the same line
Solution Manual edit →git add → git commit
Preventive measures Small step submission, frequent pulling, branch strategy

Mastering Git conflict resolution can greatly improve team collaboration efficiency. It is recommended to practice moremergeandrebase, familiar with the handling methods in different scenarios.

This is the article about this detailed guide to resolving Git code conflict problems. For more related Git code conflict resolution content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!