Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git commit nothing without an error?

Tags:

git

python

fabric

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") 
like image 748
kojiro Avatar asked Nov 14 '11 15:11

kojiro


People also ask

Can you commit nothing git?

Use git commit --allow-empty -m <message> to create an empty commit with the provided <message> .

How do I commit an empty commit?

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.

Can you git commit without message?

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.

Why does it say nothing to commit?

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.


2 Answers

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.

like image 113
Tobi Avatar answered Oct 07 '22 13:10

Tobi


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. 
like image 31
Sven Marnach Avatar answered Oct 07 '22 13:10

Sven Marnach