如何在 Git 分支中搜索文件或目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/372506/
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
How can I search Git branches for a file or directory?
提问by Peeja
In Git, how could I search for a file or directory by path across a number of branches?
在 Git 中,如何通过跨多个分支的路径搜索文件或目录?
I've written something in a branch, but I don't remember which one. Now I need to find it.
我在一个分支上写过一些东西,但我不记得是哪一个了。现在我需要找到它。
Clarification: I'm looking for a file which I created on one of my branches. I'd like to find it by path, and not by its contents, as I don't remember what the contents are.
澄清:我正在寻找我在我的一个分支上创建的文件。我想通过路径而不是内容找到它,因为我不记得内容是什么。
回答by Dustin
git log
+ git branch
will find it for you:
git log
+git branch
会为您找到它:
% git log --all -- somefile
commit 55d2069a092e07c56a6b4d321509ba7620664c63
Author: Dustin Sallings <[email protected]>
Date: Tue Dec 16 14:16:22 2008 -0800
added somefile
% git branch -a --contains 55d2069
otherbranch
Supports globbing, too:
也支持通配符:
% git log --all -- '**/my_file.png'
The single quotes are necessary (at least if using the Bash shell) so the shell passes the glob pattern to git unchanged, instead of expanding it (just like with Unix find
).
单引号是必需的(至少在使用 Bash shell 时),因此 shell 将 glob 模式传递给 git 不变,而不是扩展它(就像使用 Unix 一样find
)。
回答by ididak
git ls-tree might help. To search across all existing branches:
git ls-tree 可能会有所帮助。要搜索所有现有分支:
for branch in `git for-each-ref --format="%(refname)" refs/heads`; do
echo $branch :; git ls-tree -r --name-only $branch | grep '<foo>'
done
The advantage of this is that you can also search with regular expressions for the file name.
这样做的好处是您还可以使用正则表达式搜索文件名。
回答by albfan
Although ididak's responseis pretty cool, and Handyman5provides a script to use it, I found it a little restricted to use that approach.
虽然ididak 的回应非常酷,而且Handyman5提供了一个使用它的脚本,但我发现使用这种方法有点受限。
Sometimes you need to search for something that can appear/disappear over time, so why not search against all commits? Besides that, sometimes you need a verbose response, and other times only commit matches. Here are two versions of those options. Put these scripts on your path:
有时您需要搜索随着时间的推移可能出现/消失的东西,那么为什么不搜索所有提交呢?除此之外,有时您需要详细的响应,而其他时候只提交匹配项。以下是这些选项的两个版本。将这些脚本放在您的路径上:
git-find-file
git-find-file
for branch in $(git rev-list --all)
do
if (git ls-tree -r --name-only $branch | grep --quiet "")
then
echo $branch
fi
done
git-find-file-verbose
git-find-file-verbose
for branch in $(git rev-list --all)
do
git ls-tree -r --name-only $branch | grep "" | sed 's/^/'$branch': /'
done
Now you can do
现在你可以做
$ git find-file <regex>
sha1
sha2
$ git find-file-verbose <regex>
sha1: path/to/<regex>/searched
sha1: path/to/another/<regex>/in/same/sha
sha2: path/to/other/<regex>/in/other/sha
See that using getoptyou can modify that script to alternate searching all commits, refs, refs/heads, been verbose, etc.
看到使用getopt您可以修改该脚本以交替搜索所有提交、引用、引用/头、冗长等。
$ git find-file <regex>
$ git find-file --verbose <regex>
$ git find-file --verbose --decorated --color <regex>
Checkout https://github.com/albfan/git-find-filefor a possible implementation.
查看https://github.com/albfan/git-find-file以获得可能的实现。
回答by Greg Hewgill
You could use gitk --all
and search for commits "touching paths" and the pathname you are interested in.
您可以使用gitk --all
和搜索提交“触摸路径”和您感兴趣的路径名。
回答by lumbric
Copy & paste this to use git find-file SEARCHPATTERN
复制并粘贴此以使用 git find-file SEARCHPATTERN
Printing all searched branches:
打印所有搜索到的分支:
git config --global alias.find-file '!for branch in `git for-each-ref --format="%(refname)" refs/heads`; do echo "${branch}:"; git ls-tree -r --name-only $branch | nl -bn -w3 | grep ""; done; :'
Print only branches with results:
只打印带有结果的分支:
git config --global alias.find-file '!for branch in $(git for-each-ref --format="%(refname)" refs/heads); do if git ls-tree -r --name-only $branch | grep "" > /dev/null; then echo "${branch}:"; git ls-tree -r --name-only $branch | nl -bn -w3 | grep ""; fi; done; :'
These commands will add some minimal shell scripts directly to your ~/.gitconfig
as global git alias.
这些命令会将一些最小的 shell 脚本直接添加到您的~/.gitconfig
as global git alias 中。
回答by Dominik George
A quite decent implementation of the find
command for Git repositories can be found here:
find
可以在此处找到 Git 存储库命令的相当不错的实现: