Git - List all files currently under source control?
Solution 1:
If you want to list all files for a specific branch, e.g. master
:
git ls-tree -r master --name-only
The -r
option will let it recurse into subdirectories and print each file currently under version control. You can also specify HEAD
instead of master
to get the list for any other branch you might be in.
If you want to get a list of all files that ever existed, see here:
git log --pretty=format: --name-only --diff-filter=A | sort -u
Solution 2:
The git ls-files
command will do what you need.
Source: http://www.kernel.org/pub/software/scm/git/docs/git-ls-files.html
Solution 3:
git ls-files
will only print files in the current working directory.
If, for instance, you have a git repo for dotfiles (core.worktree = /
), then you will have files outside the git root and that simple command won't work anymore.
In short, this will work:
git --git-dir "`git rev-parse --git-dir`" \
-C "`git config core.worktree || pwd`" \
ls-files
Example:
mkdir ~/dotfiles
cd ~/dotfiles
git config core.worktree /
# Ignore all files by default, else Git will find all files under "/"
echo "*" > .git/info/exclude
# Add files at the git repo's root and somewhere in the work tree
touch README
git add -f README
git add -f /etc/ssh/sshd_config
# `git status` would now print:
# new file: ../../../etc/ssh/sshd_config
# new file: README
git status
git commit -m "Initial commit"
# At this point, `git ls-files` prints only:
# README
git ls-files
# But you can print all files inside the work tree. This will print:
# etc/ssh/sshd_config
# home/yourusername/dotfiles/README
git --git-dir "`git rev-parse --git-dir`" -C "`git config core.worktree || pwd`" ls-files
If you want paths specified relative to your current (shell) directory, this does the job:
alias gls='git ls-tree -r master --name-only HEAD "`git config core.worktree`"'
and in the example above, it would print
README
../../../etc/ssh/sshd_config