我在Github上有一个私人存储库,用于我正在进行的一个项目。到目前为止,我只在家里的台式机上工作,但我刚刚买了一台笔记本电脑,我正在尝试设置它,以便我可以在任何一台电脑上工作,并进行推/拉更改。

我在我的Github账户上为笔记本电脑添加了一个新的SSH密钥,并且成功地克隆并更改了我设置的公共测试回购。但是,我不能克隆私人回购。为了克隆私有回购,我需要在命令行中做什么特别的事情吗?我是否需要为我的笔记本电脑设置一个新的GitHub帐户,并将自己设置为合作者?

我使用的命令是git clone git://github.com/username/reponame.git


当前回答

解决方案:2022年2月

进入>开发者设置>个人访问令牌链接:https://github.com/settings/tokens 单击生成新令牌 选择作用域(启用全部以完全访问)

这将生成一个令牌,看起来像这样:

现在使用以下命令:

git clone https://user:TOKEN@github.com/username/repo.git


更新2021年(过时)

正在执行以下命令:

git clone https://user:pass@github.com:username/repo.git

将在以下错误中解决:

密码认证支持已于2021年8月13日移除。请改用个人访问令牌。

并将您链接到以下支持页面:

https://docs.github.com/ en /认证/ keeping-your-account-and-data-secure / creating-a-personal-access-token



方案2021(过时)

点击你的图标配置文件,然后转到(使用上面的链接获取更多指导):

设置>开发人员设置>个人访问令牌>生成新的令牌

其他回答

当从启用2FA的私有回购中克隆时,您需要遵循简单的步骤

转到你的Git帐户 进入设置->开发人员设置->个人访问令牌 单击Generate new token 创建一个带有您想要的标题和功能的令牌 当您克隆私有回购时,通过使用git clone repoName,在输入您的用户名后,提供个人访问令牌作为密码。

当您获得私有回购的身份验证失败错误消息时,请执行相同的步骤

如果新使用的计算机有不同的凭据,则运行此命令

git 克隆 https://github.com/username/reponame.git

直接不行。Git将尝试使用存储的凭据,而不会提示您输入用户名和密码。由于凭证不匹配,git将输出未找到Repository,克隆操作失败。我解决这个问题的方法是删除旧的凭据,因为我不再使用它们了,然后再次运行上面提到的命令,输入所需的用户名和密码并克隆私有回购。

2021年8月13日更新

从这里开始:

2020年7月,我们宣布了要求使用 基于令牌的身份验证(例如,个人访问、OAuth或 GitHub应用程序安装令牌)用于所有经过身份验证的Git操作。 从2021年8月13日开始,我们将不再接受账户密码 在GitHub.com上验证Git操作时。

你今天需要做的事情

For developers, if you are using a password to authenticate Git operations with GitHub.com today, you must begin using a personal access token over HTTPS (recommended) or SSH key by August 13, 2021, to avoid disruption. If you receive a warning that you are using an outdated third-party integration, you should update your client to the latest version. For integrators, you must authenticate integrations using the web or device authorization flows by August 13, 2021, to avoid disruption. For more information, see Authorizing OAuth Apps and the announcement on the developer blog.


在命令行上使用令牌

1)创建个人访问令牌-遵循此步骤。

2)一旦你有了一个令牌,你可以在通过HTTPS执行Git操作时输入它而不是密码。

例如,在命令行中输入以下内容:

$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token # <----- Bingo!

(*)在这里看到更多。

在github中添加桌面ssh公钥。

https://github.com/settings/keys

您可以克隆的回购没有任何密码。

2023年更好的解决方案

使用个人访问令牌可以工作,但是您可能更喜欢将令牌访问范围缩小到您想要克隆的存储库

Github引入了细粒度的个人访问令牌,它可以做到这一点。

以下是需要遵循的步骤:

在右上角菜单中选择“设置”

在接下来的页面中,从左侧选择“开发人员设置”

然后在下一页的“个人访问令牌”下选择“细粒度令牌”

然后点击“生成新令牌”按钮

在下一页中,您将被要求为您的令牌提供一个名称,您也可以选择Repository访问。如果您只想指定一个存储库,那么请选择“仅选择存储库”,然后从下拉菜单中选择您的存储库

之后,在同一页面中,您需要指定Repository权限。如果你只是想克隆一个私有回购,将“contents”设置为“Access: read-only”

最后点击“生成令牌”按钮

获得令牌后,复制它并运行命令

git clone https://oauth2:<YOUR-TOKEN-HERE>@github.com/<your_user>/<your_repo>.git

就是这样。