Using command line git, how can I make git show a list of the files that are being tracked in the repository?
-
5possible duplicate of List files in local git repo?– KristjánCommented Sep 28, 2015 at 15:54
-
3I believe it is. But it does not feel right to mark this as dup when this has a better answer.– lindheCommented Mar 30, 2016 at 20:25
-
9in a parallel universe, it's uncanny how similar this question is including all the answers, yet each with its own distinctive flair.– Jeff PuckettCommented Apr 10, 2017 at 2:45
-
2@lindhe: it's almost like the StackExchange obsession with closing duplicates is not 100% sensible– iconoclastCommented Feb 25, 2023 at 20:48
7 Answers
To list all the files currently being tracked under the branch master
, use ls-tree
:
git ls-tree -r master --name-only
To list all files that have ever existed (i.e. including deleted files):
git log --pretty=format: --name-only --diff-filter=A | sort - | sed '/^$/d'
-
112Use git ls-tree -r HEAD --name-only if you want to list files of the current branch– RamastCommented Mar 18, 2015 at 5:32
-
6
-
18@NicolasLykkeIversen -
git
does not version directories directly. Instead it stores files and their paths.ls-tree
will output all the versioned files. To better understand this distinction, try staging an empty directory togit
. The empty directory will never show up in the staged changes. The only way to version such an empty directory is to actually version a file under the directory. For use cases where you need such an empty placeholder directory in version control, you can create a dummy file under the directory, and version that. I hope the explanation is clear.– TuxdudeCommented Mar 31, 2016 at 18:22 -
4Just note,
ls-tree master
doesn't show the tracked files in staging area.– nn0pCommented Apr 18, 2016 at 8:05 -
1The command to list all files that ever existed does actually only list files until the point HEAD is currently at and it also ignores cases like renames. So I would instead suggest
git log --pretty=format: --name-only --all | sort -u | sed '/^$/d'
– micCommented Nov 27, 2017 at 13:56
The files managed by git are shown by git ls-files
. Check out its manual page.
-
7That seems to only show files at or below the current directory.– Lyle ZCommented Apr 21, 2015 at 21:52
-
12Mind if I edit this to include the relevant sections of the manual page? Commented Aug 24, 2015 at 17:51
-
2@NathanBasanese perhaps you can add another answer with that information. Commented Jun 29, 2016 at 9:34
-
@LyleZ Perhaps this is intended to be in keeping with
ls
...orls -R
– flow2kCommented Apr 19, 2017 at 0:14 -
1@LyleZ Also, this behavior is the same with
git ls-tree
- it is relative to thepwd
.– flow2kCommented Apr 19, 2017 at 0:19
The accepted answer only shows files in the current directory's tree. 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.-r
recurses into subdirectories. Combined with--full-tree
, this gives you all committed, tracked files.--name-only
removes SHA / permission info for when you just want the file paths.HEAD
specifies which branch you want the list of tracked, committed files for. You could change this tomaster
or any other branch name, butHEAD
is the commit you have checked out right now.
This is the method from the accepted answer to the ~duplicate question https://stackoverflow.com/a/8533413/4880003.
-
Nathan, you said "Combined with
--full-tree
, this gives you all committed, tracked files". Does that mean all files that have ever been committed since the beginning of the repo, or is there some cutoff? The currently accept answer includes agit log
command whose output is a couple thousand lines longer than your command. I'm trying to hunt down large files that were committed almost a decade ago so I can prune them from history. Commented Feb 2, 2022 at 0:58
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)"
-
The
glr
alias you provided looked a bit weird so I made a version more consistent with the format of the first alias you provided:alias glr='ls --color=auto -d $(git ls-tree -rt $(git branch | grep \* | cut -d " " -f2) --name-only)'
. Tested withgit version 2.20.1
on Debian 10. Commented May 28, 2020 at 21:56
Building on the existing answers, you can use tree
to view it a little prettier:
git ls-tree --full-tree --name-only -r HEAD | tree --fromfile .
You probably want to paginate this:
git ls-tree --full-tree --name-only -r HEAD | tree -C --fromfile . | ${PAGER:-less}
This certainly deserves a place as tree
alias in the git config :)
list files tracked by git, sort by author date
git ls-tree -r --name-only main . | while read -r path; do date=$(git log -n1 --format=format:%aI -- "$path"); echo "$date $path"; done | sort -r | cut -c27-
Windows Command Prompt
To find all directories being tracked by Git on a Windows system, you can use the following command in the Command Prompt:
dir /s /b /ad "C:\*" | findstr /i "\\\.git$"
This command retrieves the URLs of all remotes configured in Git repositories and extracts the repository names from the URLs.
-
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.– Community BotCommented Apr 18 at 21:30