

Specify a specific SSH private key for git pull/git clone
source link: https://ma.ttias.be/specify-a-specific-ssh-private-key-for-git-pull-git-clone/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Specify a specific SSH private key for git pull/git clone
Mattias Geniar, November 19, 2019
Follow me on Twitter as @mattiasgeniar
I’ve been moving some projects around lately and found myself in need of a weird thing I hadn’t considered before: specifying a specific SSH private key for running things like git clone
or git pull
.
Luckily, it wasn’t that hard.
Using a specific environment variable
You can overwrite the SSH command that’s being used by git, by giving it a custom environment variable.
Consider this example:
$ GIT_SSH_COMMAND='ssh -i /var/www/html/ma.ttias.be/.ssh/id_rsa' git pull
This runs the git pull
command, but it does so by using a very specific private key located in /var/www/html/ma.ttias.be/.ssh/id_rsa
.
The GIT_SSH_COMMAND
is available in modern git versions and can be used to overwrite the identity (-i
) or things like the SSH port.
A custom SSH config
An alternative approach, but one I found less useful for quick-and-dirty git operations, is to modify your SSH client config.
You can specify an alias for SSH operations and use that to point to the correct identity file.
$ cat ~/.ssh/config
Host yourserver
Hostname something.domain.tld
IdentityFile /var/www/html/ma.ttias.be/.ssh/id_rsa
IdentitiesOnly yes
Now, if you git clone from that specific alias, it will use your private key.
$ git clone git@yourserver:yourrepo.git
The yourserver translates to the alias used in ~/.ssh/config
.
Want to subscribe to the cron.weekly newsletter?
I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.
It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.
No spam. Just some good, practical Linux & open source content.
Recommend
-
36
It's not the same without you Join the community to find out what other Atlassian users are discussing, debating and creating.
-
18
git clone 指定 ssh-key 其实我们往往会遇到这样一个问题 打个比方: 公司邮箱是:[email protected] 个人邮箱:[email protected] & [email protected] 我们分别有:公司git仓库 / github仓库...
-
21
Introduction If you are using SSH keys with Git to clone and pull your repositories, you may have to manage several SSH keys. For example, it is common to setup a "deploy key" in GitHub (Repository | Settings | Deploy Keys
-
3
New issue Fix implicit_clone for &&T #8231
-
9
Copy link Member m-ou-se c...
-
7
Git: Clone a specific branch less than 1 minute read...
-
7
Git 免密、免 SSH 进行 push & pull博主:HCLonely发布时间:2020-07-0354350 次浏览607 字数分类:
-
9
How to use specific private SSH key in git commands 252 views 6 months ago Ubuntu When you run git or ssh commands,...
-
8
Conversation Contributor...
-
9
【ssh_config】SSH中配置多个private key_多个私钥配置-CSDN博客 【ssh_config】SSH中配置多个private key ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK