Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

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?
by

1 Answer

Bharatgxwzm
Use:
git diff-index --quiet HEAD


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

Login / Signup to Answer the Question.