Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 952
skovalyov Avatar asked Aug 29 '13 15:08

skovalyov


People also ask

What is SHA 1 commit?

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.

How many characters is a commit SHA?

A commit in git always has a hash that contains 40 characters.


1 Answers

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

like image 144
remram Avatar answered Sep 29 '22 12:09

remram