如何在Git分支中搜索文件或目录?

本文介绍了如何在Git的多个分支中通过路径搜索文件或目录,提供了多种方法,包括使用`git ls-tree`、`git log`以及通过shell脚本实现,同时也提到了可以使用正则表达式来搜索文件名。
摘要由CSDN通过智能技术生成

本文翻译自:How can I search Git branches for a file or directory?

In Git, how could I search for a file or directory by path across a number of branches? 在Git中,我如何通过多个分支的路径搜索文件或目录?

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. 我想通过路径找到它,而不是通过它的内容找到它,因为我不记得内容是什么。


#1楼

参考:https://stackoom.com/question/1YuA/如何在Git分支中搜索文件或目录


#2楼

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. 虽然ididak的响应非常酷,而且Handyman5提供了一个使用它的脚本,但我发现使用这种方法有点受限。

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 混帐查找文件

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

Now you can do 现在你可以做到

$ 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

See that using getopt you can modify that script to alternate searching all commits, refs, refs/heads, been verbose, etc. 看到使用getopt,您可以修改该脚本以交替搜索所有提交,引用,引用/头,冗长等。

$ git find-file <regex>
$ git find-file --verbose <regex>
$ git find-file --verbose --decorated --color <regex>

Checkout https://github.com/albfan/git-find-file for a possible implementation. 结帐https://github.com/albfan/git-find-file以获得可能的实施方案。


#3楼

Copy & paste this to use git find-file SEARCHPATTERN 复制并粘贴此选项以使用git find-file SEARCHPATTERN

Printing all searched branches: 打印所有搜索的分支:

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; :'

Print only branches with results: 仅打印带有结果的分支:

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; :'

These commands will add some minimal shell scripts directly to your ~/.gitconfig as global git alias . 这些命令会将一些最小的shell脚本直接添加到~/.gitconfig作为全局git别名


#4楼

git ls-tree might help. git ls-tree可能有所帮助。 To search across all existing branches: 要搜索所有现有分支:

for branch in `git for-each-ref --format="%(refname)" refs/heads`; do
  echo $branch :; git ls-tree -r --name-only $branch | grep '<foo>'
done

The advantage of this is that you can also search with regular expressions for the file name. 这样做的好处是您还可以使用正则表达式搜索文件名。


#5楼

你可以使用gitk --all并搜索提交“触摸路径”和你感兴趣的路径名。


#6楼

git log will find it for you: 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

Supports globbing, too: 也支持globbing:

% git log --all -- '**/my_file.png'

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 ). 单引号是必要的(至少如果使用bash shell),所以shell将glob模式传递给git不变,而不是扩展它(就像使用Unix find )。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值