在所有 Git 历史记录中搜索一个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4468361/
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:39:15  来源:igfitidea点击:

Search all of Git history for a string?

git

提问by Jorge Israel Pe?a

I have a code base which I want to push to GitHub as open source. In this git-controlled source tree, I have certain configuration files which contain passwords. I made sure not to track this file and I also added it to the .gitignorefile. However, I want to be absolutely positive that no sensitive information is going to be pushed, perhaps if something slipped in-between commits or something. I doubt I was careless enough to do this, but I want to be positive.

我有一个代码库,我想将其作为开源推送到 GitHub。在这个由 git 控制的源代码树中,我有一些包含密码的配置文件。我确保不跟踪此文件,并将其添加到.gitignore文件中。但是,我想绝对肯定的是,不会推送任何敏感信息,也许是在提交之间发生了某些事情。我怀疑我是否粗心大意这样做,但我想保持积极态度

Is there a way to "grep" all of git? I know that sounds weird, but by "all" I mean every version of every file that ever existed. I guess if there is a command that dumps the diff file for every commit, that might work?

有没有办法“grep”所有的git?我知道这听起来很奇怪,但“全部”是指曾经存在的每个文件的每个版本。我想如果有一个命令可以为每次提交转储 diff 文件,那可能有用吗?

回答by Nathan Kinsinger

Git can search diffs with the -S option (it's called pickaxe in the docs)

Git 可以使用 -S 选项搜索差异(在文档中称为 pickaxe )

git log -Spassword

This will find any commit that added or removed the string password. Here a few options:

这将找到添加或删除 string 的任何提交password。这里有几个选项:

  • -p: will show the diffs. If you provide a file (-p file), it will generate a patch for you.
  • -G: looks for differences whose added or removed line matches the given regexp, as opposed to -S, which "looks for differences that introduce or remove an instance of string".
  • --all: searches over all branches and tags; alternatively, use --branches[=<pattern>]or --tags[=<pattern>]
  • -p: 将显示差异。如果您提供一个文件 ( -p file),它将为您生成一个补丁。
  • -G: 查找添加或删除的行与给定正则表达式匹配的差异,而不是-S“查找引入或删除字符串实例的差异”。
  • --all: 搜索所有分支和标签;或者,使用--branches[=<pattern>]--tags[=<pattern>]

回答by cdhowie

git rev-list --all | (
    while read revision; do
        git grep -F 'password' $revision
    done
)

回答by kenorb

Try the following commands to search the string inside all previous tracked files:

尝试使用以下命令在所有先前跟踪的文件中搜索字符串:

git log --patch  | less +/searching_string

or

或者

git rev-list --all | GIT_PAGER=cat xargs git grep 'search_string'

which needs to be run from the parent directory where you'd like to do the searching.

它需要从您想要进行搜索的父目录运行。