如何让git显示正在跟踪的文件列表?

本文介绍如何使用Git命令行工具列出存储库中正在跟踪的文件列表,包括只显示当前分支下的文件、显示所有曾经存在的文件(包括已删除的文件),以及显示已提交的所有跟踪文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自: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 to master or any other branch name, but HEAD 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)"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值