分支
- 1. diff in Linux/Unix
- 2. diff in Git
- 3. git diff 两点语法
Linux/Unix 系统中存在diff 命令,可以用来显示两个文本/工作路径的差异。Git diff 在此基础上进行的扩展。
1. diff in Linux/Unix
Linux 系统中的diff 命令:提供了一个文件如何转化为另一个文件的正式描述。
:~/git-test/diff_test$ touch first.txt
:~/git-test/diff_test$ touch second.txt
:~/git-test/diff_test$ vim first.txt
:~/git-test/diff_test$ vim second.txt
:~/git-test/diff_test$ cat first.txt
Now is the time
For all good men
To come to the day
of their country.
:~/git-test/diff_test$ cat second.txt
Today is the time
For all good men
And women
To come to the aid
of their country.
# 对比合并格式的差异 unified diff
:~/git-test/diff_test$ diff -u first.txt second.txt
--- first.txt 2021-10-13 09:08:48.691194175 +0800 # --- 表示原始文件
+++ second.txt 2021-10-13 09:09:00.840860053 +0800 # +++ 表示新文件
@@ -1,4 +1,5 @@ # 两个文件拥有的行数
-Now is the time # -号开始的行表示新文件中删除了该行
+Today is the time # +号开始的行表示新文件中增加了该行For all good men # 空格开始的行表示两个文件中共有一致行
-To come to the day
+And women
+To come to the aidof their country.
(base) caros@caros-ThinkStation-P340:~/git-test/diff_test$
UNIX 系统中的diff命令可以计算两个目录结构中所有对应文件之间的差异
diff -r origin/src/ new/src # 命令是不是这么写待求证
2. diff in Git
git diff 命令依据参数的个数确定两个比较对象,不显示指出文件名时,默认对比两个目录下所有文件的差异。
git diff # 工作目录-暂存区
git diff commit_id # 工作目录-某次提交;commit_id=HEAD就是对比上次提交
git diff --cached commit_id # 暂存区-某次提交
git diff commit1_id commit2_id # 某两次提及
git diff xxx xxx file_name # 只对比file_name的区别(文件名前加不加--都行,为了和git checkout -- filename 一致建议加上)
git 命令的可用参数
--M # 查找重命名文件(不知道有啥用)
-w # diff 比较时忽略空白字符,还有一个详细命令 --ignore-all-spcace
--state # 统计两个树状态之间的差异,报告简介的显示有多少行发生了变化
--color # 使输出结果使用多种颜色表示,一种颜色显示一种变化。(试了一下没啥变化)
-S"string" # pickaxe 命令
Document # 只显示document的变更
git diff -S"octopus" mater~50 # matrer分支上最近50个提交中包含string 的变更。
3. git diff 两点语法
git diff 两点语法:用于显示两个提交之间的不同,两个命令等价。
git diff master bug/pr-1
git diff master..bug/pr-1. # 两个命令等价
对比git log 的两点语法:显示各自可达,又同时不可达的提交
git log commit1..commit2