如何在 Git 分支中搜索文件或目录?
在 Git 中,如何通过多个分支的路径搜索文件或目录?
我在一个分支里写过一些东西,但我不记得是哪一篇了。 现在我需要找到它。
澄清:我正在寻找在我的一个分支上创建的文件。 我想通过路径找到它,而不是通过其内容找到它,因为我不记得内容是什么。
In Git, how could I search for a file or directory by path across a number of branches?
I've written something in a branch, but I don't remember which one. Now I need to find it.
Clarification: I'm looking for a file which I created on one of my branches. I'd like to find it by path, and not by its contents, as I don't remember what the contents are.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
git log
+gitbranch
将为您找到它:也支持通配符:
单引号是必需的(至少如果使用 Bash shell),因此 shell 传递通配符模式到 git 不变,而不是扩展它(就像 Unix
find
一样)。git log
+git branch
will find it for you:Supports globbing, too:
The single quotes are necessary (at least if using the Bash shell) so the shell passes the glob pattern to git unchanged, instead of expanding it (just like with Unix
find
).git ls-tree 可能会有所帮助。 搜索所有现有分支:
这样做的好处是您还可以使用正则表达式搜索文件名。
git ls-tree might help. To search across all existing branches:
The advantage of this is that you can also search with regular expressions for the file name.
虽然ididak的响应非常酷,并且Handyman5提供了使用它的脚本,但我发现了它使用这种方法有点受限。
有时您需要搜索随着时间的推移可能出现/消失的内容,那么为什么不搜索所有提交呢? 除此之外,有时您需要详细的响应,而其他时候只需提交匹配项。 以下是这些选项的两个版本。 将这些脚本放在您的路径上:
git-find-file
git-find-file-verbose
现在您可以
使用 getopt 您可以修改该脚本以交替搜索所有提交、引用、引用/头、详细信息等。
签出 https://github.com/albfan/git-find-file 了解可能的实现。
Although ididak's response is pretty cool, and Handyman5 provides a script to use it, I found it a little restricted to use that approach.
Sometimes you need to search for something that can appear/disappear over time, so why not search against all commits? Besides that, sometimes you need a verbose response, and other times only commit matches. Here are two versions of those options. Put these scripts on your path:
git-find-file
git-find-file-verbose
Now you can do
See that using getopt you can modify that script to alternate searching all commits, refs, refs/heads, been verbose, etc.
Checkout https://github.com/albfan/git-find-file for a possible implementation.
命令行
您可以使用 gitk --all 并搜索提交“接触路径”和您感兴趣的路径名。UI
<
a href="https://i.sstatic.net/Ms6OA。 png" rel="nofollow noreferrer">
(归功于:@MikeW 的建议。)
Command Line
You could use
gitk --all
and search for commits "touching paths" and the pathname you are interested in.UI
(Credit to: @MikeW's suggestion.)
复制& 粘贴此命令以使用
git find-file SEARHPATTERN
打印所有搜索到的分支:
仅打印包含结果的分支:
这些命令将直接将一些最小的 shell 脚本添加到您的
~/.gitconfig
中全局 git 别名。Copy & paste this to use
git find-file SEARCHPATTERN
Printing all searched branches:
Print only branches with results:
These commands will add some minimal shell scripts directly to your
~/.gitconfig
as global git alias.此命令查找引入指定路径的提交:
This command finds commits which introduced specified paths: