How to tell Git which SSH Key to use

It is not possible to tell Git which SSH credentials to use — strictly speaking. But you can use SSH config to effectively achieve the same result.

Suppose you have a private key ~/.ssh/special_id_rsa and you are trying to clone the repository git@github.com:username/reponame.git . You would need to add a new host entry to your SSH config that uses the desired private key.

# ~/.ssh/config

Host your.hostname.com
    Hostname github.com
    User git
    IdentityFile ~/.ssh/special_id_rsa

If the SSH config file ~/.ssh/config does not exist, simply create it and add the host info. You then clone the repository by replacing the repo url domain with the host defined in the SSH config.

git clone git@your.hostname.com:username/reponame.git

After successful clone,git fetch and git commit will automatically use the special_id_rsa private key to connect to the Git server.

For already existing repositories however, you will additionally need to modify the Git config file .git/config inside the project. The url of remote “origin” must be changed to the host defined in ~/.ssh/config.

# .git/config
[remote "origin"]
    url = git@your.hostname.com:username/reponame.git

Now you can “tell” Git which SSH credentials to use through SSH config.

How to tell Git which SSH Key to use

  • qq_43638135
    妲己再美究为妃: 博主没有想过自己接一些私活干吗?我现在还没毕业,但是我也确实听说外挂市场自动化游戏脚本市场挺火热的,并且报酬也很丰厚,但是具体的我也不是很清楚,求解答。 (1个月前 #47楼) 查看回复(2) 举报 回复
    22