How to let Jenkins git commit only if there are changes? How to let Jenkins git commit only if there are changes? bash bash

How to let Jenkins git commit only if there are changes?


git diff --quiet && git diff --staged --quiet || git commit -am 'Added license headers'

This command do exactly what is required, 'git commit only if there are changes', while the commands in the other answers do not: they only ignore any error of git commit.


You can also just catch the error code with an OR operator:

git commit -m "Added license headers" || echo "No changes to commit"


To stop the build from breaking on the shell build step returning exit code 1 at any one point, e.g., when trying to make a git commit although there is nothing to commit, you can simply wrap the respective commands into an echo.

echo `git add -A && git commit -m "Added license headers"`

Now, whether there are untracked files to add to the index or not, and whether the working tree is dirty or clean, echo will return exit code 0, as there will be some string to be echoed.