雖然git 支持ssh 協(xié)議祈搜,但是官方并不支持 ssh 的 -i
指令來指定要使用的key文件(私鑰)贸辈。
假如在同一臺服務(wù)器上默認(rèn)的私鑰并不是你的惋鸥,你不想修改默認(rèn)私鑰贺嫂,但是你也并不想使用別人的私鑰文件來push
自己的代碼(雖然push
操作顯示的commit
作者信息是你)际起。如果能指定自己的私鑰文件就方便多了拾碌。
下面分享一個shell腳本,就可以方便的指定私鑰文件街望,命令: git.sh -i xxx/id_rsa git-command
首先校翔,創(chuàng)建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 "$@"
然后灾前,添加可執(zhí)行權(quán)限chmod +x git.sh
最后防症,把該文件放到/usr/local/bin/
目錄下,完成哎甲。
等需要的時候只需進(jìn)行如下操作:
git.sh -i xxx/xxx(私鑰文件地址) xxx(git 指令)
如 git.sh -i ~/id_rsa push origin release-debug