My git repository tracks many kinds of files, such as Ruby files, YAML files, and Markdown files. I want a list of all Ruby files in my repo, but I can't use find -type f -name '*.rb'
because I also have a build directory that is not tracked by git but contains lots of ruby files. How can I list only those *.rb
files that are tracked by git?
Asked
Active
Viewed 1.0k times
21

jayhendren
- 4,286
- 2
- 35
- 59
1 Answers
33
Try the following command:
git ls-files '*.rb'

jayhendren
- 4,286
- 2
- 35
- 59
-
3or just `git ls-files *.rb` .. simpler – PA. Oct 19 '15 at 19:08
-
Thank you, @PA. this usage wasn't clear from the git-ls-files man page, and I couldn't find any mention of using arguments to `git ls-files` like this on SO. – jayhendren Oct 19 '15 at 22:21
-
playing bat golf? remove the single quotes – PA. Oct 20 '15 at 08:37
-
4@PA. - that doesn't work. Without the single quotes, the shell expands the glob pattern. – jayhendren Oct 20 '15 at 22:11
-
1ooops, OP didn't mention the shell used. It works on windows. – PA. Oct 21 '15 at 06:50
-
@jayhendren What does it mean "expands the glob pattern"? – 1252748 Jan 09 '19 at 21:02
-
On UNIX/Linux, if you are in a directory with the files `foo.rb` and `bar.rb`, all important shells will expand `git ls-files *.rb` to `git ls-files bar.rb foo.rb` _before_ executing `git`. If you want to keep the shell from expanding the glob pattern, you have to quote the pattern (using single or double quotes) or escape the glob character (`git ls-files \*.rb`). – hagello Jul 19 '23 at 15:21
-
1If there is not any `*.rb` file in your current directory, the behavior of `git ls-files *.rb` depends on the settings of your shell. This might be either an error (no file to match) or the shell might just pass `*.rb` to `git ls-files`. Moral of the story: Use quoting and/or escaping. Without quoting or escaping, it all just depends on circumstances. – hagello Jul 19 '23 at 15:24