6

Given an SSH URL such as git@github.com:<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?

Magnus Bäck
  • 11,381
  • 3
  • 47
  • 59
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271

1 Answers1

11

You could simply use git ls-remote:

 git ls-remote git@github.com: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?".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • When using `git ls-remote` on an non-existing repo, I'm prompted with `Username for ...` and `Password for ...`. The error is only thrown after I hit enter to go through them. Is there any option to ignore that? – chepukha Feb 01 '15 at 06:36
  • @chepukha strange: I don't see that, with ssh or https: `git ls-remote https://github.com/username/reponame` will give immediatly: `remote: Repository not found. fatal: repository 'https://github.com/username/reponame/' not found`. Are you behind a proxy? – VonC Feb 01 '15 at 08:41
  • Here's what I got:```$ git ls-remote https://github.com/substack/tape HEAD 51f2f97d7eade23b1e23b7cfea37f449ade5b9c3 HEAD $ git ls-remote https://github.com/substack/tape2 Username for 'https://github.com': Password for 'https://github.com': remote: Repository not found. fatal: Authentication failed for 'https://github.com/substack/tape2/'```. No I'm not behind any proxy. – chepukha Feb 02 '15 at 03:56
  • @chepukha interesting. Make it an independent question (with a link back to this one) for others to have a go at it. Do mention your OS and git version too. – VonC Feb 02 '15 at 06:12