16

I'm looking for a command similar to the following, but that always lists from the repository root, rather than the current working directory:

git ls-files --cached --others --exclude-standard

If possible I'd rather not use bash/batch, since I'm looking for a cross-platform solution.

orlp
  • 112,504
  • 36
  • 218
  • 315

3 Answers3

12

If Git aliases aren't an option, it can be done in 2 commands:

git ls-files $(git rev-parse --show-toplevel)
Community
  • 1
  • 1
Bluu
  • 5,226
  • 4
  • 29
  • 34
7

If you can create an alias,

git config --global alias.ls-files-root "! git ls-files"

Then you should be able to do

git ls-files-root --cached --others --exclude-standard

Explanation: Aliases starting with '!' are executed as shell commands from the top-level directory. Related: Is there a way to get the git root directory in one command?.

Community
  • 1
  • 1
tom
  • 21,844
  • 6
  • 43
  • 36
  • On Windows it needs double quotes - do double quotes also work for Linux? – orlp May 12 '15 at 12:36
  • No (in some shells, e.g. `bash`, `!` performs history expansion inside double quotes but not single quotes). I'll have to think about it -- double quotes won't work on Linux, single quotes and backslashes won't work on Windows... – tom May 12 '15 at 12:43
  • PowerShell on Windows should work with single quotes, if that's any help. – tom May 12 '15 at 13:00
  • It's been a while, but I just realised you can use `"! git ls-files"` in bash (the space after the exclamation mark prevents history expansion). That should be portable. – tom Jun 17 '15 at 02:10
0

Commands that expect pathspecs as an arg generally allow for leading :/ to mean the root of the working tree. So you can use:

git ls-files --full-name :/

This is better than combining with git rev-parse --show-toplevel output, because the latter won't allow you to find specific file in the repo. I.e. this: git ls-files -- $(git rev-parse --show-toplevel)"*filename.c" won't work, but this git ls-files ":/*filename.c" does. And it's shorter too.

Hi-Angel
  • 4,933
  • 8
  • 63
  • 86