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

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

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


当前回答

我使用Android Studio从GitHub私有存储库和双因素身份验证(2FA)克隆项目。我在lzl124631x的答案中创建了一个个人令牌。

然后我用这样的url克隆了repo: https://YourGitHubUsername:YourGeneratedPersonalToken@github.com/YourRepoPath.git

其他回答

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!

(*)在这里看到更多。

我有一个公司(私人)帐户,启用了2因素身份验证,所以我必须结合一些帖子,使其工作如下。(记下来,这样可能对有同样情况的人有用)

Initially it was the Fatal Error. 
fatal: repository 'https:
...
remote: Repository not found.
fatal: repository 'https:

安装凭据管理器并更新GIT,如此处所述:https://codeshare.co.uk/blog/how-to-solve-the-github-error-fatal-httprequestexception-encountered/

这并没有解决问题,因为当我尝试使用clone命令时,问题移动到下面:

$ git clone https://<username>:<password>@github.com/<ORG_NAME>/<PROJECT-NAME>.git

remote: Invalid username or password.
fatal: Authentication failed for 'https://

我的密码中有$符号,出于某种原因GIT命令行/ GIT Bash不喜欢它。我可以看到,在错误返回的文本中没有$符号。

fatal: Authentication failed for 'https://<username>:<password-missing-$-symbol>@github.com/<ORG_NAME>/<PROJECT-NAME>.git'

我不得不参考这个网站:https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage

请注意:如果您的帐户存储了错误的密码 Windows凭证管理器也会增加问题。我 移除了以这种方式存储的github凭据,然后移动到下面的步骤。

$ git config --global credential.helper cache
$ git clone https://<username>:<password>@github.com/<ORG_NAME>/<PROJECT-NAME>.git

现在弹出Windows凭据管理器。我在文本框中输入了我的用户名和密码(这接受了带有$符号的密码),并提示我输入2-Factor身份验证代码。我从谷歌Authenticator中输入身份验证代码,克隆就完美地启动了。

这招对我很管用:

Git克隆https://username@github.com/username/repo_name

我认为值得一提的是,如果由于某些原因不能使用SSH协议,并且修改私有存储库http(s) URL以提供基本的身份验证凭据也不是一个选项,那么还有一个替代方案。

基本身份验证头可以使用http配置。extraHeader git-config选项:

git config --global --unset-all "http.https://github.com/.extraheader"
git config --global --add "http.https://github.com/.extraheader" \
          "AUTHORIZATION: Basic $(base64 <<< [access-token-string]:x-oauth-basic)"

其中[access-token-string]占位符应该被替换(包括方括号)为生成的真实令牌值。您可以在这里和这里阅读更多关于访问令牌的信息。

如果配置已正确应用,则配置的AUTHORIZATION头将包含在git命令访问的github.com IP地址的每个HTTPS请求中。

解决方案: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(过时)

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

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