在Git中,我如何通过多个分支的路径搜索文件或目录?
我在一个分支中写了一些东西,但我不记得是哪一个.现在我需要找到它.
澄清:我正在寻找一个我在其中一个分支上创建的文件.我想通过路径找到它,而不是通过它的内容找到它,因为我不记得内容是什么.
Dus*_*tin 409
git log会为你找到它:
% 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
Run Code Online (Sandbox Code Playgroud)
也支持globbing:
% git log --all -- '**/my_file.png'
Run Code Online (Sandbox Code Playgroud)
单引号是必要的(至少如果使用bash shell),所以shell将glob模式传递给git不变,而不是扩展它(就像使用Unix一样git log).
idi*_*dak 63
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
Run Code Online (Sandbox Code Playgroud)
这样做的好处是您还可以使用正则表达式搜索文件名.
alb*_*fan 17
虽然ididak的响应非常酷,而且Handyman5提供了一个使用它的脚本,但我发现使用这种方法有点受限.
有时您需要搜索可能随着时间的推移出现/消失的内容,那么为什么不搜索所有提交?除此之外,有时您需要详细的响应,有时只需提交匹配.以下是这些选项的两个版本.将这些脚本放在您的路径上:
混帐查找文件
for branch in $(git rev-list --all)
do
if (git ls-tree -r --name-only $branch | grep --quiet "$1")
then
echo $branch
fi
done
Run Code Online (Sandbox Code Playgroud)
混帐找到的文件,详细
for branch in $(git rev-list --all)
do
git ls-tree -r --name-only $branch | grep "$1" | sed 's/^/'$branch': /'
done
Run Code Online (Sandbox Code Playgroud)
现在你可以做到
$ 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
Run Code Online (Sandbox Code Playgroud)
看到使用getopt,您可以修改该脚本以交替搜索所有提交,引用,引用/头,冗长等.
$ git find-file <regex>
$ git find-file --verbose <regex>
$ git find-file --verbose --decorated --color <regex>
Run Code Online (Sandbox Code Playgroud)
结帐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; :'
Run Code Online (Sandbox Code Playgroud)
仅打印带有结果的分支:
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; :'
Run Code Online (Sandbox Code Playgroud)
这些命令将直接添加一些最起码的shell脚本到你~/.gitconfig作为全球git的别名.
| 归档时间: |
|
| 查看次数: |
111809 次 |
| 最近记录: |