如何仅针对一个目录级别进行`git ls-files`。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10452962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:52:41  来源:igfitidea点击:

how to `git ls-files` for just one directory level.

gitlsglob

提问by Philip Oakley

I'm using msysgit (1.7.9), and I'm looking for the right invocation of the git ls-filescommand to show just the (tracked) files and directories at the current level, either from the index, or the current working directory if that's easier.

我正在使用 msysgit (1.7.9),并且我正在寻找正确的git ls-files命令调用以仅显示当前级别的(跟踪的)文件和目录,无论是从索引还是当前工作目录(如果是)更轻松。

Essentially it would give a directory listing similar that that you would see on Github. Coming from Windows, I'm not too familiar with the right way of doing the globbing(?).

本质上,它会提供一个类似于您在 Github 上看到的目录列表。来自 Windows,我不太熟悉做 globbing(?) 的正确方法。

采纳答案by jthill

I think you want git ls-tree HEADsed'd to taste. The second word of ls-tree's output will be treefor directories, blobfor files, commitfor submodulesm, the filename is everything after the ascii tab

我想你想git ls-tree HEAD尝尝。ls-tree 输出的第二个字将tree用于目录、blob文件、commit子模块,文件名是 ascii 选项卡之后的所有内容

回答by David Cain

I believe git ls-tree --name-only [branch]will do what you're looking for.

我相信git ls-tree --name-only [branch]会做你想要的。

回答by antak

git ls-tree <tree-ish>is good and all, but I can't figure out how to specify the indexas the <tree-ish>. (Although I'm sure there's bound to be some all-caps reference to do just that.)

git ls-tree <tree-ish>很好,但我不知道如何将索引指定为<tree-ish>. (虽然我确信肯定会有一些全大写的参考来做到这一点。)

Anyhow, ls-filesimplicitly works on the index so I might as well use that:

无论如何,ls-files隐式适用于索引,所以我不妨使用它:

$ git ls-files | cut -d/ -f1 | uniq

This shows files and directories only in the current directory.

这仅显示当前目录中的文件和目录。

Change cut's -fargument to control depth. For instance, -f-2(that's dashtwo) shows files and directories up to two levels deep:

改变控制深度cut-f参数。例如,-f-2(即破折号二)显示最多两层深的文件和目录:

$ git ls-files | cut -d/ -f-2 | uniq

IF you specify the <path>argument to ls-files, make sure to increase -fto accommodate the leading directories:

如果您将<path>参数指定为ls-files,请确保增加-f以容纳前导目录:

$ git ls-files foo/bar | cut -d/ -f-3 | uniq

回答by AlexJWR

To just list the files in the current working directory that are tracked by git, I found that the following is several times faster than using git ls-tree...:

只列出当前工作目录中由 git 跟踪的文件,我发现以下比使用快几倍git ls-tree...

ls | grep -f <(git ls-files)

It would take a little messing around with sed if you also wanted to include directories, something along the lines of:

如果您还想包含目录,则需要对 sed 进行一些处理,例如:

ls | grep -f <(git ls-files | sed 's/\/.*//g' | sort | uniq)  

assuming you don't have any '/' characters in the names of your files. As well as...

假设您的文件名中没有任何“/”字符。也...

ls -a | grep -f <(git ls-files | sed 's/\/.*//g' | sort | uniq)

ls -a | grep -f <(git ls-files | sed 's/\/.*//g' | sort | uniq)

in order to also list "invisible" (yet-tracked) files.

为了还列出“不可见”(尚未跟踪)的文件。

回答by Mike Maxwell

I'm surprised this is so hard... but don't get me started on my griping about git.

我很惊讶这太难了……但不要让我开始抱怨 git。

A variant on jthill's answer seems to be aliasable (hey, I'm a linguist, I have a license to make new words). The variant is

jthill 答案的一个变体似乎是可别名的(嘿,我是一名语言学家,我有创建新单词的许可证)。变体是

ls -d `git ls-tree HEAD | sed -e "s/^.*\t//"`

This uses 'ls' to format the output, so you get color coding (if you use that), etc. It also works as an alias:

这使用 'ls' 来格式化输出,因此您可以获得颜色编码(如果您使用它)等。它也可以用作别名:

alias gitls='ls -d `git ls-tree HEAD | sed -e "s/^.*\t//"`'

FWIW, you can also alias the recursive command, so that you used the 'ls' formatting (e.g. if your path+filenames aren't too long, you'll get two column output, color coding of executables, etc.)

FWIW,您还可以为递归命令取别名,以便使用“ls”格式(例如,如果您的路径+文件名不太长,您将获得两列输出、可执行文件的颜色编码等)

alias gitls-r='ls `git ls-files`'

回答by Ryan

The simplest solution I have found, believe it or not, is to simply cdinto the directory you want in your terminal. Running git ls-filesin /project/srcwill give you results from only that directory, versus running it in /project

我发现的最简单的解决方案,不管cd你信不信,就是简单地进入你想要的终端目录。运行git ls-files/project/src会给你只从该目录的结果,对在运行它/project

Not the most technical answer, but hey, it works!

不是最技术性的答案,但是嘿,它有效!