如何在存储库中搜索特定字符串的所有Git和Mercurial提交? [英] How to search through all Git and Mercurial commits in the repository for a certain string?

查看:102
本文介绍了如何在存储库中搜索特定字符串的所有Git和Mercurial提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Git仓库,有很少的分支和悬挂的提交。我想搜索存储库中的所有这些提交以获取特定的字符串。



我知道如何获取历史记录中所有提交的日志,但这些不包含分支或摇晃的斑点,只是HEAD的历史。我想让他们都找到一个具体的提交错误。



我也想知道如何在Mercurial中执行此操作,因为我在考虑

  -g,--walk-reflogs 
不要走提交祖先链,而是从
是最近一个到最老的一个。

所以你可以这样做,在一个悬而未决的提交消息中找到一个特定的字符串:

  git log -g --grep = search_for_this 

或者,如果要搜索特定字符串的更改,可以使用镐符搜索选项-S:

  git log -g -Ssearch_for_this 
#这也适用,但可能会比较慢,它只显示文本添加结果
git grep search_for_this $(git log -g - -pretty = format:%h)

Git 1.7.4将添加-G选项,允许您传递-G 来查找包含< regexp>的行何时被移动了-S不能做。 -S只会告诉你什么时候包含字符串的行总数发生了变化(即添加/删除字符串)。

最后,您可以使用gitk来显示悬挂提交:

  gitk --all $(git log -g --pretty = format:%h)

然后使用其搜索功能查找放错位置的文件。所有这些工作假设缺少的提交没有过期并被垃圾收集,如果它悬停了30天,并且过期了reflog或运行了过期的命令,则可能会发生。


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.

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.

I would also like to know how to do this in Mercurial, as I'm considering the switch.

解决方案

You can see dangling commits with 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":

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).

Finally, you could use gitk to visualise the dangling commits with:

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.

这篇关于如何在存储库中搜索特定字符串的所有Git和Mercurial提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