Force git to use specific key.pub

4.4k Views Asked by At

I have to keys in ~/.ssh dir. When I am trying to commit something in console bitbucket is asking me for password instead use ssh key. Also when I try to clone this repo in order to use global git settings it is still asking me for password.

My questions:

  • how to config git to use ssh keys?
  • how to force it?
  • which key it is trying to use?
  • is it trying to much it by email? (if yes than is it using mail from global config or local repository config?)
  • can I use ssh key without additional password, or is it required to have one?
2

There are 2 best solutions below

0
On

To make GIT use a specific SSH key, you prefix the GIT command with

GIT_SSH_COMMAND="ssh -i <path-to-key>"

Where "path-to-key" is the path to the private SSH key (without the ".pub").

So if you would like to clone from git://random-address and your public key is in /home/me/.ssh/mysecondkey.pub, you run:

GIT_SSH_COMMAND="ssh -i /home/me/.ssh/mysecondkey" git clone git://random-address

You need to make sure that you choose a clone-URL which does not start with "https://", but either "ssh://" or "git://". I know at least GitHub lets you choose which one to use.

0
On

Adding the following as I had the same problem about specifying a specific public key for some repository:

As per this answer: you can add a configuration per repository as to what ssh command the git client uses. If you want a specific ssh-key used you can do:

git config --local core.sshCommand 'ssh -i <path-to-key>'

Then git will use that command, and thus that key to authenticate with the remote.