%%
# 纲要
> 主干纲要、Hint/线索/路标
# Q&A
#### 已明确
#### 待明确
> 当下仍存有的疑惑
**❓<font color="#c0504d"> 有什么问题?</font>**
# Buffer
## 闪念
> sudden idea
## 候选资料
> Read it later
%%
# git diff 说明
`git diff` 可用于比较:**不同提交之间**、**特定提交与当前工作区**、**不同分支**之间的差异。
![[_attachment/05-工具/git 使用/git diff 比较.assets/IMG-git diff 比较-B989CE4AC833D6EE56E89D87273AD751.png|595]]
| 命令 | 说明 |
| ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------- |
| `git diff [<path>...]` | 比较 **工作区相较于==暂存区==** 之间的差异 |
| | |
| `git diff <commit> [<path>...]` | 比较 **工作区相较于==指定提交==** 之间的差异 <br>(例如 `git diff HEAD`,查看工作区相较于最近一版提交间的差异) |
| | |
| `git diff --staged [<commit>] [<path>...]` | 比较 **==暂存区==相较于==指定提交==** 之间的差异(默认 HEAD) |
| | |
| `git diff commit1 commit2 [<path>...]` | 比较**两个不同提交——commit2 相较于 commit1 的差异** |
| `git diff branch1 branch2 [<path>...]` | 比较**两个不同分支**——**分支2 相较于分支1 的差异**<br>(分支本质上是指向"commit" 的引用,语法同上) |
| | |
| `git diff commit1...commit2 [<path>...]` | 比较 commit2 相较于 "**commit1 与 commit2 共同祖先提交**" 的差异 <br>(等价于 `git diff $(git merge-base commit1 commit2) commit2`) |
其中 `[<path>...]` 指示文件路径
选项:
- `--stat`: 以**统计信息的形式**显示**文件的更改情况——新增 or 移除的文件,文件添加、删除的行数**。
![[_attachment/05-工具/git 使用/git diff 比较.assets/IMG-git diff 比较-0D1259D7D86DAE9ED751F5A969232F02.png|442]]
> [!caution] `diff` **基于端点(即提交的快照)进行比较**,**并不支持范围语法**。
>
> - `git diff commit1..commit2` 实际上等价于 `git diff commit1 commit2`;
> - `git diff commit1...commit2` 实际上等价于 `git diff $(git merge-base A B) B`;
>
> ![[_attachment/05-工具/git 使用/git diff 比较.assets/IMG-git diff 比较-C04226BE4507C25C406EEE7EFE1F2B77.png|755]]
>
> [!NOTE] git diff 命令中的 `--` 为分隔符,用于**明确区分命令参数和文件路径**
>
> `--` 指示其之后的内容被视为**文件或路径**,而非命令选项或参数。
> 示例:`git diff HEAD -- example.txt`,查看 `example.txt` 文件在**工作区与上一版提交**之间的差异
>
<br><br><br>
# gif diff 输出格式说明
git diff 输出格式采用的是 diff 工具的 "**Unified Format**",参见 [[05-工具/GNU 工具/diff 工具#统一格式(Unified Format)|diff 工具-统一格式]]
> [!info] git diff 输出格式说明
>
> ![[_attachment/05-工具/git 使用/git diff 比较.assets/IMG-git diff 比较-0A06BB7D57ABEAE8EB617A3136FA8FF2.png]]
>
> [!caution] 可能存在某些行先被删除再被重新添加的情况,似乎与“CRLF 行尾与 LF”有关。
<br><br><br>
# 配置使用 VSCode 的 diff view 进行比较
参见[^1]
![[_attachment/05-工具/git 使用/git diff 比较.assets/IMG-git diff 比较-20400C528CCB5B5242B048E927E2BCFF.png|575]]
上述设置后,使用 `git difftool` 比较两个分支 or 两个文件时,将**打开 VSCode 并显示 diff view 窗口**。
<br><br><br>
# git diff 使用总结 🌠
###### ❓如何比较**同一个文件在不同commit、不同分支、暂存区与工作区之间的差异**?
- **工作区相较于暂存区**—— `git diff <file>`
- **工作区相较于特定提交**—— `git diff <commit> <file>`
- **暂存区相较于特定提交**—— `git diff --staged [commit] <file>`(省略提交时默认 HEAD)
- **commit2 相较于 commit1**—— `git diff commit1 commit2 <file>`
- **分支 2 相较于分支 1**—— `git diff branch1 branch2 <file>`
###### ❓如何比较**增减的文件列表**?
- => 使用 `--stat` 选项
<br><br>
# 参考资料
# Footnotes
[^1]: [How can I see 'git diff' on the Visual Studio Code side-by-side file? - Stack Overflow](https://stackoverflow.com/questions/51316233/how-can-i-see-git-diff-on-the-visual-studio-code-side-by-side-file)