skip to Main Content
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


  1. Chosen as BEST ANSWER

    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 diff --name-only --diff-filter=M
    

  2. 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:

    • use git status --porcelain (not sure if this exists in the truly ancient Git you’re using with CentOS here), and interpret its output;
    • use 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; or
    • use git ls-files -m -d -t (which can be compressed to git 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:

    $ git ls-files -mdt
    C Makefile
    R zlib.c
    C zlib.c
    

    Note how zlib.c appears twice, once as Removed, and once as Changed (modified). It is just a small matter of programming to have the R status override the C status when you read the results.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search