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

Viewed 242

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?

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

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.

Related