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

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

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


当前回答

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

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

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

其他回答

我可以创建一个SSH密钥并使用它来克隆存储库。命令将保持不变:

git clone git@github.com:DriveWealth/my-repo-name.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

对我来说,解决方案是:

git clone https://myusername@restoftherepolink.git

在这里,你需要是回购的所有者,但如果你不是,那么它将

git clone https://myusername@github.com/ownersusername/repo_name.git

如果你启用了2FA,那么:

从右上角的个人资料图标进入设置或访问https://github.com/settings/profile 转到底部选项卡或转到https://github.com/settings/tokens 打开最后一个标签这里个人令牌。并生成一个令牌 复制令牌并运行git clone https://myusername@restoftherepolink.git

当提示输入密码时,把标记放在这里。

如果你想在Dockerfile中实现它,下面几行可以帮助你。

ARG git_personal_token
RUN git config --global url."https://${git_personal_token}:@github.com/".insteadOf "https://github.com/"
RUN git clone https://github.com/your/project.git /project

然后我们可以使用下面的参数进行构建。

docker build --build-arg git_personal_token={your_token} .

我需要一种非交互式的方法来克隆私有回购。

受本期启发:https://github.com/github/hub/issues/1644

步骤1。

在github开发者设置中创建一个个人访问令牌: https://github.com/settings/tokens

步骤2。

git clone https://$token:x-oauth-basic@github.com/$username/$repo.git