git - 如何找到删除了某些代码的 Git 提交?

标签 git

我向 repo 协议(protocol)添加了一些代码行并提交了它。然后提交次数之后,这些行已被删除。

我有添加行时的提交散列和删除它们的后续提交的散列。我怎样才能找到它们被删除的确切提交?

最佳答案

您可以使用 -S-G git log 的选项.它们之间的区别在于 -S采用固定字符串(代表您要匹配的代码行),而 -G做同样的事情,但采用正则表达式:

git log --oneline -S 'fixed string'
git log --oneline -G '^foobar$'

-S-G选项将返回首先引入或删除字符串/正则表达式模式的提交。您甚至可以添加 --patch-p如果您想查看提交匹配项的差异,请选择此选项。

文档

来自official Linux Kernel git log documentation :

-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 - 如何找到删除了某些代码的 Git 提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24193759/

相关文章:

git - 从 GitHub 上不存在的 fork 存储库向 GitHub 项目 pull 请求

git stash 更改适用于新分支?

具有相同名称但大小写不同的文件的 Git rebase 问题

git - 如何防止 git format-patch 截断文件名

java - 如何适应多种编码风格? (git 与 IDE)

svn:externals git 项目的子文件夹

git - 为来自 Ubuntu 的现有文件创建一个新的 BitBucket 存储库

git - git commit占用了多少磁盘空间

java - 引导项目 merge 新变化

git - 如何提供对 Composer 使用的 git 存储库的访问