Git-Repo 在所有提交的文件中搜索字符串(未知分支和提交)

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

Git-Repo searching for String in all committed files (unknown branch and commit)

git

提问by Yo Ludke

I have a piece of code in another than my current branch in my git repo, and I am not sure which commit and which branch. How can I search in all files that I committed until now for a specific string (and afterwards show the surrounding of that line of code)?

我的 git repo 中的当前分支之外还有一段代码,我不确定哪个提交和哪个分支。如何在我提交的所有文件中搜索特定字符串(然后显示该行代码的周围)?

回答by John Szakmeister

Use git grepto locate the commit:

使用git grep定位承诺:

git grep "string" $(git rev-list --all)

The git rev-list --allmakes it search the entire history of the project.

git rev-list --all使它搜索项目的整个历史。

That will give an output like this:

这将给出如下输出:

<commit>:<path>:<matched line>

Then you can use git branch --containsto find out which branch the commit is on:

然后你可以使用git branch --contains来找出提交所在的分支:

git branch --contains <commit>

回答by medmunds

If the git grep "string"variation above is giving you "list too long" errors, you can use git log -Sinstead. The -S option searches the contents of the files that were committed:

如果git grep "string"上面的变体给您“列出太长”的错误,您可以git log -S改用。-S 选项搜索已提交文件的内容:

git log -S "string"  # look for string in every file of every commit
git log -S "string" -- path/to/file  # only look at the history of the named file

(More in the "Pro Git" book under "searching".)

(更多在“搜索”下的“Pro Git”一书中。)

回答by Heath Borders

If jszakmeister's answergives you an Argument list too longresponse:

如果jszakmeister 的回答给了你一个Argument list too long回应:

$ git grep "string" $(git rev-list --all)
-bash: /usr/local/bin/git: Argument list too long

You can pipe it into xargsinstead:

你可以用管道xargs代替:

$ git rev-list --all | xargs git grep "string"

回答by Alex

change the branch with git branch "branch-name"and then do git grep "specific string"in your repository root. if you don't have too many branches this should get you there quick enough.

更改分支,git branch "branch-name"然后git grep "specific string"在您的存储库根目录中执行。如果你没有太多的分支,这应该能让你足够快地到达那里。