【问题标题】:How to search (git grep) changes in specific commit and file type?如何搜索(git grep)特定提交和文件类型的更改?
【发布时间】:2021-01-15 18:18:27
【问题描述】:

我想搜索特定提交中的所有更改。请注意,我只想搜索更改,而不是整个更改的文件。

这是我尝试过的:

$ git grep -A5 -E -e 'search_term\(.*?,'  <commit_hash>

我似乎无法将其限制为仅一次提交。返回的结果还包括来自在此提交中未更改的文件的搜索结果。

如何将搜索限制为特定的文件名和类型?我可以限制扩展,但不能使用如下所示的模式:

$ git grep -A5 -E -e 'search_term\(.*?,'  <commit_hash> -- '*.ts
... works
$ git grep -A5 -E -e 'search_term\(.*?,'  <commit_hash> -- 'file_prefix.*\.ts
... does not work!

【问题讨论】:

  • 如果你想搜索差异,为什么不搜索差异呢? git diff thatcommit~ thatcommit -- your path patterns here.
  • @jthill 我也想搜索上下文,即搜索中包含任何更改之前的 N 行和之后的 M 行。如何扩展git diff 输出中显示的“上下文”数量?

标签: git grep


【解决方案1】:

看看

  • git log -G &lt;pattern&gt; # 及其各种选项
  • git log -S # 及其各种选项
  • git show &lt;hash&gt;
  • git log -p ... | grep

有很多不同的方法可以做到这一点。使用上面的命令和git help &lt;command&gt;,您应该能够找到您正在寻找的具体内容。

【讨论】:

  • Git 日志没有帮助。因为日志只知道更改而不知道文件。我不能要求它使用git log ... | grep ... 在某个搜索词之前获取 5 行。如果我可以使用第三方工具,我就不会使用git grep
  • 好的,这似乎与您最初的问题完全相反“我想搜索特定提交中的所有更改。请注意,我只想搜索更改,而不是整个更改的文件。”跨度>
猜你喜欢
  • 2020-12-22
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-08
  • 1970-01-01
  • 2020-12-08
相关资源
最近更新 更多