gitgit-ls-files

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/*


Solution

  • 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.)