7

I am trying to create a Bash script that knows if there are changes in current working directory. I know that

$ git status

returns a message like "nothing to commit". What I am trying to, is to define a variable to true or false. This boolean value will tell me if there are or not changes.

Obviously I am not an expert of bash scripts. I tried something like this,

there_are_changes=$(git status | grep nothin)
echo $there_are_changes

but it doesn't work as expected. What should I do?

jub0bs
  • 54,300
  • 24
  • 162
  • 166
sensorario
  • 18,131
  • 26
  • 91
  • 148
  • possible duplicate of [How do I programmatically determine if there are uncommited changes?](http://stackoverflow.com/questions/3878624/how-do-i-programmatically-determine-if-there-are-uncommited-changes) – stiemannkj1 Feb 27 '15 at 19:06
  • I've tried to delete questions, ... but I am reading very very very useful answers. I'll keep it!!! – sensorario Feb 27 '15 at 19:12
  • Do you have python available? If so, scripts like this one are probably easier to write in python. – amahfouz Feb 27 '15 at 18:59

2 Answers2

28

The git-diff man page describes two options of relevance here:

--quiet
Disable all output of the program. Implies --exit-code.

and

--exit-code
Make the program exit with codes similar to diff(1). That is, it
exits with 1 if there were differences and 0 means no differences.

Therefore, a robust approach would be to run

git diff --quiet; nochanges=$?

The shell variable nochanges will be equal to 0 (i.e. true) if there are no changes, and 1 (i.e. false) otherwise.

You can then use the value of nochanges in conditional statements as follows:

if [ $nochanges -eq 0 ]; then
    # there are no changes
else
    # there are changes
fi

Alternatively, if you don't need to store the exit status in a variable, you can do:

if git diff --quiet; then
    # there are no changes
else
    # there are changes
fi

Since git diff is a porcelain Git command and you want to do things programmatically, you should probably use the plumbing Git command called git diff-index instead (which also has a --quiet flag, but which must be supplied a tree-ish argument):

if git diff-index --quiet HEAD; then
    # there are no changes
else
    # there are changes
fi

As pointed out in a comment below, the approach outlined above does not cover untracked files. To cover them as well, you can use the following instead:

if [ -z "$(git status --porcelain)" ]; then
    # there are no changes
else
    # there are changes
fi
jub0bs
  • 54,300
  • 24
  • 162
  • 166
  • 1
    I didn't know about the `--quiet` flag for `git diff`, that's neat! – jmervine Feb 27 '15 at 19:10
  • As `git diff --quiet; nochanges=$?` will stop execution in scripts that are set to stop after a non zero exit code, you can instead use `CHANGES=false; (git diff --quiet) || CHANGES=true` – Alasdair McLeay Jun 02 '20 at 16:11
  • How do we only check for changes? i.e. avoid the `else`? – Richard Jan 06 '21 at 15:25
  • This does NOT work to detect untracked files. To check for create/modify/delete, you should check if `git status --porcelain` outputs text (such as via `if [[ -n "$(git status --porcelain)" ]]; then echo "things changed"; fi` ) – jeremysprofile Aug 23 '21 at 17:38
  • 1
    @jeremysprofile Good point. I've amended by answer accordingly. – jub0bs Sep 10 '21 at 07:50
4

You can check if the variable is set by using the -n expression.

#!/bin/bash
CHANGESTOCOMMIT=$(git status | grep 'Changes to be com')
UNSTAGEDCHANGES=$(git status | grep 'Changes not staged')

# If there are staged changes:
if [ -n "$CHANGESTOCOMMIT" ]; then
    echo "Changes need to be committed"
fi
if [ -n "$UNSTAGEDCHANGES" ]; then
    echo "Changes made but not staged."
fi

Git tracks changed files that are both staged for committing, and also unstaged files, so your script might want to check both options (or not). The -n operator checks to see if the variable has been set - if it is empty it will return false.

An alternative is -z which returns True if it is empty (the logical opposite of -n. For a full list of conditional expressions please refer to the Bash Reference Manual.

Aaron D
  • 7,134
  • 3
  • 42
  • 46
  • Yeah, the downvote doesn't make sense for this. @Jubobs answer is a bit cleaner, but you got a +1 from me. – jmervine Feb 27 '15 at 19:08
  • I mixed up `-z` and `-n` so my script had a logic error. Now fixed. – Aaron D Feb 27 '15 at 19:11
  • 1
    That command substitution is inelegant; `git diff --quiet` already provides the functionality sought after by the user. – jub0bs Feb 27 '15 at 19:12
  • 1
    You're right, your solution is more elegant for this use case. The question was about how to check if the variable was set (true or false) in bash, but your solution sidesteps the need for grep completely. – Aaron D Feb 27 '15 at 19:14