Toshimaru's Blog

git diff with exit code 1

If there’s a diff in git diff, I want the command to exit with an error.

That’s when to use --exit-code.


Make the program exit with codes similar to diff(1). That is, it exits with 1 if there were differences and 0 means no differences.

Let’s try it.

$ git diff --exit-code
diff --git a/_config.yml b/_config.yml
index e5c6d1aa..a2b7826c 100644
--- a/_config.yml
+++ b/_config.yml
@@ -1,3 +1,4 @@
 title: "Toshimaru's Blog"
 url: ""
 description: >

$ echo $?

The exit code is 1 (error).

If no diff in git diff:

$ git diff --exit-code

$ echo $?

The exit code is 0 (no error).

Load more