ashwin929
ashwin929

Reputation: 43

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.

Upvotes: 0

Views: 99

Answers (1)

jthill
jthill

Reputation: 60443

As of git 1.7.4.1, just

git diff --cached --quiet

should do it.

Upvotes: 1

Related Questions