67

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?

  • Would checking a status from last command help here? ($?) – UVV Sep 11 '14 at 13:27
  • Could you give more details please? What is the main idea for your script? – tachomi Sep 11 '14 at 13:29
  • @tachomi I added the context in the edit – brentwpeterson Sep 11 '14 at 13:53
  • you could just assume it's not clean and do a git reset --hard origin/branch if that is what you are going for... like if you are trying to cleanup after compiling something, etc. – SnakeDoc Sep 11 '14 at 16:42
  • 1
    @SnakeDoc You could, but I assume that the inverse case would be more common, i.e. exit if the working directory is dirty to avoid mangling local changes. Considering both case would make question more useful for future readers. – Thomas Nyman Sep 11 '14 at 16:54
117

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.

  • 4
    ughhh git status --porcelain will exit with code 0 even if there are changes not staged for commit and untracked files. – Alexander Mills Aug 31 '18 at 21:19
  • I was interested in determining ahead of time if git stash would do anything (it doesn't output a useful return code). I had to add --ignore-submodules as otherwise git status would indicate submodule changes which git stash ignores. – Devin Lane Oct 27 '18 at 21:03
  • @AlexanderMills: I observed the same. But then checked what if [ -z was doing. The -z means that if the following string is empty, the if evaluates to true. In other words, if this git status --porcelain results in no string, the repo is clean. If not, it lists the modified/added/removed files and is no longer an empty string. The if then evaluates to false. – Adeynack 2 days ago
15

Use:

git diff-index --quiet HEAD

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

  • You should add link to post where did you get this!! – kyb Feb 14 '18 at 14:43
  • 6
    Returns 0 when there are untracked files in the current directory. – Adam Parkin Apr 12 '18 at 1:48
  • 1
    I don't think it's a return code, should be an exit code, I think there is a difference, return code is for bash functions, but maybe it's the same thing – Alexander Mills Aug 31 '18 at 21:20
  • 1
    If files had been touched/overwritten but are otherwise identical to the index, you need to first run git update-index --refresh before git diff-index HEAD. More info: stackoverflow.com/q/34807971/1407170 – sffc Nov 1 '18 at 2:28
  • @AdamParkin I just add all files with git add . before issuing it. Usually it's the way to use it in a script – ceztko Dec 7 '18 at 13:27
1

Minor extension to André's excellent answer.

This is one way to evaluate the results and also avoid a pitfall if you're in a script which previously issued set -e.

Untracked files are ignored.

set +e
git diff-index --quiet HEAD

if [ $? == 1 ] ; then
  set -e
  GIT_MODS="dirty"
else
  set -e
  GIT_MODS="clean"
fi
-1
#!/bin/bash
echo "First arg: $1"

cd $1

bob="Already up-to-date."
echo $bob

echo $(git pull) > s.txt
cat s.txt
if [ "$(cat s.txt)" == "$bob" ]
then
echo "up"
else
echo "not up"
fi
rm -rf s.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.