How can I check, in a script, if local changes are present? Perhaps in combination with git describe
?
Since git 1.6.6, git-describe
has accepted a --dirty
option. If you have uncommitted changes in your working tree then you'll get output like this:
$ git describe --dirty
1.0.2-2-g215081f-dirty
You will need to make sure that both the two following properties are met:
That there are no differences between HEAD and the index cache
git diff-index --cached HEAD
That there are no differences between the index and the working tree:
git diff-files
Both commands take a --quiet
parameter which will set the exit code according to whether there are differences or not (starting some time after git 1.4). If you need to make it work on git 1.4, you need to run the commands without --quiet
and check whether they produce any output.
Note: git diff
is a porcelain command, and thus should not be used in scripts. Use above plumbing commands instead.
Example shell code, taken from my git_version.sh script:
git_dirty=yes
# git-1.4 does not understand "git-diff-files --quiet"
# git-1.4 does not understand "git-diff-index --cached --quiet HEAD"
if [ "x$($GIT diff-files)" = "x" ] && [ "x$($GIT diff-index --cached HEAD)" = "x" ]; then
git_dirty=no
fi
If you can require a git version >= 1.5, if git diff-files --quiet && git diff-index --quiet --cached HEAD; then
can replace above comparison.
Note: This solution (exactly like Antony's interactive git diff HEAD --quiet
) only discovers local changes relative to HEAD. However, local commits can arguable also be considered local changes, and naturally will not show up in any diff against HEAD. You will need to check the SHA1 value git describe
uses to detect whether HEAD is from a set of commits you consider not to be local changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With