简体   繁体   English

如何找到提交历史记录中存在特定代码行的文件?

[英]How can I find the file that a particular line of code existed within in my commit history?

There is a line of code that I wish to recover from a previous commit as it turns out that this line of code was very important and useful. 我希望从上一次提交中恢复一行代码,因为事实证明这一行代码非常重要且有用。

For example, let's say that this is my line of code: 例如,假设这是我的代码行:

df[df$type == "creator", ]

The problem is that I can't remember the file that this code was saved in, nor can I recall where/when it was committed. 问题是我忘记了保存此代码的文件,也记不起提交的位置/时间。

I use SourceTree and have tried the Search option, but it only gives me the option to search Commit Message , File Changes and Authors . 我使用SourceTree并尝试了“ Search选项,但它只为我提供了搜索“ Commit Message ,“ File Changes和“ Authors的选项。

I use the File Changes search option but the results show me previous commits whereas I would like to see a list of files that contained the line of code at some point in time. 我使用“ File Changes搜索选项,但结果显示了先前的提交,而我希望在某个时间点查看包含代码行的文件列表。

Aside from manually looking through each version of each file in each commit, I'm not sure how to approach it. 除了手动浏览每个提交中每个文件的每个版本外,我不确定如何处理它。

How can I do this? 我怎样才能做到这一点?

Note that I am also open to suggestions/solutions using the terminal also. 请注意,我也可以通过终端使用建议/解决方案。

Update: The line of code can be in either one of three files and I know roughly the time it was committed (about two weeks ago). 更新:代码行可以在三个文件之一中,我大致知道它的提交时间(大约两周前)。

Try git log , for example: 尝试git log ,例如:

git log --name-only -S"<text-you-search-for-goes-here>"

It will print all commits in which any file contained the searched text at any point in time and for each commit it displays: 它将在任何时间点打印所有文件中包含搜索文本的所有提交,并针对每个提交显示:

  • commit hash 提交哈希
  • author 作者
  • date of the commit 提交日期
  • commit message 提交信息
  • names of files in which the searched text appeared (thanks to --name-only ) 出现搜索文本的文件的名称(由于--name-only

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM