克隆私人Github回购

Sol*_*mon 198 git private github

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

我为笔记本电脑的Github帐户添加了一个新的SSH密钥,并成功克隆并更改了我设置的公共测试仓库.但是,我无法克隆私人仓库.在命令行中我需要做些什么特别的事情才能克隆私人仓库?我是否需要为笔记本电脑设置一个新的GitHub帐户并将自己设置为合作者?

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

小智 374

Apr, 2022 Update:

You can clone a private repository from your account and you can also clone a private repository from organization if you're its owner or member.

*pat is PAT(Personal Access Token).

git clone https://<pat>@github.com/<your account or organization>/<repo>.git
Run Code Online (Sandbox Code Playgroud)

To clone a private repository from your account or organization, you need to generate a PAT(Personal Access Token) on your Github account, and add it to the command above. *Organization doesn't have PAT generator.

This is how you generate a PAT on your Github account:

1、进入“设置”

在此输入图像描述

2、向下滚动然后转到“开发者设置”

在此输入图像描述

3、“个人访问令牌”,然后按“生成新令牌”

在此输入图像描述

4、填写“Note”,然后勾选“repo”

*如果未选中“repo” ,则无法克隆私有存储库。

在此输入图像描述

5,向下滚动然后按“生成令牌”

在此输入图像描述

6、最后生成PAT :

在此输入图像描述

这是带有上面生成的 PAT 的命令:

git clone https://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@github.com/<your account or organization>/<repo>.git
Run Code Online (Sandbox Code Playgroud)

  • 尝试过此操作,但它仍然要求输入密码,而我的密码失败。如果我公开它,它就会起作用。问题是关于 github 中的私有存储库,所以我认为这缺少信息。 (4认同)

小智 152

私有克隆URL采用的形式git@github.com:username/repo.git- 也许您需要使用git@而不是git://

git:// URL是只读的,看起来私有存储库不允许这种形式的访问.


cha*_*had 132

这对我有用:

git clone https://username@github.com/username/repo_name

当然,这是在将我的SSH密钥添加到Github之后.我在CentOs服务器上使用它,如果有必要的话.

  • 谢谢你.但我不知道为什么会这样.从未提示输入密码.应该通过https git克隆以任何方式触摸我的ssh键吗?我以为不是. (4认同)
  • @diimdeep Git 和其他工具,经常使用 git: 协议来访问远程存储库中的文件。某些防火墙配置会阻止 git:// URL,这会导致在尝试克隆存储库或下载依赖项时出错。(例如,公司防火墙因阻止 git 而“臭名昭著”:。)如果遇到此问题,您可以强制使用 https: 代替,通过运行以下命令:git config --global url."https:// ".而不是 git:// (2认同)
  • 在某些情况下,如果系统提示您输入密码然后拒绝访问,可能是因为您需要提供“个人访问令牌”而不是您的 GitHub 登录密码。[1] https://help.github.com/articles/which-remote-url-should-i-use/ [2] https://help.github.com/articles/creating-a-personal-access-命令行令牌/ (2认同)
  • 答案是错误的,SSH密钥与HTTP访问无关。 (2认同)

izi*_*k f 56

这对我有用:

git clone https://username:password@github.com/username/repo_name.git
Run Code Online (Sandbox Code Playgroud)

  • 使用密码进行身份验证将于 2021 年 8 月 13 日弃用,此后将不再有效。https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ (4认同)
  • 这是特殊字符密码的问题,在日志中留下您的密码,似乎不再工作 (3认同)
  • 尝试此操作-系统将提示您输入您的帐户密码。不用担心密码泄漏到历史记录或日志中。git clone https:// username @ github.com / username / repo_name.git` (2认同)
  • 如果您使用个人访问令牌,这仍然有效,并且可以轻松编写脚本来提示输入令牌(“read -s”)。 (2认同)

med*_*zid 49

2023 年和 2024 年更好的解决方案

使用个人访问令牌是可行的,但是您可能更愿意将令牌访问范围缩小到仅您想要克隆的存储库

Github 推出细粒度个人访问令牌,它可以做到这一点。

以下是要遵循的步骤:

  1. 从右上角的菜单中选择“设置”

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

  1. 之后,在同一页面中,您需要指定存储库权限。如果您只想克隆私有存储库,请将“内容”设置为“访问:只读”

在此输入图像描述

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

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

git clone https://oauth2:<YOUR-TOKEN-HERE>@github.com/<your_user>/<your_repo>.git
Run Code Online (Sandbox Code Playgroud)

就是这样。


lzl*_*31x 46

看在上帝的份上,我已经多次遇到过这个问题,每次登陆这个页面,都会尝试一切并失败!

这是因为我启用了2FA!

根据https://help.github.com/articles/which-remote-url-should-i-use/#when-2fa-is-enabled

如果您启用  了双因素身份验证,或者您正在访问使用SAML单点登录的组织,则  必须  提供个人访问令牌,  而不是输入HTTPS Git的密码.

  1. 请点击此链接并创建访问令牌
  2. git clone https://github.com/username/repo_name.git (默认的git repo链接足够好!)
  3. 输入您的用户名并使用访问令牌作为密码!

更新:

如果您不介意在命令行中公开访问令牌,您还可以将访问令牌粘贴为用户名,然后按Enter键输入(不需要密码).

  • 谢谢你,另一种方法是设置 url 格式,如下所示:`https://username:access_token@github.com/username/repo_name.git` 如果我将公共存储库更改为私有,这对我很有帮助。我需要做的就是用该格式更改远程源。 (2认同)

Luh*_*uhn 34

