首頁·終端機命令

git blame

git

用法

git blame [-L <start>,<end>] <file>

為每一行標出最後改動它的提交;只做格式化的提交會蓋住真正的作者,這就是 -w 和 --ignore-rev 的用處。

常用選項

選項含義
-L <a>,<b>only those lines
-wignore whitespace, so re-indenting stops hiding the author
-Cfollow code moved in from another file
--ignore-rev <rev>skip a commit, e.g. a bulk reformat
-eshow email addresses instead of names
-sdrop the author and date columns

範例

git blame -L 40,60 src/app.ts

who last touched lines 40 to 60

git blame -w -C src/app.ts

looks past whitespace and moved code

git blame --ignore-rev 9f3c2ab src/app.ts

ignores the commit that reformatted everything

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

怎麼看

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

常見問題

Q. git blame 是做什麼的?

為每一行標出最後改動它的提交;只做格式化的提交會蓋住真正的作者,這就是 -w 和 --ignore-rev 的用處。

Q. 怎麼寫?

git blame [-L <start>,<end>] <file> —— 方括號表示可以省略的部分。

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

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

相關命令

man 手冊: man git-blame