41

Is there any way how to check whether git repository exist just before perform git clone action? Something like git clone $REMOTE_URL --dry-run?

I would like to implement fast checking method which will check existence before the clone action will be performed. I expect that connection might be slow and repository might be big even more than 1GB, so I don't want to rely on time extensive operation due to simple validation. The clone will be performed asynchronously in the background when validation passes.

I found this https://superuser.com/questions/227509/git-ping-check-if-remote-repository-exists but it works from git repository context after initial clone action is done.

Maybe there is some git call which works without git repository context and return "some data" if repository on destination exist or error if doesn't.

James Jones
  • 3,732
  • 5
  • 23
  • 44
Jan Stanicek
  • 1,061
  • 1
  • 13
  • 27

4 Answers4

50

The answer that you linked does what you want it to do.

Try running this in a folder without a repository in it:

git ls-remote https://github.com/git/git

It should show you the references on the remote, even though you haven't added a local repository with git init or done a git clone.

See more: https://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html

Leigh
  • 11,344
  • 4
  • 27
  • 35
9

You can easily do this using GitHub's API (make sure to leave out .git from the repo name):

curl https://api.github.com/repos/<user>/<repo>

If the repository is not found:

curl https://api.github.com/repos/coldhawaiian/blarblar
{
  "message": "Not Found",
  "documentation_url": "https://developer.github.com/v3"
}

Otherwise:

curl https://api.github.com/repos/coldhawaiian/git-ninja-toolkit
{
  "id": 11881218,
  "name": "git-ninja-toolkit",
  "full_name": "coldhawaiian/git-ninja-toolkit",
  "owner": {
    "login": "coldhawaiian",
    "id": 463580,
    "avatar_url": "https://avatars.githubusercontent.com/u/463580?",
    "gravatar_id": "f4de866459391939cd2eaf8b369d4d09",
    "url": "https://api.github.com/users/coldhawaiian",
    "html_url": "https://github.com/coldhawaiian",
    "followers_url": "https://api.github.com/users/coldhawaiian/followers",
    // etc...
    "type": "User",
    "site_admin": false
  },
  // etc...
}
  • 5
    Of course, this only helps if the remote is hosted on GitHub. The `ls-remote` solution should work with any remote. – Chris May 28 '14 at 16:53
  • 2
    For Bitbucket, the following URL can be used: https://api.bitbucket.org/2.0/repositories/{user}/{repo_name} If the Repository doesn't exist: it returns `{'message' : Repository user/repo_name not found}` If the Repository exists, it returns `Forbidden`. Which isn't exactly informative, but it works. – sparkonhdfs Jan 29 '16 at 20:24
  • The other benefit of git ls-remote is it works for private repositories if you are authenticated in command line. – Bibek Shrestha Apr 21 '21 at 17:27
  • Fwiw, this [doesn't seem to work for wikis](https://github.community/t/api-for-repositorys-wiki/902) unfortunately. Guess I'll have to use the `ls-remote` method. – V. Rubinetti May 12 '21 at 18:03
  • Another downside of using GitHub API is the fact that there is a limit on GitHub API requests (60/min if not authenticated), but with `ls-remote` there is no such a limit. – Emadpres Oct 08 '21 at 09:07
7

You can use ls-remote:

$ git ls-remote git@github.com:github/markup.git
794d5d36dae7c1a9a0ed3d452ad0ffa5ab2cc074    HEAD
d27b5b1c5ae84617d4a9356eaf565c7b555c4d1d    refs/heads/can_render_regex
c03cce8f271d683c9cdeb5253c9cb21b5f0e65a0    refs/heads/fix-tests-part-deux
# Snip many more lines

$ git ls-remote git@github.com:nothing/here.git
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Note that ls-remote exits 128 when this occurs, so you could redirect the command output if you don't need it and just check the command's return value.

Note that in some cases you may get prompted for input, e.g. if you try to access a GitHub https://... repository. This is because there may be a matching private repository.

Chris
  • 112,704
  • 77
  • 249
  • 231
2

Try the following:

curl -u "$username:$token" https://api.github.com/repos/user/repository-name

If the Git repository does not exist, the output will be:

{
    "message": "Not Found",
    "documentation_url": "https://developer.github.com/v3"
}

If the Git repository exists, the output will be:

{
    "id": 123456,
    "name": "repository-name",
    "full_name": "user/repository-name",
    "owner": { ... } 
    ............
}
honk
  • 8,101
  • 11
  • 72
  • 75
Ravi Prajapati
  • 1,736
  • 1
  • 10
  • 8