I'm trying to write a fabric script that does a git commit
; however, if there is nothing to commit, git exits with a status of 1
. The deploy script takes that as unsuccessful, and quits. I do want to detect actual failures-to-commit, so I can't just give fabric a blanket ignore for git commit
failures. How can I allow empty-commit failures to be ignored so that deploy can continue, but still catch errors caused when a real commit fails?
def commit(): local("git add -p && git commit")
Use git commit --allow-empty -m <message> to create an empty commit with the provided <message> .
Git makes this process of pushing an empty commit super simple. It's like pushing a regular commit, except that you add the --allow-empty flag. You can see that the commit has been pushed to your branch without any changes after running the above commands.
Git does not recommend to commit without any message. Git commit messages are necessary to look back and see the changes made during a particular commit. If everyone will just commit without any message, no one would ever know what changes a developer has done.
The Git “nothing to commit, working directory clean” message tells us that we have not made any changes to our repository since the last commit. If this message appears and the contents of your remote repository are different to your local repository, check to make sure you have correctly set up an upstream branch.
Catch this condition beforehand by checking the exit code of git diff?
For example (in shell):
git add -A git diff-index --quiet HEAD || git commit -m 'bla'
EDIT: Fixed git diff
command according to Holger's comment.
From the git commit
man page:
--allow-empty Usually recording a commit that has the exact same tree as its sole parent commit is a mistake, and the command prevents you from making such a commit. This option bypassesthe safety, and is primarily for use by foreign SCM interface scripts.
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