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 add
ed 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?