给定一个 SSH URL,例如 git@github.com:<user>/<project>.git
,我如何测试 github 上是否存在特定存储库(假设我也设置了正确的 SSH key )?
使用 git clone
是不可取的,因为如果它确实存在,它将立即继续下载它。我没有看到任何可以传递给 git clone
的标志,这将简单地检查存储库是否存在而无需下载。
我想我可以解析 URL 并改为使用公共(public) API,方法是运行如下命令:
curl "https://api.github.com/repos/<user>/<project>"
但这会返回很多无关信息,而不是简单的是/否。
是否有更好的解决方案来简单地检查存储库是否存在?
最佳答案
您可以简单地使用 git ls-remote
:
git ls-remote git@github.com:username/reponame
(也适用于 https url,不需要设置任何 ssh key )
如果存储库存在,它将列出 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 - 我可以使用 SSH URL 检查 git 存储库是否存在吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27668072/