Git 如何使用 SSH 密钥、用户名进行克隆

Sus*_*vam 35 git clone

我有以下内容,我需要在 Windows 终端命令提示符或 linux 中克隆存储库。

  • 网址: git@xxxxx.com:xxx/xxx/git
  • 用户名: xxx@xxx.in
  • SSH 密钥: ssh-rsa AAAAB3NzaC1yxxxxxxxxxxxx....xxx@xxx.in

我试过:

git clone git@xxxxx.com:xxx/xxx/git
Run Code Online (Sandbox Code Playgroud)

我得到

Permission denied(public key)
Couldn't read from remote repository
Run Code Online (Sandbox Code Playgroud)

还尝试将 URL 更改为:

git clone https://xxxxx.com:xxx/xxx/git
Run Code Online (Sandbox Code Playgroud)

Wis*_*ell 89

总是迟到回答任何问题,您可能有多个 ssh 密钥,如果未指定,git 将尝试使用,id_rsa但如果您需要不同的密钥,您可以使用

git clone git@provider.com:userName/projectName.git --config core.sshCommand="ssh -i ~/location/to/private_ssh_key"
Run Code Online (Sandbox Code Playgroud)

这样,它将应用此配置并使用与id_rsa从 git 存储库实际获取任何数据之前不同的密钥。

随后fetchpush将使用指定的密钥对克隆的存储库进行身份验证。

希望这对任何人都有帮助。

  • 但它会导致递归子模块出现问题 (4认同)
  • 你摇滚。这是一个很好的解决方案。每个克隆都简单且可重复。 (2认同)
  • 好吧,子模块的问题与我最初试图解决的问题有点不同,但基本上你需要覆盖每个子模块内的 `.git/config` 文件并将 sshCommand 更正为任何子模块需要。您可以在这里阅读文档获得更好的解释 https://git-scm.com/docs/git-config#_configuration_file (2认同)

Ala*_*ALI 31

我建议您按照以下步骤操作:

步骤 1:检查现有的 SSH 密钥

$> ls -al ~/.ssh

你看到任何名为id_rsa和的文件id_rsa.pub吗?

如果是,请转到步骤 3

如果没有,您需要生成它们

第 2 步:生成新的 SSH 密钥

$> ssh-keygen -t rsa -b 4096 -C "yourEmail"

将您的 SSH 密钥添加到 ssh-agent

$> eval "$(ssh-agent -s)"

$> ssh-add ~/.ssh/id_rsa

步骤 3.1:将 SSH 密钥添加到您的 GIT 帐户。

获取您的公钥

$> cat ~/.ssh/id_rsa.pub

转到您的 GIT 项目 -> 设置 -> SSH 密钥

然后在 SSH 密钥中传递您的公钥内容

步骤 3.2:强制 SSH 客户端使用给定的私钥

当您无法在 Git 帐户上设置密钥时,这是一种替代解决方案

$> sudo nano ~/.ssh/config

然后改变这一行

IdentityFile <yourPrivateKey>

第 4 步:克隆项目

$> git clone git@xxxxx.com:xxx/xxx/git


Dai*_*jan 8

使用 GitHub 测试 ssh 以获得可操作的反馈。

在命令行中运行ssh -T git@github.com
(参见https://help.github.com/articles/testing-your-ssh-connection/


如果可行,接下来请使用 git。但可能会有,是你的问题。

克隆,然后使用 git clone git@github.com:$USERNAME/$REPONAME.git

  • 并没有真正解决所提出的问题。 (2认同)