Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude files from git ls-files?

Tags:

git

bash

How do I list everything except markdown files? I have tried to run ls-files with the --exclude flag, but the excluded files are still shown in the output.

My git version is 2.6.4 (Apple Git-63)

$ git ls-files ChromeExt/read-coffee Node/README.md Node/web-scraping README.md  $ git ls-files --exclude *.md ChromeExt/read-coffee Node/README.md Node/web-scraping README.md 
like image 437
Nate Avatar asked Apr 20 '16 19:04

Nate


2 Answers

Git has its own way to extend glob patterns, and that includes a way to exclude files that match one pattern but not another. For example, the following will list all paths not ending in .md:

git ls-files -- . ':!:*.md' 

It's important to quote the pattern, because you want git to parse it, not the shell.

And to match all files ending with .js, except the ones ending with .min.js:

git ls-files -- '*.js' ':!:*.min.js' 

You can also use this with other commands, such as git grep:

git grep -w funcname -- '*.js' ':!:*.min.js' 

This syntax is explained under pathspec in gitglossary (or git help glossary or man gitglossary)

like image 70
geirha Avatar answered Sep 24 '22 13:09

geirha


That was already discussed in 2010:

There is no indication in the man page that -x doesn't apply to -c.

Hence the addition:

Since b5227d8, -x/--exclude does not apply to cached files.
This is easy to miss unless you read the discussion in the EXCLUDE PATTERNS section. Clarify that the option applies to untracked files and direct the reader to EXCLUDE PATTERNS.

The git ls-files man page does mentions:

-x <pattern> --exclude=<pattern> 

Skip untracked files matching pattern

If your Readme.md is tracked, the exclude pattern won't apply.

like image 37
VonC Avatar answered Sep 21 '22 13:09

VonC