Dirty Git


git status --porcelain tells you the status of the working directory.

It sometimes happens that a program needs to know whether a Git working directory is in a dirty state or not, i.e. if Git thinks that there might be something to be done or not.

As an example, if you modify a tracked file then the situation is dirty because you haven’t committed the changes yet. Similarly, if you have a file that is neither tracked nor ignored, then again you have a dirty situation because you should clarify your intentions with respect to this file and lower down Git’s stress level. It’s so sensitive.

One interesting discussion about it can be found in Checking for a dirty index or untracked files with Git. My personal take-away is that the command:

git status --porcelain

is everything I need, because it tells everything that Git is not totally fine with. It’s also one of the answers.

If you need a true/false test, you can use this:

is_git_dirty() { [ -n "$(git status --porcelain)" ]; }

The typical use case that makes me look for this trick from time to time is packing stuff in a Git-tracked directory, e.g. via git archive or using git ls-files. I would certainly not want to pack something that is dirty, would I?

Comments? Octodon, , GitHub, Reddit, or drop me a line!