在 Git 中,我如何通过路径在多个分支中搜索文件或目录?
我在一个分支中写了一些东西,但我不记得是哪一个分支。现在我需要找到它。
澄清:我正在寻找我在其中一个分支上创建的文件。我想通过路径找到它,而不是通过其内容,因为我不记得其内容是什么。
我在一个分支中写了一些东西,但我不记得是哪一个分支。现在我需要找到它。
澄清:我正在寻找我在其中一个分支上创建的文件。我想通过路径找到它,而不是通过其内容,因为我不记得其内容是什么。
git log
+ git branch
可以帮你找到它:
% git log --all -- somefile
commit 55d2069a092e07c56a6b4d321509ba7620664c63
Author: Dustin Sallings <dustin@spy.net>
Date: Tue Dec 16 14:16:22 2008 -0800
added somefile
% git branch -a --contains 55d2069
otherbranch
还支持通配符匹配:
% git log --all -- '**/my_file.png'
单引号是必要的(至少在使用Bash shell时),这样shell就会原封不动地将glob模式传递给git,而不是扩展它(就像Unix的find
一样)。
somefile
的确切路径,那么这个方法就能起作用:如果你需要在路径/文件名中进行正则表达式搜索,那么你可以使用 ididak 的答案。 - ShreevatsaRgit log --all -- **/my_file.png
- webmatgit log --grep='branch' --author='me' -- *.sql
,效果非常好。git版本是1.7.11.1。 - PREEBgitk
也支持通配符。非常好的答案@webmat!如果您想查看文件的删除/创建等操作,可以使用git log --all --stat -- **/my_file.png
命令,这样您就不必猜测是否从已删除该文件的提交中检出它。 - eacousineau--all
=> "假装所有在refs/目录下的引用都被列在命令行中作为<commit>"。 - Snowcrash可以使用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
这样做的好处是您还可以使用正则表达式搜索文件名。
git for-each-ref --format="%(refname)" refs/heads
; do”。
(c)使用“git ls-tree --name-only”命令可以使输出更整洁。
(d)在回答中指出这种方法优于Dustin的解决方案的优点可能是值得的,即您不需要确切地知道文件名 - 您可以通过正则表达式匹配路径的任何部分进行搜索。 - Mark Longairrefs/remotes
而不是refs/heads
。要搜索所有内容(本地分支、远程跟踪分支和标签),只需使用refs/
即可。 - sleske尽管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-verbosefor 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/heads,详细信息等。
$ git find-file <regex>
$ git find-file --verbose <regex>
$ git find-file --verbose --decorated --color <regex>
可以查看https://github.com/albfan/git-find-file,了解一种可能的实现方式。
git find-file
,你可以将它添加为 git 别名,只需运行 git config --global alias.find-file '!git-find-file'
即可。 - lumbric复制并粘贴此内容以使用 git find-file SEARCHPATTERN
打印所有搜索的分支:
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 "$1"; done; :'
仅打印带有结果的分支:
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 "$1" > /dev/null; then echo "${branch}:"; git ls-tree -r --name-only $branch | nl -bn -w3 | grep "$1"; fi; done; :'
这些命令将直接将一些最小化的shell脚本添加到您的~/.gitconfig
中,作为全局的git别名。global git alias。
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done git fetch --all git pull --all
- Rob Towgit log --source --all --diff-filter=A --name-only -- '**/my_file.png'