在Git中,如何在多个分支中按路径搜索文件或目录?
我在一个分支中写了一些东西,但我不记得是哪一个了。现在我需要找到它。
澄清:我正在寻找我在我的一个分支上创建的文件。我想通过路径找到它,而不是通过它的内容,因为我不记得内容是什么。
在Git中,如何在多个分支中按路径搜索文件或目录?
我在一个分支中写了一些东西,但我不记得是哪一个了。现在我需要找到它。
澄清:我正在寻找我在我的一个分支上创建的文件。我想通过路径找到它,而不是通过它的内容,因为我不记得内容是什么。
当前回答
可以在这里找到Git存储库的find命令的一个相当不错的实现:
https://github.com/mirabilos/git-find
其他回答
Git log + Git分支会为你找到它:
% 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
也支持globbing:
% git log --all -- '**/my_file.png'
单引号是必要的(至少在使用Bash shell时),因此shell将glob模式原形不变地传递给git,而不是展开它(就像Unix find一样)。
您可以使用gitk——all并搜索提交的“触摸路径”和您感兴趣的路径名。
复制粘贴这使用git查找文件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别名。
可以在这里找到Git存储库的find命令的一个相当不错的实现:
https://github.com/mirabilos/git-find
这个命令查找引入指定路径的提交:
git log --source --all --diff-filter=A --name-only -- '**/my_file.png'