# Command whose exit code indicates clean or dirty working directory

3 messages from 2025-04-10 to 2025-04-10. Participants: Ross Goldberg, Junio C Hamano, brian m. carlson.
Thread: https://gitlist.dev/t/63280

## Ross Goldberg, 2025-04-10 17:49

Subject: Command whose exit code indicates clean or dirty working directory
Message-ID: <jreR52go49E9pIfAmIdRaoqIgKnVV7uOhR4Mbl9_p2dLG_7mPgO8qsUk7CaWC0nGbJD-2F4nGdQ8X65UtIQ5XOBNQXXzrnHnKbOCLCfEil0=@proton.me>
URL: https://gitlist.dev/e/jreR52go49E9pIfAmIdRaoqIgKnVV7uOhR4Mbl9_p2dLG_7mPgO8qsUk7CaWC0nGbJD-2F4nGdQ8X65UtIQ5XOBNQXXzrnHnKbOCLCfEil0%3D%40proton.me

```
What is the simplest git command that returns 0 if the working directory is completely clean, or nonzero if it is dirty in any way (staged diff, unstaged diff, file that is both untracked &amp; not ignored, etc.), with nothing printed to stdout or to stderr?
If the simplest such command is complex, is there any feature request to create a simple command, possibly with options that specify what is considered dirty (staged diffs, unstaged diffs, untracked &amp; not ignored, etc.)?

 
 
   Sent from   Proton Mail for iOS

```

## Junio C Hamano, 2025-04-10 19:24

Subject: Re: Command whose exit code indicates clean or dirty working directory
Message-ID: <xmqqlds7om51.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqlds7om51.fsf%40gitster.g
In-Reply-To: <jreR52go49E9pIfAmIdRaoqIgKnVV7uOhR4Mbl9_p2dLG_7mPgO8qsUk7CaWC0nGbJD-2F4nGdQ8X65UtIQ5XOBNQXXzrnHnKbOCLCfEil0=@proton.me>

```
Ross Goldberg <ross.goldberg@proton.me> writes:

> If the simplest such command is complex, is there any feature request to create a simple
> command, possibly with options that specify what is considered dirty (staged diffs,
> unstaged diffs, untracked & not ignored, etc.)?

With the "possibly with options" part, I doubt there is.  Once you
start having to specify options, you admit that the problem space is
no longer simple.  Depending on occasion, you would be caring
different kind of cleanliness.

If you want to know if there are changes added to the index, "git
diff --cached --quiet" would be a way to ask about it.  If you want
to know if there are changes in the working tree relative to the
index, "git diff --quiet" would be a way to ask about it.  If you
want to know if there are changes in the working tree relative to
the HEAD commit, "git diff --quiet HEAD" would be.  You can use them
to ask about various things you care about, and you can combine
yourself, instead of using a single command with options to specify
which questions to ask.

Having said all that, if what you care the most is simplicity, you
can probably use "git describe --always --dirty", and see if the
output ends with "-dirty" suffix, perhaps?  I suspect that it does
not care about untracked files, which you may have to independently
ask command like "git clean -n".



```

## brian m. carlson, 2025-04-10 22:11

Subject: Re: Command whose exit code indicates clean or dirty working directory
Message-ID: <Z_hB9gZL6FE7PezC@tapette.crustytoothpaste.net>
URL: https://gitlist.dev/e/Z_hB9gZL6FE7PezC%40tapette.crustytoothpaste.net
In-Reply-To: <jreR52go49E9pIfAmIdRaoqIgKnVV7uOhR4Mbl9_p2dLG_7mPgO8qsUk7CaWC0nGbJD-2F4nGdQ8X65UtIQ5XOBNQXXzrnHnKbOCLCfEil0=@proton.me>

```
On 2025-04-10 at 17:49:23, Ross Goldberg wrote:
> What is the simplest git command that returns 0 if the working directory is completely clean, or nonzero if it is dirty in any way (staged diff, unstaged diff, file that is both untracked &amp; not ignored, etc.), with nothing printed to stdout or to stderr?
> If the simplest such command is complex, is there any feature request to create a simple command, possibly with options that specify what is considered dirty (staged diffs, unstaged diffs, untracked &amp; not ignored, etc.)?

Here's what I've used:

[ -z "$(git status --porcelain)" ]

That assumes you know the directory is a non-bare working tree.

`git status --porcelain` prints nothing if the working directory is
clean and prints what's dirty otherwise.
-- 
brian m. carlson (they/them)
Toronto, Ontario, CA

```
