git - 使用 SSH 克隆 git repo

标签 git ssh

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

然后,正如我所看到的 here ,我试过这样克隆:

git clone ssh://my_username@my-repository.com:7999/my_project.git

git clone ssh://git@my-repository.com:7999/my_project.git

但是,这些选项不起作用:
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

我唯一的进步是尝试这个:
git clone my_username@my-repository.com:7999/my_project.git

这要求我输入密码 3 次,然后失败了。我怀疑这虽然不使用 SSH,
因为我认为 SSH 不应该要求输入密码。
Password:
Password:
Password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,keyboard-interactive).
fatal: Could not read from remote repository.

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

编辑

我在 Windows 上,我正在使用 Git Bash。
ssh-add -l

退货
The agent has no identities.

最佳答案

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

只需尝试使用 SSH 连接:

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

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

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

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

如果这有效,那么您可以将 SSH 配置设置为更方便。
~/.ssh/config你可以这样写:
Host ${FRIENDLY_NAME}
    User ${USERNAME}
    Hostname ${SERVER}
    Port ${PORT}
    IdentityFile ${MY_PRIVATE_KEY}

然后,您可以像这样测试连接:
ssh ${FRIENDLY_NAME}

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

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

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

关于git - 使用 SSH 克隆 git repo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42840033/

相关文章:

GIT-IntelliJ : Failed to start Git process

git - 在 github 中的 pull 请求之前,将所有提交压缩为一个

ssh - 通过SSH访问Neo4J Shell?

git - 在单个主机上使用多个 key 时,SSH 使用错误的 key

ssh - 使用 SSH 将文件从子文件夹复制到文件夹

linux - 远程 ssh 命令未退出

python - 为什么在响应 kex-neogitation 时,openssh 服务器会出现一些随机字节?

git - Azure DevOps '401 - Uh-oh, you do not have access' 受邀用户

ruby-on-rails - 如何解决heroku "You are trying to install in deployment mode after changing your Gemfile"错误?

git - 如何防止 merge 的更改显示为 'changes to be committed'