【问题标题】:git ls-files: showing blob files onlygit ls-files:仅显示 blob 文件
【发布时间】:2021-06-19 14:43:36
【问题描述】:

如何使用git ls-files 仅显示文件(即不包括目录)? 例如,我想从以下输出中删除 asd1 和 asd2:

$> git ls-files                         
asd1/bar
asd1/foo
asd2/bar
base.html
index.html
list.html

有什么方法可以在不涉及字符串操作的情况下做到这一点?

【问题讨论】:

  • 请注意,来自git ls-files 的上述输出仅包含文件。我认为您真的想询问git ls-tree,如您自己的回答所示。

标签: git git-ls-files


【解决方案1】:

这就是管道的用途:

 git ls-files | while read filename; do basename $filename; done

我也可以走这条路:

git ls-files | sed 's/.*\///'

【讨论】:

  • 这部分工作:它仍然打印asd1/asd2中的文件。即:bar foo bar base.html index.html list.html
【解决方案2】:

好吧,我终于让它工作了......我留下答案希望能节省一些其他时间:

git ls-tree --name-only -r HEAD | grep -v -w -e "$(git ls-tree -d --name-only HEAD)"

【讨论】:

  • 这使用git ls-tree,而不是git ls-files。他们做不同的事情! ls-tree 检查树对象; ls-files 比较复杂,但一般都是从索引开始。 Git 的索引具有“扁平树”的形式,所有目录都已从中删除。
猜你喜欢
  • 2020-06-24
  • 2020-01-03
  • 2020-08-06
  • 2019-08-08
  • 2016-08-13
  • 1970-01-01
  • 2023-03-27
  • 2020-09-06
  • 1970-01-01
相关资源
最近更新 更多