首页·终端命令

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