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

发布于 2024-07-10 14:55:14 字数 169 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

十级心震 2024-07-17 14:55:14

git log + gitbranch 将为您找到它:

% git log --all -- somefile

commit 55d2069a092e07c56a6b4d321509ba7620664c63
Author: Dustin Sallings <[email protected]>
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 传递通配符模式到 git 不变,而不是扩展它(就像 Unix find 一样)。

git log + git branch will find it for you:

% git log --all -- somefile

commit 55d2069a092e07c56a6b4d321509ba7620664c63
Author: Dustin Sallings <[email protected]>
Date:   Tue Dec 16 14:16:22 2008 -0800

    added somefile


% git branch -a --contains 55d2069
  otherbranch

Supports globbing, too:

% 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).

美胚控场 2024-07-17 14:55:14

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 ls-tree might help. 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.

流年里的时光 2024-07-17 14:55:14

虽然ididak的响应非常酷,并且Handyman5提供了使用它的脚本,但我发现了它使用这种方法有点受限。

有时您需要搜索随着时间的推移可能出现/消失的内容,那么为什么不搜索所有提交呢? 除此之外,有时您需要详细的响应,而其他时候只需提交匹配项。 以下是这些选项的两个版本。 将这些脚本放在您的路径上:

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

现在您可以

$ 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 了解可能的实现。

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

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.

$ 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.

还给你自由 2024-07-17 14:55:14

命令行

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

<

a href="https://i.sstatic.net/Ms6OA。 png" rel="nofollow noreferrer">ui fields

(归功于:@MikeW 的建议。)

Command Line

You could use gitk --all and search for commits "touching paths" and the pathname you are interested in.

UI

ui fields

(Credit to: @MikeW's suggestion.)

ら栖息 2024-07-17 14:55:14

复制& 粘贴此命令以使用 git find-file SEARHPATTERN

打印所有搜索到的分支:

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 别名

Copy & paste this to use 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.

孤独患者 2024-07-17 14:55:14

此命令查找引入指定路径的提交:

git log --source --all --diff-filter=A --name-only -- '**/my_file.png'

This command finds commits which introduced specified paths:

git log --source --all --diff-filter=A --name-only -- '**/my_file.png'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文