我有两个 Github 帐户(工作 + 个人),都有自己的 SSH 密钥 (
key_work
& key_personal
)
项目位于:
~/git/work
~/git/personal
我正在尝试将
GIT_SSH_COMMAND
设置为环境变量,以便它查看调用 git 命令的目录,并选择适当的 ssh 密钥。
到目前为止我已经尝试过了
.zshrc
export GIT_SSH_COMMAND='~/git-ssh.sh'
~/git-ssh.sh
#!/bin/bash
dir=$(pwd);
if [[ $dir == *"work"* ]]; then
ssh -i ~/.ssh/key_work -o IdentitiesOnly=yes
elif [[ $dir == *"personal"* ]]; then
ssh -i ~/.ssh/key_personal -o IdentitiesOnly=yes
else
exit 1
fi
但是,当从 git/work (或个人)运行
git clone [email protected]:someorg/repo.git
时,我得到
git clone [email protected]:someorg/repo.git
Cloning into 'repo'...
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYa] [-B bind_interface] [-b bind_address]
[-c cipher_spec] [-D [bind_address:]port] [-E log_file]
[-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file]
[-J destination] [-L address] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-P tag] [-p port] [-R address]
[-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
destination [command [argument ...]]
ssh [-Q query_option]
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
SSH 密钥可以访问,并且在我设置
GIT_SSH_COMMAND
之前就可以工作,所以我的脚本肯定有问题。有什么想法吗?
ssh
从脚本运行会错过目的地 (user@host
)。 Git 将目标作为参数传递给脚本,但您忘记将其传递给 ssh
。我的建议是将脚本中的所有参数传递给 ssh
:
ssh -i key "$@"
"$@"
表示“传递所有参数”。请参阅man sh
中的特殊参数部分。