Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a Git commit exists on some remote?

I want to check whether a particular Git commit sha1 exists on a specific remote. I tried the following command:

git fetch <remote> <revision> -q --dry-run

The problem is that the command returns success not only when the revision is present on the remote, but also when it is present locally.

Is there some way to check this ignoring the local revision?

like image 950
bobeff Avatar asked Mar 26 '26 21:03

bobeff


2 Answers

If you have fetched all remote heads, you can try

$ git branch -r --contains <commit-sha>

This will give you a list of branches which contain the commit and you can see if the result includes remote ones.

If you want this to work without having fetched the remote, then a server-side config must be present: uploadpack.allowReachableSHA1InWant=true and the git versions of both server and client must be >= 2.5

like image 103
Borislav Stanimirov Avatar answered Mar 30 '26 07:03

Borislav Stanimirov


Adding --depth=1 seems to work.

I can not explain why it works, and I could not find any mention of this in the documentation. Probably wouldn't rely on this if I needed a robust solution.

like image 41
Sedfer Avatar answered Mar 30 '26 07:03

Sedfer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!