git 不同分支的 ls 文件

git ls-files for different branch

我想查找指定分支基目录下的所有markdown文件。 git ls-files 有人向我建议,但文档没有提到指定分支。我希望能够在不签出该分支的情况下执行此操作。这能做到吗?

如果您正在使用 git ls-tree,您可能需要自己过滤输出,以便 grep 您需要的内容。

如前所述 in this thread 早在 2012 年:

There are two uses of "pattern" there, and the former is probably OK but the latter is indeed misleading.

Back when we wrote this command, there were two distinct "pathspec" families, and ls-tree belong to the one with "diff" family that did not take anything but "leading path pattern" (the other one from "ls-files" and "grep" family allowed wildcards). Giving a wildcard to the command in the "ls-tree/diff" family was never supported.

If you change ls-tree to use its paths arguments as pathspec, you will break backward compatibility.

git ls-files 检查 indexwork-tree(或两者)中的文件。由于当前索引和工作树内容通常反映从当前分支的尖端提取的当前提交,因此它不会以这种方式工作。但有一个解决方法。

一样,git ls-tree 检查 tree 对象,例如存储在提交中的对象。但是,git ls-tree 不接受 pathspec 参数,例如 **/*.md.

解决方法是将感兴趣的提交读入临时索引。要干净地做到这一点,请使用 mktemp 创建一个临时文件,然后删除临时文件并使用 git read-tree 重新创建该文件作为有效的临时索引,其中包含您希望检查的提交的图像,然后您可以使用 git ls-files 检查它。例如:

$ cd git
$ sh -c 'export GIT_INDEX_FILE=$(mktemp); rm $GIT_INDEX_FILE; git read-tree e83c5163316f89bfbde7d9ab23ca2e25604af290; git ls-files -- "*.h" "**/*.h"; rm $GIT_INDEX_FILE'
cache.h
$ sh -c 'export GIT_INDEX_FILE=$(mktemp); rm $GIT_INDEX_FILE; git read-tree origin/master; git ls-files -- "*.md"; rm $GIT_INDEX_FILE'
.github/CONTRIBUTING.md
.github/PULL_REQUEST_TEMPLATE.md
README.md
contrib/vscode/README.md