I use git ls-files
in a script to collect all files tracked by git. Unfortunately if you rm
a file (rather than git rm
) it does not exist on the file system any more but it will still be listed by git ls-files
So the question: Is there a simple and efficient way to exclude files which do not exist on file system any more from either the git ls-files
output itself or by filtering it afterwards (using e.g. bash)? Something like
git ls-files --existing-only
Background: I want to create a CMake dummy target, which holds all files that are part of the project directory (i.e. tracked by git). And I use something like
execute_process(
COMMAND bash -c "cd ${CMAKE_SOURCE_DIR}; git ls-files"
OUTPUT_VARIABLE ADDITIONAL_PROJECT_FILES
)
to generate the file list. But unfortunately rm
ing a file and not yet staging the change will result in errors because the file can't be found by CMake any more..
Update: Before my edit I was talking about git rm
ing a file - which will be handled correctly by git ls-files
. But the problem persists: If someone removes a file (without using git for it) git ls-files
will list it (and I run into trouble).
comm -23 <(git ls-files | sort) <(git ls-files --deleted | sort)
From comm --help
:
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)