检查git存储库是否存在

Jan*_*cek 21 git github

有没有办法在执行git clone操作之前检查git存储库是否存在?有点像git clone $REMOTE_URL --dry-run

我想实现快速检查方法,它将在执行克隆操作之前检查是否存在.我希望连接速度可能很慢,而且存储库可能大于1GB,因此我不希望由于简单的验证而依赖于大量的时间操作.验证通过后,克隆将在后台异步执行.

我发现这个https://superuser.com/questions/227509/git-ping-check-if-remote-repository-exists但它在初始克隆操作完成后从git repository context起作用.

也许有一些git调用在没有git存储库上下文的情况下工作,如果存在目标存储库则返回"某些数据",如果不存在则返回错误.

Lei*_*igh 28

您链接的答案就是您希望它做的事情.

尝试在没有存储库的文件夹中运行它:

git ls-remote https://github.com/git/git
Run Code Online (Sandbox Code Playgroud)

它应该显示远程的引用,即使您没有添加本地存储库git init或已完成git clone.

查看更多:https://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html

  • +1 适用于不依赖 github api 的通用解决方案。 (2认同)
  • `git ls-remote git@github.com:johndoe/my-cool-repo.git` <- 如果您传递的是 git url 而不是 https,则不会要求您进行身份验证 (2认同)

Chr*_*ris 7

您可以使用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.
Run Code Online (Sandbox Code Playgroud)

请注意,ls-remote发生这种情况时会退出 128,因此如果不需要,您可以重定向命令输出,只需检查命令的返回值。

请注意,在某些情况下,您可能会收到输入提示,例如,如果您尝试访问 GitHubhttps://...存储库。这是因为可能存在匹配的私有存储库。


小智 7

您可以使用GitHub的API轻松完成此操作(确保.git从repo名称中删除):

curl https://api.github.com/repos/<user>/<repo>
Run Code Online (Sandbox Code Playgroud)

如果未找到存储库:

curl https://api.github.com/repos/coldhawaiian/blarblar
{
  "message": "Not Found",
  "documentation_url": "https://developer.github.com/v3"
}
Run Code Online (Sandbox Code Playgroud)

除此以外:

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...
}
Run Code Online (Sandbox Code Playgroud)

  • 当然,这只有在远程托管在GitHub上时才有用.`ls-remote`解决方案应该适用于任何遥控器. (3认同)
  • 对于 Bitbucket,可以使用以下 URL:https://api.bitbucket.org/2.0/repositories/{user}/{repo_name} 如果 Repository 不存在:它返回 `{'message' : Repository user/ repo_name not found}` 如果 Repository 存在,它返回 `Forbidden`。这并不完全是信息性的,但它有效。 (2认同)