git GitHub:搜索旧版本的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4705517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:52:07  来源:igfitidea点击:

GitHub: searching through older versions of files

gitgithub

提问by normski

I know that using GitHub I can search through all the current versions of my files in a repo. However, I would also like to search through the older versions of my repo files. For example, say, I used to have a function called get_info() in my code, but deleted it several versions ago, is it possible to search for get_info and find the code. If it is not possible using GitHub, is it possible from the git command line?

我知道使用 GitHub 我可以在存储库中搜索所有当前版本的文件。但是,我还想搜索我的 repo 文件的旧版本。比如我的代码里曾经有个函数叫get_info(),但是好几个版本之前删掉了,能不能搜索get_info,找到代码。如果无法使用 GitHub,是否可以从 git 命令行?

EDIT

编辑

Thanks to @Mark Longair for showing how this can be done from the git command line. If it's not possible in GitHub it would be a great feature to have.

感谢@Mark Longair 展示了如何从 git 命令行完成此操作。如果在 GitHub 中无法实现,那将是一个很棒的功能。

回答by Mark Longair

Currently, I don't believe it's possible to search within the complete history of a repository's code on the github website - the closest is just searching within the current code of a repository with the "code search" option on this page.

目前,我认为不可能在 github 网站上的存储库代码的完整历史记录中进行搜索 - 最接近的只是使用此页面上的“代码搜索”选项在存储库的当前代码中进行搜索。

However, from the command line, you can find any commits that introduced or removed lines mentioning get_infowith the -Soption to git log. e.g.:

但是,从命令行中,您可以找到任何引入或删除get_info带有-S选项的行的提交git log。例如:

git log -Sget_info -p

(n.b. there should be no space between -Sand the search term)

(注意-S和搜索词之间不应有空格)

(also note: to search for more than one word, surround in '):

(另请注意:要搜索多个单词,请用 括起来'):

git log -S'get info' -p

So, at a minimum that should find the commit where the function was first introduced and the one that removed it. I added the -pso you can also see the patches - if lots of commits introduced changes that mentioned the function that may be helpful. If the function was only on another branch it might also be useful to use --allto search all branches.

因此,至少应该找到首次引入该函数的提交和删除它的提交。我添加了-p所以你也可以看到补丁 - 如果大量提交引入了提到可能有用的功能的更改。如果该函数仅在另一个分支上,则用于--all搜索所有分支也可能很有用。

Jefromipoints out in a comment below that git 1.7.4 will introduce the -Goption as an alternative - this change is summarized in a recent blog post from Junio Hamano (git maintainer): http://gitster.livejournal.com/48191.html

Jefromi在下面的评论中指出,git 1.7.4 将引入该-G选项作为替代方案 - 此更改在 Junio Hamano(git 维护者)最近的一篇博客文章中进行了总结:http: //gitster.livejournal.com/48191.html