git/list[1] front-page[2] threads[3] people[4] search[5] about
 

git diff --exit-code returns 0 when binary files differ

From
KSKohei Shibata <shiba200712@gmail.com>
Date
Sep 21, 2024, 04:26 UTC
Message-ID
<CACpkL8WsNqhQ7SP27-XQwp1bzKjyUT6m2idFarZ2Z5rLVYg4pQ@mail.gmail.com>

I've encountered an issue with `git diff --exit-code` where it returns 0 for binary files that have actual changes.

> What did you do before the bug happened? (Steps to reproduce your issue)
1. Initialize a new git repository:
```
git init
```
2. Create a binary file and commit it:
```
echo '*.bin binary' > .gitattributes
dd if=/dev/urandom of=a.bin bs=32 count=1
git add .
git commit -m 'commit'
```
3. Modify the binary file:
```
echo a > a.bin
git diff --exit-code  # says "Binary files a/a.bin and b/a.bin differ"
echo $?               # returns 0
```
> What did you expect to happen? (Expected behavior)
`git diff --exit-code` should exit with 1
> What happened instead? (Actual behavior)
`git diff --exit-code` returns 0 even when the binary file is modified.
> Anything else you want to add:

I could not find the exact condition to change exit code. In some cases, depending on the content of the file, `git diff --exit-code` does return 1 as expected. I don't use an external diff tool.

[System Info] git version: git version 2.46.1 cpu: x86_64 no commit associated with this build sizeof-long: 8 sizeof-size_t: 8 shell-path: /bin/sh libcurl: 7.68.0 zlib: 1.2.11 uname: Linux 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 compiler info: gnuc: 9.4 libc info: glibc: 2.31 $SHELL (typically, interactive shell): /bin/bash

[Enabled Hooks]

Best regards, Kohei

Next: René Scharfe
Message 1 of 4 in “git diff --exit-code returns 0 when binary files differ”
  1. Kohei ShibataSep 21, 2024
  2. diff: report modified binary files as changes in builtin_diff()René Scharfe, Sep 21, 2024
  3. Thomas BraunSep 25, 2024
  4. Junio C HamanoSep 25, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.