How to check if the commit exists in a Git repository by its SHA-1

前端 未结 4 1604
野的像风
野的像风 2020-12-10 00:10

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.

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 00:54

    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

提交回复
热议问题