雖然git 支持ssh 協議,但是官方并不支持 ssh 的 -i
指令來指定要使用的key文件(私鑰)。
假如在同一臺服務器上默認的私鑰并不是你的,你不想修改默認私鑰,但是你也并不想使用別人的私鑰文件來push
自己的代碼(雖然push
操作顯示的commit
作者信息是你)。如果能指定自己的私鑰文件就方便多了。
下面分享一個shell腳本,就可以方便的指定私鑰文件,命令: git.sh -i xxx/id_rsa git-command
首先,創建git.sh
文件,添加如下代碼:
#!/bin/bash
# The MIT License (MIT)
# Copyright (c) 2013 Alvin Abad
if [ $# -eq 0 ]; then
echo "Git wrapper script that can specify an ssh-key file
Usage:
git.sh -i ssh-key-file git-command
"
exit 1
fi
# remove temporary file on exit
trap 'rm -f /tmp/.git_ssh.$$' 0
if [ "$1" = "-i" ]; then
SSH_KEY=$2; shift; shift
echo "ssh -i $SSH_KEY \$@" > /tmp/.git_ssh.$$
chmod +x /tmp/.git_ssh.$$
export GIT_SSH=/tmp/.git_ssh.$$
fi
# in case the git command is repeated
[ "$1" = "git" ] && shift
# Run the git command
git "$@"
然后,添加可執行權限chmod +x git.sh
最后,把該文件放到/usr/local/bin/
目錄下,完成。
等需要的時候只需進行如下操作:
git.sh -i xxx/xxx(私鑰文件地址) xxx(git 指令)
如 git.sh -i ~/id_rsa push origin release-debug