首頁·終端機命令

git diff

git

用法

git diff [--staged] [<commit>] [-- <path>]

只顯示還沒暫存的改動;所以剛 git add 完看起來是空的,那時要用 --staged。

常用選項

選項含義
--stagedcompare the staged snapshot with HEAD (same as --cached)
--statsummary of files and line counts instead of the patch
-wignore whitespace changes
--name-onlylist changed paths only
<a>...<b>changes on b since it forked from a
-- <path>restrict to a path; the -- keeps it from being read as a branch

範例

git diff

shows only what is NOT staged yet

git diff --staged

shows what git commit would record

git diff main...feature

the work feature added since it left main

難的地方在於復原有好幾種:把分支的指向挪走,和補一個反向提交,不是一回事。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. git diff 是做什麼的?

只顯示還沒暫存的改動;所以剛 git add 完看起來是空的,那時要用 --staged。

Q. 怎麼寫?

git diff [--staged] [<commit>] [-- <path>] —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 6 個,完整列表在 man git。這條命令屬於git。

相關命令

man 手冊: man git-diff