user1795832
user1795832

Reputation: 2160

git ls-files exclude files in a directory

I'm trying to run git ls-files and exclude a specific directory from it. Does the --exclude flag only exclude file patterns, and not directories?

git ls-files -x */util/*

Upvotes: 0

Views: 2358

Answers (2)

jthill
jthill

Reputation: 60255

To make exclusion patterns work on tracked files, ask for excluded-and-tracked files and invert your patterns.

So if you want to list everything that's tracked and outside the media directory,

git ls-files -ix\* -x'!media/*'

which defaults to listing tracked files and matches that sequence of patterns.

To filter both tracked and untracked, git ls-files -ocix\* -x'!media/*'

Upvotes: 1

torek
torek

Reputation: 487883

As the documentation says, -x takes a pattern argument, so it only excludes patterns.

But Git only stores files: "directories" or "folders" only exist in the imagination—and, alas, reality—of your computer's operating system, not in Git. Git just has files named foo/util/bar or whatever. But that's fine: if your computer insists on storing a file named bar inside a directory/folder named util inside a directory/folder named foo when Git is storing a file named foo/util/bar, the pattern */util/* matches Git's file name.

Note that -x only excludes untracked files, so it only affects git ls-files invocations that print the names of files found in the work-tree, not those that print the names of files found in the index. Files stored in the index literally do have long names that may contain slashes, such as dir/sub/file.ext: the index has no ability to store directories / folders. (This is why Git cannot store an empty directory. Git builds new commits from whatever is in the index, and the index does not store directories, so Git cannot build a commit containing an empty directory.)

Upvotes: 1

Related Questions