2

The documentation for git-diff-files(1) says

Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, …) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B)...

However, the following commands do not show the added file (don't run this in a directory with an origin subdirectory):

start=$PWD
origin="$start/origin"

rm -rf $origin
mkdir $origin
cd $origin
touch 1.txt
echo "2" > 2.txt
git init
git add -A
git commit -m "init"

echo "1" > 1.txt
rm 2.txt
echo "3" > 3.txt
git diff-files --diff-filter=ADM

The output looks like:

$ git diff-files --diff-filter=ADM
:100644 100644 e69de... 0000... M      1.txt
:100644 000000 0cfbf... 0000... D      2.txt

Does git diff-files never show added files? The diff-filter option suggests that it is capable of selecting added files.

John Cheng
  • 571
  • 1
  • 4
  • 11
  • I think that for `1.txt` and `2.txt` their `D/M` statuses overwrite status `A`. `3.txt` is not yet added at all, it's untracked file. – phd Dec 31 '17 at 03:25
  • I acknowledge that in this case, `3.txt` is untracked. I take "untracked" to mean a file that is not in the index (files listed by `git ls-files -o`).Though the text of the instruction implies that --diff-filter=A will show `3.txt` as it is different between the working tree and the index and the difference is that, relative to the index, the file in the working tree is "added". In short, under what condition will `git diff-files` show a file as "A"? – John Cheng Jan 01 '18 at 00:42
  • I thought you need `git add 3.txt` but I tried that myself and cannot get status `A` myself. – phd Jan 01 '18 at 14:46

1 Answers1

3

Indeed git diff-files will never show added files.

The diff-files command shows the difference between the index and the files in the working copy. By definition a file that wasn't added to the index yet (via git add) will not be part of the index and can therefore not show up in any diff between the index and the working copy. Just adding the file to the index will make the file identical in the index and the working copy, hence it will not have any difference between the index and working copy and therefore not show up in diff-files either. Modifying an added file in the working copy will then directly result in the M state.

The reason that the documentation shows all of the possible --diff-filter arguments is that the diff options documentation is shared among all of the commands that take diff options (diff-files, diff-index, diff-tree, format-patch, log and show). Their respective documentation simply includes the diff-options documentation:

include::diff-options.txt[]

Seen in this line of the git-diff-files documentation source for example. The common diff-options then has the shared --diff-filter text starting here.

Since these files are processed when the documentation is built, as evidenced by the conditional

ifndef::git-format-patch[]

in the line above the --diff-filter documentation, the text could conceivably be varied for the diff-files command (or augmented to state the fact that some arguments do not apply). Opening a bug report to the git project for this change may make sense.

mmlr
  • 1,895
  • 11
  • 17
  • Great answer. I didn't think of looking at the source git source code to understand the documentation. Indeed, I never thought of using code to generate documentation. – John Cheng Jan 03 '18 at 15:51
  • And [this is](https://stackoverflow.com/a/13598028/7976758) how people use `git log --diff-filter=A` to find commit where a file was created. – phd Jan 07 '18 at 02:14