git-push with specific SSH key

来源:互联网 发布:淘宝和天猫有什么区别 编辑:程序博客网 时间:2024/06/05 07:06

 

Thursday, 11 August 2011

I figured this was worth a post, as I’ve never ran into this problem before. I’ve been working on setting up a server, to which I connect using RSA. The standard name for an RSA key file isid_rsa(.pub). However, as I use my ‘standard’ key elsewhere, I wanted to use a specific key for this server.

Connecting via SSH to the server with the key is as simple as adding -i /path/to/key. The problem arose when I needed to be able to push to a Git repository hosted on the server, and adding-i to git-push doesn’t work.

The solution was to add a Host directive to my ~/.ssh/config file. Then, use thatHost to connect to when push’ing to the remote server.

If it doesn’t exist, create the file ~/.ssh/config. Add the following to it, editing where necessary.

Host RemoteServer  HostName remote-server.tld  User git  IdentityFile ~/.ssh/remoteserver_key

Remember to reload SSH after creating this file. This example config would be the equivalent of running a command likessh git@remote-server.tld -i ~/.ssh/remoteserver_key. You can even runssh RemoteServer to test the connection out.

In your Git repository, add a new remote repository. Here I’ve called it origin, as the convention might have it.

git remote add origin RemoteServer:path/to/repository.git

Instead of specifying a user @ a domain, it uses the name of the Host in the SSH config. The path/to/repository.git is relative, on the average system, that will probably point to/home/git/path/to/repository.git.

Try running a git push origin master to see if it works!

0 0
原创粉丝点击