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
一样)。
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)可能值得在您的回答中指出,这比达斯汀的解决方案有一个优势,即您不需要确切知道文件名 - 您可以通过匹配路径任何部分的正则表达式进行搜索
refs/remotes
而不是 refs/heads
。要搜索所有内容(本地分支、远程跟踪分支和标签),只需使用 refs/
。
虽然 ididak's response 非常酷,并且 Handyman5 提供了一个脚本来使用它,但我发现使用这种方法有点受限。
有时您需要搜索可以随时间出现/消失的内容,那么为什么不搜索所有提交呢?除此之外,有时你需要一个详细的响应,而其他时候只提交匹配。以下是这些选项的两个版本。将这些脚本放在您的路径上:
git 查找文件
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 您可以修改该脚本以交替搜索所有提交、参考、参考/头、冗长等。
$ 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
并搜索提交“接触路径”和您感兴趣的路径名。
复制&粘贴此以使用 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 脚本作为 global git alias 直接添加到您的 ~/.gitconfig
。
此命令查找引入指定路径的提交:
git log --source --all --diff-filter=A --name-only -- '**/my_file.png'
不定期副业成功案例分享
somefile
的确切路径,则此方法有效:例如,如果您需要对路径/文件名进行正则表达式搜索,则可以使用 ididak 的答案。git log --all -- **/my_file.png
git log --grep='branch' --author='me' -- *.sql
。像魅力一样工作。 git 版本 1.7.11.1gitk
也支持通配。这是一个很好的答案@webmat!如果您想查看它在哪里被删除/创建/等,您可以使用git log --all --stat -- **/my_file.png
,这样您就不必猜测您是否正在从删除它的提交中检出它。