Git: software test if the index is dirty

I am writing a script that 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, if I already git add edited files that I do not want to automatically execute when the script starts? 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 the working tree are not dirty, then git stash apply will not add anything to the list, but git stash apply will incorrectly apply everything that is on top of the plate and discard the history of this influx.

How can I make a bash condition based on the fact that the index is dirty?

+4
source share
2 answers

git diff --cached --quiet

Previous answer:

git diff --cached will 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 it conditional. If there is no better answer, I can dump the output to a file and make a condition if the file is empty or not. But that sounds hack-ish. Of course, there is a better way.

+1
source

You can parse the output of git status -s .

 if git status -s | grep -q '^.M' then # ... fi 
0
source

Source: https://habr.com/ru/post/1395138/


All Articles