Hi! How can I check, in a script, if local changes are present? Perhaps in combination with git describe
?
views:
121answers:
3git status
exits with non-zero status if there is no local changes.
But I don't understand what you mean "in combination with git describe".
git diff --quiet
returns with exit status 1 if there're changes and 0 if there're not.
Keep in mind that it will show diff between staged changes and the working dir.
If you're interested in changes between your HEAD and working dir, you should use git diff HEAD --quiet
.
--quiet
implies --exit-code
.
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.