分支
- 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