gpt4 book ai didi

git - 是否可以在一个Git项目的所有分支中执行一个 'grep search'?

转载 作者:IT王子 更新时间:2023-10-29 01:22:01 27 4
gpt4 key购买 nike

是否可以在 Git 控制源项目的所有分支中运行 git grep?还是有其他命令要运行?

最佳答案

问题“How to grep (search) committed code in the git history?”建议:

 git grep <regexp> $(git rev-list --all)

搜索所有提交,其中应包括所有分支。

另一种形式是:

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

您可以在 this article 中找到更多示例:

I tried the above on one project large enough that git complained about the argument size, so if you run into this problem, do something like:

git rev-list --all | (while read rev; do git grep -e <regexp> $rev; done)

(请参阅下面本答案最后一部分的备选方案)

如果需要,请不要忘记这些设置:

# Allow Extended Regular Expressions
git config --global grep.extendedRegexp true
# Always Include Line Numbers
git config --global grep.lineNumber true

这个别名也有帮助:

git config --global alias.g "grep --break --heading --line-number"

2016 年 8 月更新:R.M.在评论中推荐

I got a "fatal: bad flag '->' used after filename" when trying the git branch version. The error was associated with a HEAD aliasing notation.

I solved it by adding a sed '/->/d' in the pipe, between the tr and the xargs commands.

 git branch -a | tr -d \* | sed '/->/d' | xargs git grep <regexp>

即:

alias grep_all="git branch -a | tr -d \* | sed '/->/d' | xargs git grep"
grep_all <regexp>

这是对解决方案 chernjie 的改进有suggested ,因为 git rev-list --all 太过分了。

A more refined command can be:

# Don't use this, see above
git branch -a | tr -d \* | xargs git grep <regexp>

Which will allow you to search only branches (including remote branches)

You can even create a bash/zsh alias for it:

# Don't use this, see above  
alias grep_all="git branch -a | tr -d \* | xargs git grep"
grep_all <regexp>

关于git - 是否可以在一个Git项目的所有分支中执行一个 'grep search'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15292391/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com