在Git中,如何在多个分支中按路径搜索文件或目录?
我在一个分支中写了一些东西,但我不记得是哪一个了。现在我需要找到它。
澄清:我正在寻找我在我的一个分支上创建的文件。我想通过路径找到它,而不是通过它的内容,因为我不记得内容是什么。
在Git中,如何在多个分支中按路径搜索文件或目录?
我在一个分支中写了一些东西,但我不记得是哪一个了。现在我需要找到它。
澄清:我正在寻找我在我的一个分支上创建的文件。我想通过路径找到它,而不是通过它的内容,因为我不记得内容是什么。
当前回答
可以在这里找到Git存储库的find命令的一个相当不错的实现:
https://github.com/mirabilos/git-find
其他回答
这个命令查找引入指定路径的提交:
git log --source --all --diff-filter=A --name-only -- '**/my_file.png'
可以在这里找到Git存储库的find命令的一个相当不错的实现:
https://github.com/mirabilos/git-find
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
这样做的好处是,您还可以使用正则表达式搜索文件名。
尽管ididak的响应非常酷,并且Handyman5提供了一个使用它的脚本,但我发现使用这种方法有点受限。
有时需要搜索可能随着时间出现/消失的内容,那么为什么不搜索所有提交呢?除此之外,有时需要详细的响应,而其他时候只提交匹配。以下是这些选项的两个版本。把这些脚本放在你的路径上:
git-find-file
for branch in $(git rev-list --all)
do
if (git ls-tree -r --name-only $branch | grep --quiet "$1")
then
echo $branch
fi
done
git-find-file-verbose
for branch in $(git rev-list --all)
do
git ls-tree -r --name-only $branch | grep "$1" | sed 's/^/'$branch': /'
done
现在你可以
$ 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
使用getopt,您可以修改该脚本,以交替搜索所有提交、refs、refs/heads、been verbose等。
$ git find-file <regex>
$ git find-file --verbose <regex>
$ git find-file --verbose --decorated --color <regex>
签出https://github.com/albfan/git-find-file以获得可能的实现。
您可以使用gitk——all并搜索提交的“触摸路径”和您感兴趣的路径名。