简体   繁体   中英

Git clone works but git pull doesn't using SSH

We use SSH keys on jenkins to clone from our remote Git. It works fine. We use the git plugin, use credentials for SSH and we are able to clone the 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. How can we use the git CLI to keep our SSH credentials?

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:

  1. Open Git Bash.

  2. Enter ls -al ~/.ssh to see if existing SSH keys are present:

     $ 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.

If there is an existing SSH key test your SSH connection:

  1. Open 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:

     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:

  1. Open Git Bash.

  2. Paste the text below, substituting in your GitHub email address.

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

This creates a new ssh key, using the provided email as a label.

    Generating public/private rsa key pair.
  1. When you're prompted to "Enter a file in which to save the key," press 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".

     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 ,

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.

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.

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. 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!

This is a properly-configured ~/.ssh/config file for example:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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