In a similar topic Validate if commit exists they recommend:
git rev-list HEAD..$sha
If it exits without error code than the commit exists.
But is it efficient enough just for validation?
I was thinking about this option:
git cat-file commit $sha
Is it correct for my task and are there any other ideas?
The SHA1 of the commit is the hash of all the information. And because this hash is unique to its content, a commit can't change. If you change any data about the commit, it will have a new SHA1. Even if the files don't change, the created date will. A commit is a code snapshot.
A commit in git always has a hash that contains 40 characters.
You can just run git cat-file -t $sha
and check it returns "commit". You are right, you don't need to actually print the actual object for that...
I'm not 100% sure that what goes on behind the scene is more efficient, though.
test $(git cat-file -t $sha) == commit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With