git-push with specific SSH key

5 篇文章 0 订阅

 

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 is id_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 that Host 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 like ssh git@remote-server.tld -i ~/.ssh/remoteserver_key. You can even run ssh 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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值