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

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

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


当前回答

如果您确定您没有启用2FA,您有权限访问回购,并且回购存在,那么您的git@github.com可能是用另一个帐户登录的。

检查一下你能做什么

ssh -T git@github.com

如果显示另一个帐户,要解决此问题:

 ssh-add -D
 ssh-add ~/.ssh/your_rsa
 ssh -T git@github.com
 git clone git@github.com:<owner_name>/<repo_name>.git

其他回答

私有克隆url的形式为git@github.com:username/repo.git -也许你需要使用git@而不是git://?

url是只读的,看起来私有回购不允许这种形式的访问。

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!

(*)在这里看到更多。

正如每个人都知道克隆的过程,我想在这里补充一些东西。 不要担心特殊字符或将“@”写成“%40”,参见字符编码

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

这条线就可以了

假设我有一个包含特殊字符的密码(我不知道在我的密码中替换什么'@') 如果我想使用我原来的密码以外的其他临时密码怎么办

为了解决这个问题,我鼓励使用GitHub开发者选项来生成访问令牌。 我相信访问令牌是安全的,你不会发现任何特殊字符。

creating-a-personal-access-token

现在我将编写下面的代码来访问我的存储库。

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

我只是用Access-token替换了我原来的密码, 现在我不担心有人看到我的访问凭证,我可以在任何时候重新生成令牌。

确保您已勾选“私有存储库的完全控制”

在github中添加桌面ssh公钥。

https://github.com/settings/keys

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

git clone https://myusername:mygithubpassword@github.com/myusername/project.git

当你试图从回购中使用私有回购时,你需要传递用户名和密码。