简体   繁体   中英

check if changes are staged for a new GIT repository

I need to detect if there are any changes staged to commit for a new GIT repository via a shell script.

git diff --cached HEAD

Above command returns 0 when changes are staged, but requires a HEAD . If it cannot find one, it returns 128 (in case of new repository).

So How to detect if there were changes staged for a new repo and whether I need to commit?

If I try to commit when there are no changes to commit, it exits with return code 1 .

I am not sure if I can use this fact.

Does commit return 1 IF AND ONLY IF there are no changes to commit?

I couldn't find any documentation supporting this.

Note: I am not trying to auto-commit when a file is changed, I am aware I can use inotifywait for that.

As of git 1.7.4.1, just

git diff --cached --quiet

should do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM