本文翻译自:How to search through all Git and Mercurial commits in the repository for a certain string?
I have a Git repository with few branches and dangling commits. 我有一个Git存储库,有很少的分支和悬挂提交。 I would like to search all such commits in repository for a specific string. 我想在存储库中搜索所有此类提交以获取特定字符串。
I know how to get a log of all commits in history, but these don't include branches or dangling blobs, just HEAD's history. 我知道如何记录历史上所有提交的日志,但这些不包括分支或悬空blob,只是HEAD的历史记录。 I want to get them all, to find a specific commit that got misplaced. 我希望得到所有这些,找到一个错位的特定提交。
I would also like to know how to do this in Mercurial, as I'm considering the switch. 我也想知道如何在Mercurial中做到这一点,因为我正在考虑转换。
#1楼
参考:https://stackoom.com/question/38fi/如何在存储库中搜索特定字符串的所有Git和Mercurial提交
#2楼
if you are a vim user, you can install tig (apt-get install tig), and use /, same command to search on vim 如果您是vim用户,可以安装tig(apt-get install tig),并使用/,相同的命令在vim上搜索
https://blogs.atlassian.com/2013/05/git-tig/ https://blogs.atlassian.com/2013/05/git-tig/
#3楼
To add just one more solution not yet mentioned, I had to say that using gitg's graphical search box was the simplest solution for me. 要添加一个尚未提及的解决方案,我不得不说使用gitg的图形搜索框对我来说是最简单的解决方案。 It will select the first occurrence and you can find the next with Ctrl-G. 它将选择第一个匹配项,您可以使用Ctrl-G找到下一个匹配项。
#4楼
One command in git that I think it's much easier to find a string: 在git中的一个命令,我认为找到一个字符串要容易得多:
git log --pretty=oneline --grep "string to search"
works in Git 2.0.4 适用于Git 2.0.4
#5楼
Building on rq's answer, I found this line does what I want: 基于rq的答案,我发现这条线做了我想要的:
git grep "search for something" $(git log -g --pretty=format:%h -S"search for something")
Which will report the commit ID, filename, and display the matching line, like this: 这将报告提交ID,文件名,并显示匹配的行,如下所示:
91ba969:testFile:this is a test
... Does anyone agree that this would be a nice option to be included in the standard git grep command? ...有没有人同意这是一个很好的选择,可以包含在标准的git grep命令中?
#6楼
Any command that takes references as arguments will accept the --all
option documented in the man page for git rev-list
as follows: 任何以引用作为参数的命令都将接受git rev-list
手册页中记录的--all
选项,如下所示:
--all
Pretend as if all the refs in $GIT_DIR/refs/ are listed on the
command line as <commit>.
So for instance git log -Sstring --all
will display all commits that mention string
and that are accessible from a branch or from a tag (I'm assuming that your dangling commits are at least named with a tag). 因此,例如git log -Sstring --all
将显示所有提交string
提交,并且可以从分支或标记访问(我假设您的悬挂提交至少以标记命名)。