【问题标题】:Can I check the existence of a git repo using an SSH URL?我可以使用 SSH URL 检查 git repo 的存在吗?
【发布时间】:2014-12-27 14:15:51
【问题描述】:

给定一个 SSH URL,例如 git@github.com:<user>/<project>.git,我如何测试 github 上是否存在特定的存储库(假设我还设置了正确的 SSH 密钥)?

使用git clone 是不可取的,因为如果它确实存在,它将立即继续下载它。我没有看到任何可以传递给git clone 的标志,它只会检查存储库的存在而无需下载。

我想我可以通过运行如下命令来解析 URL 并改用公共 API:

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

但这会返回很多无关的信息,而不是简单的是/否。

是否有更好的解决方案来简单地检查存储库是否存在?

【问题讨论】:

    标签: git github command-line ssh


    【解决方案1】:

    你可以简单地使用git ls-remote:

     git ls-remote git@github.com:username/reponame
    

    (也可以使用 https url,不需要设置任何 ssh 密钥)

    如果 repo 存在,它将列出 HEAD 和远程分支,无需克隆或下载任何其他内容。

    另一种方法是简单地测试 repo 的 https url 是否存在(或者是 404)。
    参见例如“Can I use wget to check , but not download”:

    wget -q --spider address https://github.com/username/reponame
    

    也可以使用curl:“script to get the HTTP status code of a list of urls?”。

    【讨论】:

    • 在不存在的仓库上使用git ls-remote 时,我会收到Username for ...Password for ... 的提示。只有在我按回车键通过它们后才会引发错误。有什么办法可以忽略吗?
    • @chepukha 奇怪:我看不到,使用 ssh 或 https:git ls-remote https://github.com/username/reponame 会立即给出:remote: Repository not found. fatal: repository 'https://github.com/username/reponame/' not found。您是否支持代理?
    • 这是我得到的:$ 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/'。不,我不支持任何代理。
    • @chepukha 很有趣。使其成为一个独立的问题(带有指向此问题的链接),供其他人尝试。请务必提及您的操作系统和 git 版本。
    猜你喜欢
    • 2014-04-22
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2020-04-06
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多