解决方案:2022 年 2 月

  1. 转到settings > developer settings > personal access token链接: https: //github.com/settings/tokens
  2. 点击generate new token
  3. 选择范围(启用所有范围以实现完全访问)

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

在此输入图像描述

现在使用以下命令:

git clone https://user:TOKEN@github.com/username/repo.git
Run Code Online (Sandbox Code Playgroud)

2021 年更新(已过时)

已过时:尝试以下命令:

git clone https://user:pass@github.com:username/repo.git
Run Code Online (Sandbox Code Playgroud)

将解决以下错误:

Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.

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

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



2021 年解决方案(已过时)

单击您的图标配置文件并转到(使用上面的链接获取更多指导):

settings> developer settings> personal acces token > generate new token

  • 这显然已经略有改变。`git clone https://user:TOKEN@github.com/username/repo.git` 为我工作 (5认同)

小智 18

使用Git for Windows可以更轻松地使用HTTPS网址.

然后打开一个git shell git clone https://github.com/user/repo.出现提示时输入用户名和密码.无需设置SSH密钥.


Bla*_*mba 14

对我来说,解决方案是:

git clone https://myusername@restoftherepolink.git
Run Code Online (Sandbox Code Playgroud)

在这里,您需要成为存储库的所有者,但如果不是,则它将作为

git clone https://myusername@github.com/ownersusername/repo_name.git
Run Code Online (Sandbox Code Playgroud)

如果有的2FA enabled话:

  1. 从右上角的配置文件图标转到设置,或访问https://github.com/settings/profile
  2. 转到底部选项卡或转到https://github.com/settings/tokens
  3. 在此处打开最后一个标签。并生成令牌
  4. 复制令牌并运行 git clone https://myusername@restoftherepolink.git

当提示您输入密码时,请将该令牌放在此处。 令牌生成图像在这里


Ama*_*Neo 12

每个人都知道克隆的过程,我想在这里添加更多的东西。不要担心特殊字符或将“@”写成“%40”,请参阅字符编码

$ git clone https://username:password@github.com/user/repo   
Run Code Online (Sandbox Code Playgroud)

这条线可以胜任

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

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

创建个人访问令牌

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

$ git clone https://username:token@github.com/user/repo
Run Code Online (Sandbox Code Playgroud)

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

确保您已检查repo 完全控制私有存储库 这是短片


Rtm*_*tmY 11

2021 年 8 月 13 日更新

这里

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

今天你需要做什么

  • 对于开发人员来说,如果您今天使用密码来验证 GitHub.com 上的 Git 操作,则必须在 2021 年 8 月 13 日之前开始通过 HTTPS(推荐)或 SSH 密钥使用个人访问令牌,以避免中断。如果您收到警告,指出您正在使用过时的第三方集成,则应将客户端更新到最新版本。

  • 对于集成商,您必须在 2021 年 8 月 13 日之前使用 Web 或设备授权流程对集成进行身份验证,以避免中断。有关更多信息,请参阅授权 OAuth 应用程序开发者博客上的公告


在命令行上使用令牌

1)创建个人访问令牌 - follow操作。

2 ) 获得令牌后,您可以在通过 HTTPS 执行 Git 操作时输入令牌而不是密码。

例如,在命令行上您可以输入以下内容:

$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token # <----- Bingo!
Run Code Online (Sandbox Code Playgroud)

(*)在此查看更多内容。

  • `git clone https://[USER]:[TOKEN]@github.com/username/repo.git` 也有效! (2认同)

cna*_*ash 9

我需要一种非交互式方法来克隆私有存储库。

受此问题启发: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
Run Code Online (Sandbox Code Playgroud)


one*_*rld 6

我有一个公司(私人)帐户并启用了2-Factor-Authentication,所以我不得不合并一些帖子才能使其正常工作,如下所示。(记下来,这样对情况相同的人可能有用)

Initially it was the Fatal Error. 
fatal: repository 'https:
...
remote: Repository not found.
fatal: repository 'https:
Run Code Online (Sandbox Code Playgroud)

如此处所述安装凭证管理器并更新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://
Run Code Online (Sandbox Code Playgroud)

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

fatal: Authentication failed for 'https://<username>:<password-missing-$-symbol>@github.com/<ORG_NAME>/<PROJECT-NAME>.git'
Run Code Online (Sandbox Code Playgroud)

我不得不引用此站点:https : //git-scm.com/book/en/v2/Git-Tools-Credential-Storage

请注意:如果在Windows Credential Manager中为您的帐户存储了不正确的密码, 它也会加剧该问题。在移至以下步骤之前,我删除了以这种方式存储的github凭证。

$ git config --global credential.helper cache
$ git clone https://<username>:<password>@github.com/<ORG_NAME>/<PROJECT-NAME>.git
Run Code Online (Sandbox Code Playgroud)

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


小智 6

从启用 2FA 的私有存储库克隆时,您需要遵循一个简单的步骤

  1. 转到您的 Git 帐户
  2. 转到设置-> 开发者设置-> 个人访问令牌
  3. 单击生成新令牌
  4. 创建一个带有您想要的标题和功能的令牌
  5. 克隆私有仓库时,使用 git clone repoName,输入用户名后,提供个人访问令牌作为密码。

当您收到 Private repo 的 Authentication failed 错误消息时,请按照相同的步骤操作


MK *_*ung 5

为了响应Mac的答案,你可以得到你的SSH克隆网址您的GitHub库页上,单击SSHYou can clone with HTTPS, SSH, or Subversion.,然后复制URL.


小智 5

这对我在 mac 上有用 git clone https://username@github.com:username/repo_name