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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41714882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:33:23  来源:igfitidea点击:

Git how to clone with SSH key, username

gitclone

提问by Sushivam

I have the following and i need to clone the repository in either windows terminal command prompt or linux.

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

URL:[email protected]:xxx/xxx/git

网址:[email protected]:xxx/xxx/git

username:[email protected]

用户名:[email protected]

SSH key:ssh-rsa [email protected]

SSH 密钥:ssh-rsa [email protected]

I tried as :

我试过:

git clone [email protected]:xxx/xxx/git

git 克隆 [email protected]:xxx/xxx/git

I get Permission denied(public key) Couldnt read from remote repository

我得到权限被拒绝(公钥)无法从远程存储库读取

Also tried to change the URL as:

还尝试将 URL 更改为:

git clone https://xxxxx.com:xxx/xxx/git

git 克隆https://xxxxx.com:xxx/xxx/git

回答by Ala Eddine JEBALI

I suggest that you follow those steps:

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

Step 1: Check for existing SSH keys

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

$> ls -al ~/.ssh

$> ls -al ~/.ssh

Do you see any files named id_rsaand id_rsa.pub?

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

If yes go to Step 3

如果是,请转到步骤 3

If no, you need to generate them

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

Step 2: Generate a new SSH key

第 2 步:生成新的 SSH 密钥

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

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

Add your SSH key to the ssh-agent

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

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

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

$> ssh-add ~/.ssh/id_rsa

$> ssh-add ~/.ssh/id_rsa

Step 3.1: Add the SSH key to your GIT account.

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

Get your public key

获取您的公钥

$> cat ~/.ssh/id_rsa.pub

$> cat ~/.ssh/id_rsa.pub

Go to your GIT project -> Settings -> SSH keys

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

Then past the content of your public key in SSH keys

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

Step 3.2: Force SSH Client To Use Given Private Key

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

This is an alternative solution when you can't set keys on your Git account

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

$> sudo nano ~/.ssh/config

$> sudo nano ~/.ssh/config

Then change this line

然后改变这一行

IdentityFile <yourPrivateKey>

IdentityFile <yourPrivateKey>

Step 4: Clone the project

第 4 步:克隆项目

$> git clone [email protected]:xxx/xxx/git

$> git clone [email protected]:xxx/xxx/git

回答by Daij-Djan

test ssh with GitHub to get actionable feedback.

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

in the command line run ssh -T [email protected]
(see https://help.github.com/articles/testing-your-ssh-connection/)

在命令行中运行ssh -T [email protected]
(参见https://help.github.com/articles/testing-your-ssh-connection/



if that works, bother with git next. but likely there will, be your issue.

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

to clone, THEN use git clone [email protected]:$USERNAME/$REPONAME.git

克隆,然后使用 git clone [email protected]:$USERNAME/$REPONAME.git

回答by Wiston Coronell

Always coming late to answer anything, it may be possible that you have more than one ssh keys and if not specified git will try to use id_rsabut if you need a different one you could use

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

git clone [email protected]:userName/projectName.git --config core.sshCommand="ssh -i ~/location/to/private_ssh_key"

This way it will apply this config and use a key different than id_rsabefore actually fetching any data from the git repository.

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

subsequent fetchor pushwill use the specified key to authenticate for the cloned repository.

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

Hope this is helpful to anyone.

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

回答by Thien Pham

To clone source code with Git using ssh, we can use http url with following format:

要使用 ssh 使用 Git 克隆源代码,我们可以使用以下格式的 http url:

http://usernameWITHOUTdomain:password@the_rest_httpURL

http://usernameWITHOUTdomain:password@the_rest_httpURL

ex: http://myname:[email protected]/projectname/project.git

例如:http://myname:[email protected]/projectname/project.git