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
Git-Repo searching for String in all committed files (unknown branch and commit)
提问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 grep
to locate the commit:
使用git grep
定位承诺:
git grep "string" $(git rev-list --all)
The git rev-list --all
makes 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 --contains
to 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 -S
instead. 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 long
response:
如果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 xargs
instead:
你可以用管道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"
在您的存储库根目录中执行。如果你没有太多的分支,这应该能让你足够快地到达那里。