简体   繁体   English

Git clone 工作,但 git pull 不使用 SSH

[英]Git clone works but git pull doesn't using SSH

We use SSH keys on jenkins to clone from our remote Git.我们在 jenkins 上使用 SSH 密钥从远程 Git 克隆。 It works fine.它工作正常。 We use the git plugin, use credentials for SSH and we are able to clone the repo.我们使用 git 插件,使用 SSH 凭据,我们能够克隆 repo。

But when we execute the following command in a later stage it fails:但是当我们在稍后阶段执行以下命令时,它会失败:

git pull origin master

It complains about:它抱怨:

Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

It seems like this command isn't using our SSH credentials anymore.这个命令似乎不再使用我们的 SSH 凭据了。 How can we use the git CLI to keep our SSH credentials?我们如何使用 git CLI 来保存我们的 SSH 凭证?

I've seen the answer here but it seems another case then what we are facing.我在这里看到了答案,但似乎是我们所面临的另一种情况。

+ +

 cat .git/config
[core]
    repositoryformatversion = 0
    filemode = true
    logallrefupdates = true
[remote "origin"]
    url = ssh://git@xxx/test-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

First check your existing SSH keys:首先检查您现有的 SSH 密钥:

  1. Open Git Bash.打开 Git Bash。

  2. Enter ls -al ~/.ssh to see if existing SSH keys are present:输入ls -al ~/.ssh以查看是否存在现有的 SSH 密钥:

     $ ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist
  3. Check the directory listing to see if you already have a public SSH key.检查目录列表以查看您是否已经拥有公共 SSH 密钥。

If there is an existing SSH key test your SSH connection:如果有现有的 SSH 密钥,请测试您的 SSH 连接:

  1. Open Git Bash.打开 Git Bash。

  2. Enter the following:输入以下内容:

     $ ssh -T git@github.com # Attempts to ssh to GitHub

You may see one of these warnings:您可能会看到以下警告之一:

    The authenticity of host 'github.com (IP ADDRESS)' can't be established.
    RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
    Are you sure you want to continue connecting (yes/no)?

    The authenticity of host 'github.com (IP ADDRESS)' can't be established.
    RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
    Are you sure you want to continue connecting (yes/no)?
  1. Verify that the fingerprint in the message you see matches one of the messages in step 2, then type yes:验证您看到的消息中的指纹是否与步骤 2 中的消息之一匹配,然后键入 yes:

     Hi username! You've successfully authenticated, but GitHub does not provide shell access.
  2. Verify that the resulting message contains your username.验证生成的消息是否包含您的用户名。 If you receive a "permission denied" message, see "Error: Permission denied (publickey)".如果您收到“权限被拒绝”消息,请参阅“错误:权限被拒绝(公钥)”。

If the above process doesn't work:如果上述过程不起作用:

generate SSH key to your git repository steps:生成 SSH 密钥到您的 git 存储库步骤:

  1. Open Git Bash.打开 Git Bash。

  2. Paste the text below, substituting in your GitHub email address.粘贴下面的文本,替换为您的 GitHub 电子邮件地址。

     $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This creates a new ssh key, using the provided email as a label.这将使用提供的电子邮件作为标签创建一个新的 ssh 密钥。

    Generating public/private rsa key pair.
  1. When you're prompted to "Enter a file in which to save the key," press Enter.当系统提示您“输入要在其中保存密钥的文件”时,按 Enter。 This accepts the default file location.这接受默认文件位置。

     Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
  2. At the prompt, type a secure passphrase.在提示符处,键入安全密码。 For more information, see "Working with SSH key passphrases".有关更多信息,请参阅“使用 SSH 密钥密码”。

     Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]

Adding a new SSH key to your git repository visit here ,将新的 SSH 密钥添加到您的 git 存储库访问此处

https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/ https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

Ensure that you have specified absolute path of the keyfile in your ~/.ssh/config file.确保您在~/.ssh/config文件中指定了密钥文件的绝对路径。

If you have used relative path in the ssh config, it may work while cloning depending on your present working directory but all git commands will fail once you cd into the repo folders.如果您在 ssh 配置中使用了相对路径,它可能会在克隆时工作,具体取决于您当前的工作目录,但是一旦您 cd 进入 repo 文件夹,所有 git 命令都将失败。

I'm probably posting this for my future self.我可能会为我未来的自己发布这个。

In addition to having the public key uploaded to Github's servers, as Priyanka Lalge shows us in her answer , our own system must be configured properly.除了将公钥上传到 Github 的服务器之外,正如Priyanka Lalge 在她的回答中向我们展示的那样,我们自己的系统必须正确配置。 The Achilles' heel seems to be the username.阿喀琉斯之踵似乎是用户名。 Github, and possibly Gitlab and others, require the SSH login name to be git , not your Github (or Gitlab) user name! Github,可能还有 Gitlab 和其他人,要求 SSH 登录名是git而不是你的 Github(或 Gitlab)用户名!

This is a properly-configured ~/.ssh/config file for example:这是一个正确配置的~/.ssh/config文件,例如:

Host github
    User git
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa.github
    IdentitiesOnly yes

Notice the User git and the use of IdentitiesOnly to ensure that only the single specified IdentityFile will be tried.注意用户git和使用 IdentitiesOnly 以确保仅尝试单个指定的 IdentityFile。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM