git ls-files --modified
is supposed to list only modified files. However, it includes deleted files as well.
On the other hand,
git ls-files --deleted
lists only the deleted files, which is expected.
Why are deleted files considered by git as modified when there is a separate command just to list deleted files?
System info:
> git --version
git version 1.7.1
> lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-ia32:base-4.0-noarch:core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 6.8 (Final)
Release: 6.8
Codename: Final
> uname -m
x86_64
> echo $0
/bin/tcsh
2
Answers
Just FYI for those looking for an alternative command to list modified files, excluding deleted files, without the need to parse/interpret the output for further processing -
git ls-files --modified
considers a deleted file modified, because it is in fact modified (so much so as to be entirely missing).If you need to subtract off deleted files from the
--modified
or-m
output, you have various options:git status --porcelain
(not sure if this exists in the truly ancient Git you’re using with CentOS here), and interpret its output;git ls-files
twice, once to get the modified-including-deleted list, then again to get the deleted list, and subtract off the deleted files; orgit ls-files -m -d -t
(which can be compressed togit ls-files -mdt
) and interpret its output.For my test case for this last one, I took a clone of the Git repository for Git, modified the Makefile slightly in the working tree, and removed
zlib.c
from the working tree:Note how
zlib.c
appears twice, once asR
emoved, and once asC
hanged (modified). It is just a small matter of programming to have theR
status override theC
status when you read the results.