# strange behavior of git diff-index

3 messages from 2019-05-22 to 2019-05-22. Participants: Oussama Ghorbel, Johannes Schindelin.
Thread: https://gitlist.dev/t/51151

## Oussama Ghorbel, 2019-05-22 10:00

Subject: strange behavior of git diff-index
Message-ID: <VI1PR08MB3454100258C23CB1F2B709F5EB000@VI1PR08MB3454.eurprd08.prod.outlook.com>
URL: https://gitlist.dev/e/VI1PR08MB3454100258C23CB1F2B709F5EB000%40VI1PR08MB3454.eurprd08.prod.outlook.com

```
Hi,

git diff-index is giving me incorrect information, however if I run git diff, then git diff-index again it will show the correct information.
The steps are the following:
$ git diff-index --name-only HEAD
git appears to list all files in the project irrespective if they modified or not
$ git diff
$ git diff --cached
will show nothing. So I don't have any modification. 
Now strangely if I run git diff-index, it will also show nothing which is correction behavior.

my git version is 2.7.4
Any explanation?

Thanks,
Oussama
```

## Johannes Schindelin, 2019-05-22 14:54

Subject: Re: strange behavior of git diff-index
Message-ID: <nycvar.QRO.7.76.6.1905221651270.46@tvgsbejvaqbjf.bet>
URL: https://gitlist.dev/e/nycvar.QRO.7.76.6.1905221651270.46%40tvgsbejvaqbjf.bet
In-Reply-To: <VI1PR08MB3454100258C23CB1F2B709F5EB000@VI1PR08MB3454.eurprd08.prod.outlook.com>

```
Hi Oussama,

On Wed, 22 May 2019, Oussama Ghorbel wrote:

> git diff-index is giving me incorrect information, however if I run git diff, then git diff-index again it will show the correct information.
> The steps are the following:
> $ git diff-index --name-only HEAD
> git appears to list all files in the project irrespective if they modified or not
> $ git diff
> $ git diff --cached
> will show nothing. So I don't have any modification. 
> Now strangely if I run git diff-index, it will also show nothing which is correction behavior.
>
> my git version is 2.7.4
> Any explanation?

The low-level `diff-index` command is meant to be used in scripts, and
does specifically *not* refresh the index before running. Meaning that it
could possibly mistake a file for being modified even if it is not
modified, just because it is marked "modified" in the index [*1*].

Short answer: use the high-level command `git diff` that is intended for
human consumption.

Ciao,
Johannes

Footnote *1*: It is actually a bit more complicated than that: the index
stores metadata such as mtime, size, uid, etc, and compares that to the
metadata on disk. If there is any mismatch, or if everything matches but
the mtime *also* matches the index file's itself, the file is considered
not up to date, i.e. marked as modified.

```

## Oussama Ghorbel, 2019-05-22 15:27

Subject: Re: strange behavior of git diff-index
Message-ID: <1b86251f-1b64-dfdf-7e40-e8a0c1e42e55@iopsys.eu>
URL: https://gitlist.dev/e/1b86251f-1b64-dfdf-7e40-e8a0c1e42e55%40iopsys.eu
In-Reply-To: <nycvar.QRO.7.76.6.1905221651270.46@tvgsbejvaqbjf.bet>

```
Hi Johannes,

Thanks a lot for the clarification!

Regards,
Oussama
On 5/22/19 4:54 PM, Johannes Schindelin wrote:
> Hi Oussama,
>
> On Wed, 22 May 2019, Oussama Ghorbel wrote:
>
>> git diff-index is giving me incorrect information, however if I run git diff, then git diff-index again it will show the correct information.
>> The steps are the following:
>> $ git diff-index --name-only HEAD
>> git appears to list all files in the project irrespective if they modified or not
>> $ git diff
>> $ git diff --cached
>> will show nothing. So I don't have any modification.
>> Now strangely if I run git diff-index, it will also show nothing which is correction behavior.
>>
>> my git version is 2.7.4
>> Any explanation?
> The low-level `diff-index` command is meant to be used in scripts, and
> does specifically *not* refresh the index before running. Meaning that it
> could possibly mistake a file for being modified even if it is not
> modified, just because it is marked "modified" in the index [*1*].
>
> Short answer: use the high-level command `git diff` that is intended for
> human consumption.
>
> Ciao,
> Johannes
>
> Footnote *1*: It is actually a bit more complicated than that: the index
> stores metadata such as mtime, size, uid, etc, and compares that to the
> metadata on disk. If there is any mismatch, or if everything matches but
> the mtime *also* matches the index file's itself, the file is considered
> not up to date, i.e. marked as modified.

```
