orlp
orlp

Reputation: 117661

List files with git ls-files from root instead of current working directory

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.

Upvotes: 18

Views: 2967

Answers (3)

Hi-Angel
Hi-Angel

Reputation: 5619

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.

Upvotes: 2

Bluu
Bluu

Reputation: 5535

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

git ls-files $(git rev-parse --show-toplevel)

Upvotes: 14

tom
tom

Reputation: 22939

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?.

Upvotes: 8

Related Questions