18

I want to use other IdentityFile for git. I want to use it dynamically, not via config. I'm doing this:

  $ GIT_SSH_COMMAND='ssh -i /home/my_user/.ssh/id_ed25519' git pull origin master
  repository access denied.
  fatal: Could not read from remote repository.

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

The pub key "id_ed25519.pub" is at my bitbucket.

And this fails too:

  $ git pull origin master
  repository access denied.
  fatal: Could not read from remote repository.

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

And:

$ git remote -v
origin  git@bitbucket.org:company123/repo456.git (fetch)
origin  git@bitbucket.org:company123/repo456.git (push)

Adding "-v" to 'ssh -i /home/my_user/.ssh/id_ed25519' reveals that my RSA key is being used, instead of ED. Why?

jerry
  • 827
  • 2
  • 7
  • 10

3 Answers3

18

I had the same issue with the recent Ubuntu version:

Using -vvv revealed following:

debug2: key: /home/ubuntu/.ssh/id_rsa (0x5628e48246d0), agent
debug2: key: /home/ubuntu/code/id_rsa (0x5628e4820af0), explicit

Adding -o IdentitiesOnly=yes solved it. It tells SSH to ignore identities from ssh-agent (e.g. added with ssh-add) which otherwise take precedence.

Full git command:

GIT_SSH_COMMAND='ssh -o IdentitiesOnly=yes -i /home/ubuntu/code/id_rsa -F /dev/null' git pull
luator
  • 4,769
  • 3
  • 30
  • 51
AlexTG
  • 181
  • 1
  • 2
  • I added a sentence briefly describing what `IdentitiesOnly` does. Feel free to edit/revert in case you don't agree with the change. – luator Sep 08 '22 at 09:01
9

Check you commands (is git called directly or through an alias) and configuration:
As I mention in "Using GIT_SSH_COMMAND", a git config -l might reveal other configuration that would override the environment variable.

Check the return of git config core.sshCommand.

Finally, GIT_SSH_COMMAND means Git 2.10+, so if your version of Git is too old, you will need to update it first.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

In case anyone is having this problem with the slight variation of setting GIT_SSH_COMMAND on one line, and then on a different line actually running the git command, please try one of the following:

  1. set GIT_SSH_COMMAND on the same line:
$ GIT_SSH_COMMAND="ssh -i ${key_location}" git clone [...]

or....

  1. export GIT_SSH_COMMAND
$ export GIT_SSH_COMMAND="ssh -i ${key_location}"
$ git clone [...]

I was setting GIT_SSH_COMMAND right after finding which key to use, and then running git a few lines later. I mistakenly removed the export and it broke the git command.


Since this is one of the top google links when searching for "GIT_SSH_COMMAND" and the question is more upvoted than the answers, it might be that some visitors have the same problem as OP but for a different reason than what the other answers recommend. Hopefully this is helpful for someone.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42