如何在存储库中搜索某个字符串的所有 Git 和 Mercurial 提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/746684/
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
How to search through all Git and Mercurial commits in the repository for a certain string?
提问by Josip
I have a Git repository with few branches and dangling commits. I would like to search all such commits in repository for a specific string.
我有一个 Git 存储库,其中有几个分支和悬空提交。我想在存储库中搜索所有此类提交以查找特定字符串。
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. I want to get them all, to find a specific commit that got misplaced.
我知道如何获取历史中所有提交的日志,但这些不包括分支或悬垂的 blob,只包括 HEAD 的历史。我想得到它们,找到一个错位的特定提交。
I would also like to know how to do this in Mercurial, as I'm considering the switch.
我也想知道如何在 Mercurial 中做到这一点,因为我正在考虑转换。
回答by richq
You can see dangling commits with git log -g
.
你可以看到悬空提交git log -g
。
-g, --walk-reflogs
Instead of walking the commit ancestry chain, walk reflog entries from
the most recent one to older ones.
So you could do this to find a particular string in a commit message that is dangling:
因此,您可以这样做以在悬空的提交消息中查找特定字符串:
git log -g --grep=search_for_this
Alternatively, if you want to search the changes for a particular string, you could use the pickaxe search option, "-S":
或者,如果要搜索特定字符串的更改,可以使用镐搜索选项“-S”:
git log -g -Ssearch_for_this
# this also works but may be slower, it only shows text-added results
git grep search_for_this $(git log -g --pretty=format:%h)
Git 1.7.4 will add the -G option, allowing you to pass -G<regexp> to find when a line containing <regexp> was moved, which -S cannot do. -S will only tell you when the total number of lines containing the string changed (i.e. adding/removing the string).
Git 1.7.4 将添加 -G 选项,允许您通过 -G<regexp> 来查找包含 <regexp> 的行何时被移动,而 -S 不能这样做。-S 只会告诉您包含字符串的总行数何时更改(即添加/删除字符串)。
Finally, you could use gitk to visualise the dangling commits with:
最后,您可以使用 gitk 来可视化悬空提交:
gitk --all $(git log -g --pretty=format:%h)
And then use its search features to look for the misplaced file. All these work assuming the missing commit has not "expired" and been garbage collected, which may happen if it is dangling for 30 days and you expire reflogs or run a command that expires them.
然后使用其搜索功能查找放错位置的文件。所有这些工作都假设丢失的提交没有“过期”并被垃圾收集,如果它悬空 30 天并且您使引用日志过期或运行使它们过期的命令,则可能会发生这种情况。
回答by Martin Geisler
In Mercurial you use hg log --keyword
to search for keywords in the commit messages and hg log --user
to search for a particular user. See hg help log
for other ways to limit the log.
在 Mercurial 中,您可以hg log --keyword
在提交消息中搜索关键字并hg log --user
搜索特定用户。有关hg help log
限制日志的其他方法,请参阅。
回答by Jakub Nar?bski
In addition to richq answerof using git log -g --grep=<regexp>
or git grep -e <regexp> $(git log -g --pretty=format:%h)
: take a look at the following blog posts by Junio C Hamano, current git maintainer
除了使用or 的richq答案:看看Junio C Hamano的以下博客文章,当前的git维护者git log -g --grep=<regexp>
git grep -e <regexp> $(git log -g --pretty=format:%h)
Summary
概括
Both git grepand git log --grepare line oriented, in that they look for lines that match specified pattern.
这两个混帐的grep和--grep git的日志是面向行的,在他们查找符合指定模式的行。
You can use git log --grep=<foo> --grep=<bar>
(or git log --author=<foo> --grep=<bar>
that internally translates to two --grep
) to find commits that match eitherof patterns (implicit ORsemantic).
您可以使用git log --grep=<foo> --grep=<bar>
(或git log --author=<foo> --grep=<bar>
在内部转换为 two --grep
)来查找与任一模式(隐式OR语义)匹配的提交。
Because of being line-oriented, the useful ANDsemantic is to use git log --all-match --grep=<foo> --grep=<bar>
to find committhat has bothline matching first and line matching second somewhere.
由于是面向行的,有用的AND语义git log --all-match --grep=<foo> --grep=<bar>
用于查找在某处同时具有第一个行匹配和第二个行匹配的提交。
With git grep
you can combine multiple patterns (all which must use the -e <regexp>
form) with --or
(which is the default), --and
, --not
, (
and )
. For grep --all-match
means that filemust have lines that match each of alternatives.
随着git grep
您可以将多个模式(所有这一切都必须使用组合-e <regexp>
与形式)--or
(这是默认), ,,和。对于 grep意味着文件必须具有与每个替代项匹配的行。--and
--not
(
)
--all-match
回答by Sam Hiatt
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 命令中的一个不错的选择?
回答by adl
Any command that takes references as arguments will accept the --all
option documented in the man page for git rev-list
as follows:
任何将引用作为参数的命令都将接受--all
手册页中记录的选项git rev-list
,如下所示:
--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
并且可以从分支或标签访问(我假设您的悬空提交至少用标签命名)。
回答by Yawar
With Mercurial you do a
使用 Mercurial,您可以
$ hg grep "search for this" [file...]
There are other options that narrow down the range of revisions that are searched.
还有其他选项可以缩小搜索的修订范围。
回答by Kurt Schelfthout
Don't know about git, but in Mercurial I'd just pipe the output of hg log to some sed/perl/whatever script to search for whatever it is you're looking for. You can customize the output of hg log using a template or a style to make it easier to search on, if you wish.
不知道 git,但在 Mercurial 中,我只是将 hg log 的输出通过管道传输到一些 sed/perl/任何脚本,以搜索您正在寻找的任何内容。如果愿意,您可以使用模板或样式自定义 hg log 的输出,以便更轻松地进行搜索。
This will include all named branches in the repo. Mercurial does not have something like dangling blobs afaik.
这将包括回购中的所有命名分支。Mercurial 没有类似悬空的 blob 之类的东西。
回答by Hymantrade
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上搜索
回答by icarito
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. It will select the first occurrence and you can find the next with Ctrl-G.
再补充一个尚未提及的解决方案,我不得不说使用 gitg 的图形搜索框对我来说是最简单的解决方案。它将选择第一个匹配项,您可以使用 Ctrl-G 找到下一个。
回答by Adriano Rosa
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