Skip to main content
Skip to content

排查克隆错误

如果您在克隆存储库时遇到问题,请检查这些常见错误。

HTTPS 克隆错误

对 Git 使用 HTTPS 时有几种常见错误。 这些错误通常表示您有旧版 Git,或无法访问仓库。

下面是您可能收到的 HTTPS 错误示例:

> error: The requested URL returned error: 401 while accessing
> https://github.com/USER/REPO.git/info/refs?service=git-receive-pack
> fatal: HTTP request failed
> Error: The requested URL returned error: 403 while accessing
> https://github.com/USER/REPO.git/info/refs
> fatal: HTTP request failed
> Error: https://github.com/USER/REPO.git/info/refs not found: did you run git
> update-server-info on the server?

检查 Git 版本

无需与最低 Git 版本交互 GitHub,但我们发现版本 1.7.10 是许多平台上可用的舒适稳定版本。 你可以始终在 Git 网站上下载最新版本。

确保遥控器正确

要提取的存储库必须存在于该存储库上 GitHub.com。

可以打开命令行并键入 git remote -v 来查找本地存储库的 URL:

$ git remote -v
# View existing remotes
> origin  https://github.com/ghost/cocoareactive.git (fetch)
> origin  https://github.com/ghost/cocoareactive.git (push)

$ git remote set-url origin https://github.com/ghost/ReactiveCocoa.git
# Change the 'origin' remote's URL

$ git remote -v
# Verify new remote URL
> origin  https://github.com/ghost/ReactiveCocoa.git (fetch)
> origin  https://github.com/ghost/ReactiveCocoa.git (push)

或者,可以通过应用程序 GitHub Desktop 更改 URL。

提供访问令牌

若要访问 GitHub,您必须使用 personal access token 进行身份验证,而不能使用您的密码。 有关详细信息,请参阅“管理个人访问令牌”。

如果你要访问使用 SAML SSO 的组织,并且你正在使用 personal access token (classic),则必须先授权你的 personal access token 访问该组织,然后才能进行身份验证。 有关详细信息,请参阅 关于使用单一登录进行身份验证 和 授权个人访问令牌以与单点登录一起使用。

检查您的权限

提示输入用户名和密码时,确保使用可以访问仓库的帐户。

提示

如果不想在每次与远程存储库交互时都输入用户名和密码,可以打开凭据缓存。 如果已在使用凭据缓存,请确保您的计算机缓存了正确的凭据。 不正确或过期的凭据将导致身份验证失败。

改用 SSH

如果您以前设置了 SSH 密钥,便可使用 SSH 克隆 URL,而不使用 HTTPS。 有关详细信息,请参阅“关于远程仓库”。

错误:未找到仓库

如果在克隆存储库时看到此错误,则表示存储库不存在,或者您没有访问该存储库的权限。 根据原因,存在一些此错误的解决方案。

检查拼写

出现拼写错误。 如果尝试克隆 git@github.com:owner/repotile.git,但存储库已实际命名为 owner/repoti1e,则会收到此错误。

要避免此错误,克隆时,始终从仓库页面复制和粘贴克隆 URL。 有关详细信息,请参阅“克隆仓库”。

若要更新现有仓库上的远程存储,请参阅 管理远程仓库。

检查您的权限

如果您尝试克隆私有仓库,但没有查看仓库的权限,您将收到此错误。

确保您通过以下方式之一中,拥有仓库的访问权限:

  • 仓库所有者
  • 存储库上的协作者
  • 拥有存储库访问权限的团队成员(如果存储库属于组织)

检查 SSH 访问权限

在极少数情况下,您可能没有仓库的适当 SSH 访问权限。

你应确保所使用的 SSH 密钥已关联到你在 GitHub 上的个人账户。 可以通过在命令行中键入以下内容检查此项:

$ ssh -T git@github.com
> Hi USERNAME! You've successfully authenticated, but GitHub does not
> provide shell access.

如果存储库属于组织,并且你正在使用由某个 OAuth app组织生成的 SSH 密钥, OAuth app 则访问可能受到组织所有者的限制。 有关详细信息,请参阅“关于 OAuth 应用访问限制”。

有关详细信息,请参阅 向 GitHub 帐户添加新的 SSH 密钥。

检查仓库是否确实存在

如果所有其他操作都失败,请确保存储库确实存在于 GitHub.com! 如果您尝试推送不存在的仓库,您将收到此错误。

错误:远程 HEAD 引用不存在的 ref,无法检出

如果存储库的默认分支已被删除 GitHub.com,则会发生此错误。

检测此错误很简单;当您尝试克隆以下仓库时,Git 会警告您:

$ git clone https://github.com/USER/REPO.git
# Clone a repo
> Cloning into 'repo'...
> remote: Counting objects: 66179, done.
> remote: Compressing objects: 100% (15587/15587), done.
> remote: Total 66179 (delta 46985), reused 65596 (delta 46402)
> Receiving objects: 100% (66179/66179), 51.66 MiB | 667 KiB/s, done.
> Resolving deltas: 100% (46985/46985), done.
> warning: remote HEAD refers to nonexistent ref, unable to checkout.

若要修复此错误,需要是存储库 GitHub.com的管理员。 你将要更改存储库的默认分支。

之后,您可以从命令行获取所有可用分支的列表:

$ git branch -a
# Lists ALL the branches
>   remotes/origin/awesome
>   remotes/origin/more-work
>   remotes/origin/new-main

然后,您可以切换到新分支:

$ git checkout new-main
# Create and checkout a tracking branch
> Branch new-main set up to track remote branch new-main from origin.
> Switched to a new branch 'new-main'