使用 Git,我如何在所有分支中搜索字符串?

发布于 2024-11-30 14:20:29 字数 135 浏览 2 评论 0原文

使用 Git,我如何在所有本地分支的所有文件中搜索给定的字符串?

GitHub 具体:是否可以在所有 GitHub 分支上执行上述搜索? (我的远程 GitHub 存储库上有几个远程分支,理想情况下我不必为这次搜索而关闭它们......)

Using Git, how could I search within all files in all local branches for a given string?

GitHub specific: is it possible to perform the above search across all GitHub branches? (There are several remote branches on my remote GitHub repository that ideally I wouldn't have to bring down for this search...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

场罚期间 2024-12-07 14:20:29

您可以在 Git 存储库上执行此操作:

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

GitHub 高级搜索具有代码搜索功能:

代码搜索将查找 GitHub 上公开托管的所有代码。您还可以按以下条件进行过滤:

  • 语言:语言:
  • 存储库名称(包括用户名):repo:
  • 文件路径:路径:

You can do this on a Git repository:

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

GitHub advanced search has code search capability:

The code search will look through all of the code publicly hosted on GitHub. You can also filter by:

  • the language: language:
  • the repository name (including the username): repo:
  • the file path: path:
泡沫很甜 2024-12-07 14:20:29

如果您使用 @manojlds Git grep 命令并收到错误:

-bash: /usr/bin/git: Argument list too long" 

那么您应该使用 xargs

git rev-list --all | xargs git grep "string/regexp"

另请参阅如何 grep(搜索)提交Git 历史记录中的代码

If you use @manojlds Git grep command and get an error:

-bash: /usr/bin/git: Argument list too long" 

then you should use xargs:

git rev-list --all | xargs git grep "string/regexp"

Also see How to grep (search through) committed code in the Git history

丑丑阿 2024-12-07 14:20:29

在许多情况下, git rev-list --all 可能会返回大量提交,需要很长时间才能扫描。如果您不想搜索存储库历史记录中每个分支上的每个提交,而只想搜索所有分支提示,则可以将其替换为 git show-ref -s --heads 。因此总共:

git grep "string" `git show-ref -s --heads`

或:

git show-ref -s --heads | xargs git grep "string"

提示:您可以在文件中写入输出以在编辑器中查看:

nano ~/history.txt
git show-ref -s --heads | xargs git grep "search string here" >> ~/history.txt

In many cases git rev-list --all can return a huge number of commits, taking forever to scan. If you, instead of searching through every commit on every branch in your repository history, just want to search all branch tips, you can replace it with git show-ref -s --heads. So in total:

git grep "string" `git show-ref -s --heads`

or:

git show-ref -s --heads | xargs git grep "string"

Tip: You can write output in file to view in an editor:

nano ~/history.txt
git show-ref -s --heads | xargs git grep "search string here" >> ~/history.txt
浮生未歇 2024-12-07 14:20:29

此处列出的解决方案存在一些问题(甚至已被接受)。

您不需要列出所有哈希值,因为您会得到重复项。而且,还需要更多的时间。

它建立在这个基础上,您可以在多个分支 masterdev 上搜索字符串 "test -f /"

git grep "test -f /" master dev

相同

printf "master\ndev" | xargs git grep "test -f /"

与此处 去。

这会查找所有本地分支尖端的哈希值,并仅在这些提交中搜索:

git branch -v --no-abbrev | awk -F' *' '{print $3}' | xargs git grep "string/regexp"

如果您也需要在远程分支中搜索,则添加 -a

git branch -a -v --no-abbrev | awk -F' *' '{print $3}' | xargs git grep "string/regexp"

进一步:

# Search in local branches
git branch | cut -c3- | xargs git grep "string"

# Search in remote branches
git branch -r | cut -c3- | xargs git grep "string"

# Search in all (local and remote) branches
git branch -a | cut -c3- | cut -d' ' -f 1 | xargs git grep "string"

# Search in branches, and tags
git show-ref | grep -v "refs/stash" | cut -d' ' -f2 | xargs git grep "string"

There are a few issues with the solutions listed here (even accepted).

You do not need to list all the hashes as you'll get duplicates. Also, it takes more time.

It builds on this where you can search a string "test -f /" on multiple branches master and dev as

git grep "test -f /" master dev

which is same as

printf "master\ndev" | xargs git grep "test -f /"

So here goes.

This finds the hashes for the tip of all local branches and searches only in those commits:

git branch -v --no-abbrev | awk -F' *' '{print $3}' | xargs git grep "string/regexp"

If you need to search in remote branches too then add -a:

git branch -a -v --no-abbrev | awk -F' *' '{print $3}' | xargs git grep "string/regexp"

Further:

# Search in local branches
git branch | cut -c3- | xargs git grep "string"

# Search in remote branches
git branch -r | cut -c3- | xargs git grep "string"

# Search in all (local and remote) branches
git branch -a | cut -c3- | cut -d' ' -f 1 | xargs git grep "string"

# Search in branches, and tags
git show-ref | grep -v "refs/stash" | cut -d' ' -f2 | xargs git grep "string"
深巷少女 2024-12-07 14:20:29

你可以试试这个:

git log -Sxxxx  # Search all commits
git log -Sxxxx  --branches[=<pattern>]   # Search branches

You can try this:

git log -Sxxxx  # Search all commits
git log -Sxxxx  --branches[=<pattern>]   # Search branches
冷情妓 2024-12-07 14:20:29

关注 @peter-mortensen & manojlds 的解决方案,我使用 git for-each-ref 作为子命令来仅列出具有名称的分支。

git grep "string/regexp" $(git for-each-ref --format='%(refname:short)' refs/heads)

这实现了更好的可视化,仅显示命名分支并为每个分支仅生成一个结果。

Following @peter-mortensen & manojlds's solution, I use git for-each-ref as subcommand to list only branches with name.

git grep "string/regexp" $(git for-each-ref --format='%(refname:short)' refs/heads)

This accomplish a better visualization, showing only named braches and making only one result for each branch.

春夜浅 2024-12-07 14:20:29

要显示分支名称以及搜索结果,您可以使用循环分别搜索每个分支,如下所示:

for branch in $(git branch | awk '{print $1}'); do
    echo "Branch: $branch"
    git grep "SEARCH_WORD" $(git rev-parse $branch)
done

此循环使用 gitbranch 列出所有分支,并使用 awk 仅提取分支名称。然后,它使用 git rev-parse 获取每个分支的提交哈希,并使用 git grep 在该分支中搜索字符串“deleteTemplateDocument”。输出将显示分支名称以及每个分支的匹配结果。

git log -S <search string> --source --all

https://stackoverflow.com/a/5816177/5368856

恢复提交,提交 ID 可能不在 HEAD

git revert commit_id

To display the branch name along with the search results, you can use a loop to search each branch separately, like this:

for branch in $(git branch | awk '{print $1}'); do
    echo "Branch: $branch"
    git grep "SEARCH_WORD" $(git rev-parse $branch)
done

This loop uses git branch to list all branches, and awk to extract just the branch names. Then, it uses git rev-parse to get the commit hash of each branch, and git grep to search for the string "deleteTemplateDocument" in that branch. The output will show the branch name and the matching results for each branch.

git log -S <search string> --source --all

https://stackoverflow.com/a/5816177/5368856

Revert a commit, commit id may not be at HEAD

git revert commit_id
独享拥抱 2024-12-07 14:20:29

对我来说它是:

git branch | xargs git grep -c "string_to_search"

git分支:在本地查找分支。

git grep -c :查找给定分支中的字符串。

<强>| xargs :一个管道和 xargs 将分支作为上一个命令的输入。

For me it is :

git branch | xargs git grep -c "string_to_search"

git branch : looks for branchs in local.

git grep -c : looks for the strings in a given branch.

| xargs : a pipe and xargs to give the branches as inputs to the previous command.

笑咖 2024-12-07 14:20:29

高度可定制的终端脚本:

git branch -r | grep "/master$" | xargs -I{} git --no-pager grep -E -n "(search string 1|search string 2...)" {} -- "*.js" "*.ts" "*.kt" "*.swift" "*.json"

此命令将帮助您在以“/master”结尾的分支名称中的各种编程语言的文件(可选)中搜索特定字符串(可以是正则表达式) “ Git 存储库中的(可选)

git brach -r:所有分支名称的列表

grep "/master$":(可选)使用正则表达式过滤分支名称,仅搜索以 /master 结尾的分支

git grep:搜索的主程序

  • --no-pager:(可选)一次显示所有结果,不使用分页程序(滚动显示更多)。

  • -E:此选项启用搜索字符串的扩展正则表达式语法。

  • -n:此选项显示行号以及匹配的行。

"(搜索字符串 1|搜索字符串 2...)":您可以在此处进行正则表达式

-- "*.js" "*.ts" "*.kt" "*. swift" "*.json":(可选)搜索特定文件类型,根据需要添加或删除

Highly customizable terminal script:

git branch -r | grep "/master
quot; | xargs -I{} git --no-pager grep -E -n "(search string 1|search string 2...)" {} -- "*.js" "*.ts" "*.kt" "*.swift" "*.json"

This command will help you search for specific strings (can be regex) in files of various programming languages (optional) within branches name ended with "/master" (optional) in a Git repository.

git brach -r: List of all branches name

grep "/master$": (optional) filter branch name using regex, only search branches that end with /master

git grep: Main program to search

  • --no-pager: (optional) Display all result at once, not using pager program (scroll to display more).

  • -E: This option enables extended regular expression syntax for the search strings.

  • -n: This option displays line numbers along with the matching lines.

"(search string 1|search string 2...)": you can you regex here

-- "*.js" "*.ts" "*.kt" "*.swift" "*.json": (optional) Search with specific file types, add or remove as you want

好倦 2024-12-07 14:20:29

要忽略大小写,请使用 -i:

git log -i --all --grep='word1 Word2'

To ignore case use -i:

git log -i --all --grep='word1 Word2'
自由如风 2024-12-07 14:20:29

这是一种多进程方法,通过为每个分支或提交生成一个 git grep 进程来显着提高搜索速度:

# ---------------------------------------------
# 1. Search all local branches
# ---------------------------------------------
# Search only these files and folders in all local branches
time git branch | awk '{print $NF}' \
    | xargs -P "$(nproc)" -I {} git --no-pager grep -n 'my regex search' {} \
    -- "path/to/my_file.c" "path/to/my_folder"

# ---------------------------------------------
# 2. Search all remote branches of all remotes
# ---------------------------------------------
# Search only these files and folders in all remote branches
time git branch -r | awk '{print $NF}' \
    | xargs -P "$(nproc)" -I {} git --no-pager grep -n 'my regex search' {} \
    -- "path/to/my_file.c" "path/to/my_folder"

# ---------------------------------------------
# 3. Search all local **and** remote branches
# ---------------------------------------------
# Search only these files and folders in all local and remote branches
time git branch -a | awk '{print $NF}' \
    | xargs -P "$(nproc)" -I {} git --no-pager grep -n 'my regex search' {} \
    -- "path/to/my_file.c" "path/to/my_folder"

# ---------------------------------------------
# Search **all commits** in the entire repository
# ---------------------------------------------
# Search only these files and folders in all commits (reachable from any branch 
# or tag) in the whole repository
time git rev-list --all \
    | xargs -P "$(nproc)" -I {} git --no-pager grep -n 'my regex search' {} \
    -- "path/to/my_file.c" "path/to/my_folder"

有关每个命令的完整说明以及更多信息,请参阅我的完整内容在这里回答:有关在 git 存储库中搜索(通过 grep 或类似方式)的所有内容

Here's a multi-process way that dramatically increases the speed of the search by spawning one git grep process per branch or commit:

# ---------------------------------------------
# 1. Search all local branches
# ---------------------------------------------
# Search only these files and folders in all local branches
time git branch | awk '{print $NF}' \
    | xargs -P "$(nproc)" -I {} git --no-pager grep -n 'my regex search' {} \
    -- "path/to/my_file.c" "path/to/my_folder"

# ---------------------------------------------
# 2. Search all remote branches of all remotes
# ---------------------------------------------
# Search only these files and folders in all remote branches
time git branch -r | awk '{print $NF}' \
    | xargs -P "$(nproc)" -I {} git --no-pager grep -n 'my regex search' {} \
    -- "path/to/my_file.c" "path/to/my_folder"

# ---------------------------------------------
# 3. Search all local **and** remote branches
# ---------------------------------------------
# Search only these files and folders in all local and remote branches
time git branch -a | awk '{print $NF}' \
    | xargs -P "$(nproc)" -I {} git --no-pager grep -n 'my regex search' {} \
    -- "path/to/my_file.c" "path/to/my_folder"

# ---------------------------------------------
# Search **all commits** in the entire repository
# ---------------------------------------------
# Search only these files and folders in all commits (reachable from any branch 
# or tag) in the whole repository
time git rev-list --all \
    | xargs -P "$(nproc)" -I {} git --no-pager grep -n 'my regex search' {} \
    -- "path/to/my_file.c" "path/to/my_folder"

For a full explanation of each command, and a ton more info., see my full answer here: All about searching (via grep or similar) in your git repositories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文