I am writing a script which will do this:
#!/bin/bash
mysqldump -u user -p database > db_file
git add db_file
git commit -m 'db_file updated by script'
However, what if the index is dirty when I run this script? That is, what if I already git added files that I want to not have automatically committed when this script runs? I could do this:
#!/bin/bash
mysqldump -u user -p database > db_file
git stash
git add db_file
git commit -m 'db_file updated by script'
git stash apply
But now the problem is that if the index and working tree is not dirty, then git stash doesn’t add anything to the stash list, but git stash apply will incorrectly apply whatever is on top of the stash and discard the history of that stash.
How can I make a bash conditional based on if the index is dirty?
git diff --cached --quietPrevious answer:
git diff --cachedwill show exactly what I want to know. The only problem is that the return value is true no matter what, so it’s hard to make a conditional off of that. If there is not better answer, then I can dump the output to a file and make a condition on if the file is empty or not. But that sounds painfully hack-ish. Surely there is a better way.