Reputation: 37806
Given an SSH URL such as [email protected]:<user>/<project>.git
, how could I test if a particular repository exists on github (assuming I also have the proper SSH keys set up)?
Using git clone
is undesirable, because if it does exist, it will immediately proceed to download it. I don't see any flags that can be passed to git clone
, that will simply check the repository's existence without downloading.
I suppose I could parse the URL and use the public API instead, by running a command such as this:
curl "https://api.github.com/repos/<user>/<project>"
but that gives back a lot of extraneous information instead of a simple yes/no.
Is there a better solution to simply checking if a repository exists?
Upvotes: 6
Views: 14898
Reputation: 1323553
You could simply use git ls-remote
:
git ls-remote [email protected]:username/reponame
(works with an https url too, which would not require setting any ssh key)
If the repo exists, that will list the HEAD and remote branches, without cloning or downloading anything else.
Another approach would be to simply test if the https url of the repo exists (or is 404).
See for instance "Can I use wget to check , but not download":
wget -q --spider address https://github.com/username/reponame
Using curl
is also possible: "script to get the HTTP status code of a list of urls?".
Upvotes: 11