我向一个repo添加了几行代码并提交了它。然后在若干次提交之后,这些行被删除了。
我有添加这些行时的提交哈希值,以及一个随后将其删除的提交哈希值。如何找到确切删除它们的提交?
我有添加这些行时的提交哈希值,以及一个随后将其删除的提交哈希值。如何找到确切删除它们的提交?
您可以使用-S
或-G
选项来调用git log
命令。它们之间的区别在于,-S
接受一个固定字符串(表示您要匹配的代码行),而-G
也会做相同的事情,但是它接受一个正则表达式:
git log --oneline -S 'fixed string'
git log --oneline -G '^foobar$'
-S
和-G
选项可以返回首次引入或移除字符串/正则表达式模式的提交。如果您想查看与提交匹配的差异,甚至可以添加--patch
或-p
选项。根据官方Linux内核的git log文档说明:
-S <string>
Look for differences that introduce or remove an instance of
<string>
. Note that this is different than the string simply appearing in diff output; see the pickaxe entry in gitdiffcore(7) for more details.
-G <regex>
Look for differences whose added or removed line matches the given
<regex>
.
git-blameall
。它显示了文件中存在的每一行,并包含删除行的提交。对于这种取证工作非常有用。