使用 SSH 克隆一个 git repo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42840033/
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:40:39  来源:igfitidea点击:

Cloning a git repo using SSH

gitssh

提问by octavian

I am trying to clone a git repository using SSH. Therefore, I created an ssh key pair on my local machine and I added the public key on my git repo(which is Bitbucket server).

我正在尝试使用 SSH 克隆一个 git 存储库。因此,我在本地机器上创建了一个 ssh 密钥对,并在我的 git repo(这是 Bitbucket 服务器)上添加了公钥。

Then, as I saw here, I tried cloning like this:

然后,正如我在这里看到的,我尝试像这样克隆:

git clone ssh://[email protected]:7999/my_project.git

git clone ssh://[email protected]:7999/my_project.git

These options didn't work however:

但是,这些选项不起作用:

Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

The only bit of progress I has was by trying this:

我唯一的进步是尝试这个:

git clone [email protected]:7999/my_project.git

This asked for my password 3 times and then failed. I suspect that this doesn't use SSH though, because I think SSH should not ask for a password.

这要求我输入密码 3 次,然后失败了。我怀疑这并不使用 SSH,因为我认为 SSH 不应该要求输入密码。

Password:
Password:
Password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,keyboard-interactive).
fatal: Could not read from remote repository.

So how can I clone a git repo using SSH?

那么如何使用 SSH 克隆 git 存储库呢?

EDIT

编辑

I'm on Windows and I'm using Git Bash.

我在 Windows 上,我正在使用 Git Bash。

ssh-add -l

Returns

退货

The agent has no identities.

回答by darvark

Are you sure that ssh on this host works on port 7990? If this repo is on bitbucket, then I'm almost sure that you cane use default port for ssh

你确定这台主机上的 ssh 在端口 7990 上工作吗?如果此 repo 位于 bitbucket 上,那么我几乎可以肯定您可以使用默认端口进行 ssh

git clone [email protected]/my_project.git

just like in git with bitbucket

就像在带有 bitbucket 的 git 中一样

回答by Attie

Most servers will expect users to authenticate with the 'git' username, and their own private key which is used for identification purposes.

大多数服务器都希望用户使用“git”用户名和他们自己的用于识别目的的私钥进行身份验证。

Just try connecting with SSH:

只需尝试使用 SSH 连接:

ssh ${USERNAME}@${SERVER} -p ${PORT}

This will establish if you're able to even authenticate against the server, removing git from the equation.

这将确定您是否能够对服务器进行身份验证,从等式中删除 git。

Where is your private key? If it isn't ~/.ssh/id_rsa(or a configured path), then you'll need to provide it's path. Try this:

你的私钥在哪里?如果不是~/.ssh/id_rsa(或配置的路径),则您需要提供它的路径。尝试这个:

ssh ${USERNAME}@${SERVER} -p ${PORT} -i ${MY_PRIVATE_KEY}

If this works, then you can setup your SSH configuration to be more conveniant. In ~/.ssh/configyou can put something like this:

如果这有效,那么您可以将 SSH 配置设置为更方便。在~/.ssh/config你可以把这样的事情:

Host ${FRIENDLY_NAME}
    User ${USERNAME}
    Hostname ${SERVER}
    Port ${PORT}
    IdentityFile ${MY_PRIVATE_KEY}

You can then test the connection like so:

然后,您可以像这样测试连接:

ssh ${FRIENDLY_NAME}

And finally:

最后:

git clone ssh://${FRIENDLY_NAME}/${REPO_PATH}

You'll only need to specify the port if it isn't the default (22).

如果端口不是默认值 (22),您只需指定端口。

NOTE: I've used tilde (~) because you hadn't specified Windows when I wrote my answer. You can find out your 'home' directory on Windows by pressing Win+R, entering a dot ., and pressing return. This will typically take you to C:\Users\%USERNAME%

注意:我使用波浪号 ( ~) 是因为在我写答案时您没有指定 Windows。您可以通过按 Win+R,输入一个点.,然后按回车键在 Windows 上找到您的“主”目录。这通常会带您到C:\Users\%USERNAME%