Determine if Git working directory is clean from a script

I have a script which runs rsync with a Git working directory as destination. I want the script to have different behavior depending on if the working directory is clean (no changes to commit), or not. For instance, if the output of git status is as below, I want the script to exit:

git status
Already up-to-date.
# On branch master
nothing to commit (working directory clean)
Everything up-to-date

If the directory is not clean then I would like it to execute some more commands.

How can I check for output like the above in a shell script?

Asked By: brentwpeterson

||

Parsing the output of git status is a bad idea because the output is intended to be human readable, not machine-readable. There’s no guarantee that the output will remain the same in future versions of Git or in differently configured environments.

UVVs comment is on the right track, but unfortunately the return code of git status doesn’t change when there are uncommitted changes. It does, however, provide the --porcelain option, which causes the output of git status --porcelain to be formatted in an easy-to-parse format for scripts, and will remain stable across Git versions and regardless of user configuration.

We can use empty output of git status --porcelain as an indicator that there are no changes to be committed:

if [ -z "$(git status --porcelain)" ]; then 
  # Working directory clean
else 
  # Uncommitted changes
fi

If we do not care about untracked files in the working directory, we can use the --untracked-files=no option to disregard those:

if [ -z "$(git status --untracked-files=no --porcelain)" ]; then 
  # Working directory clean excluding untracked files
else 
  # Uncommitted changes in tracked files
fi

To make this more robust against conditions which actually cause git status to fail without output to stdout, we can refine the check to:

if output=$(git status --porcelain) && [ -z "$output" ]; then
  # Working directory clean
else 
  # Uncommitted changes
fi

It’s also worth noting that, although git status does not give meaningful exit code when the working directory is unclean, git diff provides the --exit-code option, which makes it behave similar to the diff utility, that is, exiting with status 1 when there were differences and 0 when none were found.

Using this, we can check for unstaged changes with:

git diff --exit-code

and staged, but not committed changes with:

git diff --cached --exit-code

Although git diff can report on untracked files in submodules via appropriate arguments to --ignore-submodules, unfortunately it seems that there is no way to have it report on untracked files in the actual working directory. If untracked files in the working directory are relevant, git status --porcelain is probably the best bet.

Answered By: Thomas Nyman

Use:

git update-index --really-refresh
git diff-index --quiet HEAD

The return code reflects the state of the working directory (0 = clean, 1 = dirty). Untracked files are ignored.

Answered By: André van Herk

Minor extension to AndrĂ©’s excellent answer.

Untracked files are ignored.

git update-index --really-refresh
if git diff-index --quiet HEAD
then
  GIT_MODS="clean"
else
  GIT_MODS="dirty"
fi

If you want to avoid putting stuff in stdout, you may want to add >> /dev/null or similar on the update-index line.

Answered By: orion elenzil

I made this kind of test

TEST=$(git status --porcelain|wc -l)
if [ 0 -eq $TEST ]; then
   echo "No changes"
else
   echo "Changes"
fi 
Answered By: PHZ.fi-Pharazon

How about using git describe --broken --dirty --all and checking the result to see if it ends in -dirty (or -broken); then, using the same thing on any submodules with git submodule foreach ... ?

This requires a modern (version >= 3.x) of bash as I understand it, it also does not investigate untracked files – which is not unreasonable in my opinion (at least they shouldn’t be clobbered if a difference branch is checked out!) – note that it only returns a zero exit status if everything is clean:

#!/bin/bash

RESULT=$(git describe --broken --dirty --all)
STATUS="clean"
if [[ "${RESULT}" =~ broken$ ]]; then
    # Bail out now, in case it is not safe to look for sub-modules
    echo "Main repository is broken."
    exit -3
elif [[ "${RESULT}" =~ dirty$ ]]; then
    echo "Main repository is dirty - not checking any submodules."
    STATUS="dirty"
    exit -1
else
    git submodule foreach --recursive --quiet 'RESULT=$(git describe --broken --dirty --all) 
        if [[ "${RESULT}" =~ broken$ ]]; then
            echo "Submodule ${name} is broken."
            exit -3
        elif [[ "${RESULT}" =~ dirty$ ]]; then
            echo "Submodule ${name} is dirty."
            STATUS="dirty"
        else
            echo "Submodule ${name} is clean."
        fi' 
    echo -n "Main repository is clean "
    if [ "${STATUS}" = "dirty" ]; then
        echo "- but at least one submodule is dirty."
        exit -2
    else
        echo "- and so are any submodules."
    fi
fi
exit 0
Answered By: SlySven

git status --short can be used for that. It prints list of modified or untracked files without clutter. Empty output means no changes.

if [ -z "$(git status --short)" ]; then
  echo "Clean"
else
  echo "Dirty"
fi

Output is almost identical (if not identical) like in git status --porcelain suggested in other answers, but --short is more straightforward and easier to remember. On the other hand, --porcelain API will keep its behavior across Git versions and configurations (see docs) which is an important factor, too.

Answered By: skalee

If you just want a 0 or 1 process exit code, then you can just run a test outside of an if-then block.

#!/bin/bash

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

or

#!/bin/bash

test -z "$(git status --porcelain)"

Example:

$ test -z "$(git status --porcelain)"
$ echo $?
0

$ echo 'hello world' > fish.txt
$ test -z "$(git status --porcelain)"
$ echo $?
1
Answered By: Cameron Hudson
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.