合并 Conflicts
Resolving 合并 conflicts 步骤通过步骤 — 理解 conflict markers and resolution 策略.
你将学到
- 如何 读取 and 理解 conflict markers
- 步骤-通过-步骤 conflict resolution workflow
- Using 合并 tools and aborting conflicts
- 策略到 minimize conflicts in 首先位置
概念讲解
What Is a Merge Conflict?
A merge conflict occurs when two branches change the same lines of the same file and Git cannot determine which version to keep. Git pauses the merge and asks you to resolve the conflict manually.
git merge feature-branch
# Auto-merging login.js
# CONFLICT (content): Merge conflict in login.js
# Automatic merge failed; fix conflicts and then commit the result.
Conflicts are normal and nothing to fear — they happen every day in team development.
Conflict Markers
Git marks the conflicting section in the file:
function login(user) {
<<<<<<< HEAD
const token = generateToken(user);
return { token, user };
=======
const session = createSession(user);
return { session, user };
>>>>>>> feature-branch
}
<<<<<<< HEAD— your current branch's version (the one you are merging INTO)=======— the separator>>>>>>> feature-分支— the incoming branch's version (the one you are merging FROM)
The Resolution Workflow
- Identify the conflicts:
git 状态shows files with conflicts as "两者都修改" - Open each conflicted file and find the
<<<<<<<markers - Edit the file to keep the correct version (or combine both)
- Remove the conflict markers (
<<<<<<<,=======,>>>>>>>) - Stage the resolved file:
git 添加 <file> - Complete the merge:
git 提交(orgit 合并 --continue)
Choosing the Resolution
You have several options for each conflict:
- Keep your version (HEAD) — delete the incoming code and markers
- Keep their version (incoming) — delete your code and markers
- Combine both — merge the logic from both versions
- Write something new — replace the entire conflicted section
For simple conflicts, you can use checkout to choose a side:
git checkout --ours conflicted-file.js # keep our version
git checkout --theirs conflicted-file.js # keep their version
git add conflicted-file.js
Using a Merge Tool
Configure and use a visual merge tool for complex conflicts:
git config --global merge.tool vscode
git mergetool
VS Code, IntelliJ, and other editors have excellent visual merge tools that show three panels: yours, theirs, and the result.
Aborting a Merge
If you are not ready to resolve conflicts:
git merge --abort
This cancels the merge and returns to the pre-merge state. You can always try again later.
Minimizing Conflicts
- Pull frequently — keep your branch up to date with main
- Communicate with your team — avoid editing the same files simultaneously
- Keep commits small and focused — smaller changes mean smaller conflicts
- Use feature branches — isolate work to reduce overlap
- Rebase before merging —
git 拉取 --rebasekeeps your branch linear
Verifying the Resolution
After resolving, always verify your code works:
git add .
git commit # complete the merge
# Run your tests to make sure nothing broke
npm test
Never complete a merge without testing that the combined code actually works.
示例代码
# Simulate a conflict: two branches change the same line
git checkout -b feature-a
echo "Version A" > config.txt
git commit -am "feat: set config to version A"
git checkout main
echo "Version B" > config.txt
git commit -am "feat: set config to version B"
# Now merge — this will conflict
git merge feature-a
# CONFLICT (content): Merge conflict in config.txt
# See which files have conflicts
git status
# both modified: config.txt
# Open config.txt — it now contains:
# <<<<<<< HEAD
# Version B
# =======
# Version A
# >>>>>>> feature-a
# Edit the file to resolve — keep one or combine:
echo "Version A and B combined" > config.txt
# Stage the resolved file
git add config.txt
# Complete the merge
git commit -m "merge: resolve config.txt conflict"
# --- Alternative: keep one side entirely ---
git merge another-branch
git checkout --ours config.txt # keep our version
git add config.txt
git commit
# --- If overwhelmed, abort ---
git merge --abort
# --- Use a visual merge tool ---
git mergetool
# After resolving, verify everything works
npm test
Simulates conflict 通过有 two 分支更改相同 file, shows conflict markers, resolves 通过编辑 file, stages and 提交. 也 shows --我们的/--它们的 shortcuts, aborting, and using 合并 tool. 总是运行测试之后 resolving conflicts.
动手试一试
- Code Diff
Compare conflicting versions 边通过边 到 decide resolution.
- .gitignore Generator
Keep generated files out 的 Git 到 reduce 合并 conflicts.
常见错误
错误写法
Leaving conflict markers in file and committing 它们
正确做法
之后 resolving conflict, 搜索 for <<<<<<<, =======, and >>>>>>> markers in 你的 files 之前 committing. 这些 markers 必须所有是 移除. 运行 git diff --staged 到验证最终状态, and 总是运行你的测试之后 resolving.
错误写法
Panicking at conflict and force-resetting, losing 工作
正确做法
Conflicts 是 normal. 如果你 是不 ready, 运行 git 合并 --abort 到 safely 取消 and 返回到 pre-合并状态. 从不 use git reset --困难到 escape conflict — 它能 lose uncommitted 工作. Take 你的时间, resolve one file at 时间, and 测试 afterward.
相关资源
相关速查表
- Git Cheatsheet
Conflict markers, resolution workflow, and 合并 --abort.
练习 合并 Conflicts
2 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。