Written by razrlele
12:00 March 29, 2016
在ssh
到服务器上的时候可以可以通过-i
参数来指定登录密钥,有的时候git
也会有这种需求,但是git
本身没有自带-i
参数,Google一番过后发现可以用脚本来解决。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#!/bin/bash #------------------------------------------------------------------------------- # Wrapper script that can specify an ssh-key file with the Git command # # The MIT License (MIT) - http://opensource.org/licenses/MIT # Copyright (c) 2013 Alvin Abad #------------------------------------------------------------------------------- if [ $# -eq 0 ]; then echo "Wrapper script that can specify an ssh-key file with the Git command 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
将脚本设置为可执行,然后直接在.bashrc
里面alias
一下或者export
到PATH
里面去就可以直接执行了,假设脚本的名字为git_yy
,那么就可以用下面的命令为git
指定密钥了:
1 |
git_yy -i ~/.ssh/id_rsa_yy clone [repo address] |
Refernece:
How to specify an ssh-key file with the Git command