如何在存储库中搜索特定字符串的所有Git和Mercurial提交?

本文介绍如何在Git和Mercurial存储库中搜索特定字符串的所有提交。覆盖了从基本的日志搜索到使用高级命令如git grep和git log --grep的方法,以及在多个分支和悬挂提交中进行搜索的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自: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提交,并且可以从分支或标记访问(我假设您的悬挂提交至少以标记命名)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值