全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-11 5 分钟 ✍️ juanwangdev

识别冲突文件

合并发生冲突时,Git 会明确告知哪些文件存在冲突,需要手动解决。

查看冲突状态

Bash
# 查看当前状态
git status

# 输出示例
On branch main
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/main.go
        both modified:   config.yaml

冲突状态标识

状态标识说明
both modifiedUU双方都修改了
deleted by themUD他们删除了我们修改
deleted by usDU我们删除了他们修改
both addedAA双方都新增了
both deletedD D双方都删除了

使用 git status

Bash
# 详细查看冲突
git status

# 简洁模式
git status -s

# 输出
UU src/main.go    # 冲突文件
AA config.yaml    # 冲突文件
M  README.md      # 已暂存的修改

查看冲突文件列表

Bash
# 只查看冲突文件
git diff --name-only --diff-filter=U

# 输出
src/main.go
config.yaml

# 查看冲突文件数量
git diff --name-only --diff-filter=U | wc -l

查看冲突详情

Bash
# 查看冲突内容概要
git diff

# 查看特定文件的冲突
git diff src/main.go

# 使用图形化工具查看
git mergetool

冲突时的 git status 输出

Bash
$ git status
On branch main
Merging branch 'feature'

Conflicting paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)

        both modified:      src/login.go
        deleted by them:    src/old.go

no changes added to commit

快速定位冲突文件

Bash
# 方法1:grep 搜索冲突标记
grep -r "<<<<<<< HEAD" .

# 方法2:使用 Git 命令
git ls-files -u

# 输出
100644 a1b2c3d 1 src/login.go  # Base 版本
100644 d4e5f6 2 src/login.go  # HEAD 版本
100644 g7h8i9 3 src/login.go  # 合入版本

冲突解决流程

text
1. git status          查看冲突文件列表
2. 逐个打开冲突文件    解决冲突
3. git add <file>      标记已解决
4. git status          确认所有冲突已解决
5. git commit          完成合并

解决冲突前先用 git status 确认所有冲突文件,避免遗漏。

要点总结

  1. git status 查看冲突文件列表
  2. 状态标识:UU(双方修改)、UD/DU(删除冲突)
  3. --diff-filter=U 过滤只看冲突文件
  4. grep -r "<<<<<<< HEAD" 快速定位冲突位置
  5. 解决所有冲突后再提交

📝 发现内容有误?点击此处直接编辑

← 上一篇 手动解决冲突
下一篇 → Fast-Forward 合并
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库