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/*
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/*
On
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/*'
As the documentation says,
-xtakes 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/baror whatever. But that's fine: if your computer insists on storing a file namedbarinside a directory/folder namedutilinside a directory/folder namedfoowhen Git is storing a file namedfoo/util/bar, the pattern*/util/*matches Git's file name.Note that
-xonly excludes untracked files, so it only affectsgit ls-filesinvocations 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 asdir/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.)