我可以使用SSH URL检查git repo的存在吗?

Tre*_*key 2 git ssh command-line github

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

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

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

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

但这会产生很多无关的信息而不是简单的是/否.

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

Von*_*onC 6

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

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

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

如果存在repo,则会列出HEAD和远程分支,而无需克隆或下载任何其他内容.

另一种方法是简单地测试回购的https url是否存在(或者是404).
请参阅" 我可以使用wget检查,但不能下载 ":

wget -q --spider address https://github.com/username/reponame
Run Code Online (Sandbox Code Playgroud)

使用curl也是可能的:" 脚本获取URL列表的HTTP状态代码? ".