Can I check the existence of a git repo using an SSH URL?

11,825

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?".

Share:
11,825
Trevor Hickey
Author by

Trevor Hickey

and programming

Updated on June 15, 2022

Comments

  • Trevor Hickey
    Trevor Hickey less than a minute

    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?

  • chepukha
    chepukha over 7 years
    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?
  • VonC
    VonC over 7 years
    @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?
  • chepukha
    chepukha over 7 years
    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.
  • VonC
    VonC over 7 years
    @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.