In “Check if Git working tree is dirty”, git diff and git status were used to check if a working directory is dirty.
To get the exit status code, you can extract the value from the shell variable $?:
git diff --quiet; echo $?
Exit code of 0 means the working directory is clean. Exit code of 1 means the working directory is dirty.
| Code | Type |
|---|---|
| 0 | Clean |
| 1 | Dirty |
For faster performance, you can use git diff-index since it’s a lower level operation:
git diff-index --quiet HEAD; echo $?