访问Git私有回购应该自动失败而不要求信用

时间:2018-01-09 07:56:42

标签: git

我正在尝试自动化从各种git repos 列出分支的过程。

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

如果是公共回购,其工作正常。但如果它是一个私人回购,那么就要求信用。我需要避免这种情况。当 git要求输入信用时,它应自动退出。我怎样才能做到这一点? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

在这种情况下,您需要提供用于进行身份验证的用户名和密码。为避免被提示,您可以通过在URL中提供它们来完成此操作。

如果服务器提示Git输入用户名和密码(在本例中是通过HTTP 401),那么它会提供用户名和密码。通常,它会提示您输入用户名和密码:

% git ls-remote https://github.com/ethomson/secret
Username for 'https://github.com':

但是,如果您提供的凭据不正确,则Git不会提示您。相反,它会尝试使用这些凭证 - 当然 - 失败。

您可以通过在主机名之前添加username:password@ 来指定网址中的凭据:

% git ls-remote https://nobody:nopass@github.com/ethomson/secret
remote: Invalid username or password.
fatal: Authentication failed for 'https://nobody:nopass@github.com/ethomson/secret/'