本文翻译自:How can I make git show a list of the files that are being tracked?
使用命令行git,如何让git显示存储库中正在跟踪的文件列表?
#1楼
参考:https://stackoom.com/question/13U55/如何让git显示正在跟踪的文件列表
#2楼
If you want to list all the files currently being tracked under the branch master
, you could use this command: 如果要列出分支master
下当前正在跟踪的所有文件,可以使用以下命令:
git ls-tree -r master --name-only
If you want a list of files that ever existed (ie including deleted files): 如果您想要一个曾经存在的文件列表(即包括已删除的文件):
git log --pretty=format: --name-only --diff-filter=A | sort - | sed '/^$/d'
#3楼
The files managed by git are shown by git ls-files
. git管理的文件由git ls-files
。 Check out its manual page. 查看其手册页。
#4楼
As noted by, eg, Lyle Z, the current answers only show files in the current directory's tree. 如Lyle Z所述,当前答案仅显示当前目录树中的文件。 To show all of the tracked files that have been committed (on the current branch), use 要显示已提交的所有跟踪文件(在当前分支上),请使用
git ls-tree --full-tree --name-only -r HEAD
-
--full-tree
makes the command run as if you were in the repo's root directory.--full-tree
使命令运行,就像您在repo的根目录中一样。 -
-r
recurses into subdirectories.-r
递归到子目录。 Combined with--full-tree
, this gives you all committed, tracked files. 与--full-tree
结合使用,可以为您提供所有已提交的跟踪文件。 -
--name-only
removes SHA / permission info for when you just want the file paths.--name-only
删除只需要文件路径的SHA /权限信息。 -
HEAD
specifies which branch you want the list of tracked, committed files for.HEAD
指定您想要跟踪的已提交文件列表的分支。 You could change this tomaster
or any other branch name, butHEAD
is the commit you have checked out right now. 您可以将其更改为master
或任何其他分支名称,但HEAD
是您现在已签出的提交。
This is the accepted answer to the ~duplicate question https://stackoverflow.com/a/8533413/4880003 . 这是〜重复问题https://stackoverflow.com/a/8533413/4880003的公认答案。
#5楼
You might want colored output with this. 你可能想要彩色输出。
I use this one-liner for listing the tracked files and directories in the current directory of the current branch: 我使用这个单行代码列出当前分支的当前目录中的跟踪文件和目录:
ls --group-directories-first --color=auto -d $(git ls-tree $(git branch | grep \* | cut -d " " -f2) --name-only)
You might want to add it as an alias: 您可能希望将其添加为别名:
alias gl='ls --group-directories-first --color=auto -d $(git ls-tree $(git branch | grep \* | cut -d " " -f2) --name-only)'
If you want to recursively list files: 如果要递归列出文件:
'ls' --color=auto -d $(git ls-tree -rt $(git branch | grep \* | cut -d " " -f2) --name-only)
And an alias: 和别名:
alias glr="'ls' --color=auto -d \$(git ls-tree -rt \$(git branch | grep \\* | cut -d \" \" -f2) --name-only)"