首页·终端命令

git ls-files

git

用法

git ls-files [-o|-m|-i] [--exclude-standard]

列出索引里的路径而不是磁盘上的文件,所以脚本遍历已跟踪文件时靠它最稳;-i 不能单独用,必须配 -c 或 -o 再加 --exclude-standard。

常用选项

选项含义
-cfiles in the index; the default
-mtracked files modified on disk
-dtracked files that are missing
-ofiles git does not track
-iignored files; must come with -o or -c and --exclude-standard
--exclude-standardapply .gitignore and friends
-sshow mode and object id of each entry
-zseparate paths with NUL for xargs -0

示例

git ls-files

every tracked path, straight from the index

git ls-files -o --exclude-standard

untracked files only, respecting .gitignore

git ls-files -i -c --exclude-standard

tracked files that .gitignore says should be ignored

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git ls-files 是做什么的?

列出索引里的路径而不是磁盘上的文件,所以脚本遍历已跟踪文件时靠它最稳;-i 不能单独用,必须配 -c 或 -o 再加 --exclude-standard。

Q. 怎么写?

git ls-files [-o|-m|-i] [--exclude-standard] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 8 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-ls-files