threads / discuss / 63280

Command whose exit code indicates clean or dirty working directory

Subject: Command whose exit code indicates clean or dirty working directory

## tl;dr

3 messages between Apr 10, 2025 and Apr 10, 2025.

replies: 2people: 3as markdown or json

Ross Goldberg· Apr 10, 2025, 17:49 UTC · lore

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 & 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 & not ignored, etc.)?

 
 
   Sent from   Proton Mail for iOS
Junio C Hamano· Apr 10, 2025, 19:24 UTC · re: Ross Goldberg · lore

Re: Command whose exit code indicates clean or dirty working directory

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· Apr 10, 2025, 22:11 UTC · re: Ross Goldberg · lore

Re: Command whose exit code indicates clean or dirty working directory

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

← back to recent threads