LoginSignup
309
221

More than 3 years have passed since last update.

git clone 時に秘密鍵を指定する

Last updated at Posted at 2015-08-24

git の支援が弱くて変なやり方しかない。
=> GIT_SSH_COMMAND または git -c core.sshCommand が使えます。

.ssh/config をいじる

.ssh/config を1瞬いじって元に戻す、みたいな

~/.ssh/config
Host github.com
    User git
    IdentityFile ~/.ssh/deploy_key

絶対にやりたくない

.ssh/config に別 Host として登録する

~/.ssh/config
Host deploy1
  Hostname github.com
  User git
  IdentityFile ~/.ssh/deploy1_key

Host deploy2
  Hostname github.com
  User git
  IdentityFile ~/.ssh/deploy2_key

で、git clone の時は Host 名を指定する

$ git clone deploy1:organization/repository
$ git clone deploy2:organization/repository

とはいえ deploy key ごとに .ssh/config にエントリを増やしたくない。

GIT_SSH を使う

こんなスクリプトを作って、

git-ssh.sh
#!/bin/sh
exec ssh -oIdentityFile=~/.ssh/deploy_key "$@"

GIT_SSH 環境変数に指定する。

$ GIT_SSH=git-ssh.sh git clone git@github.com:...

とはいえ deploy key ごとに git-ssh スクリプトを作りたくはない。
※ GIT_SSH に指定するスクリプトに引数を指定したかったが、ダメ

git wrapper

なのでこんな git ラッパーを作るかんじになった。

git-ssh.sh
#!/bin/bash

usage_exit() {
    echo "Usage: $0 [-i identity_file] -- [GIT ARGUMENTS]" 1>&2
    exit 1
}

while getopts i:h OPT
do
    case $OPT in
        i) IDENTITY_FILE=$OPTARG
           ;;
        h) usage_exit
           ;;
    esac
done
shift $((OPTIND - 1))

if [ -n "$IDENTITY_FILE" ]; then
    tempfile=$(mktemp --dry-run)
    cat <<EOF > $tempfile
#!/bin/sh
exec ssh -oIdentityFile=${IDENTITY_FILE} "\$@"
EOF
    chmod a+x $tempfile
    GIT_SSH=$tempfile git $@
    rm -f $tempfile
else
    git $@
fi

これを使うと

$ git-ssh.sh -i ~/.ssh/deploy_key -- clone git@github.com:....

と出来るようになる。

GIT_SSH_COMMAND を使う

EDIT: 2017-08 thanks to mpyw

git 2.3 以降で環境変数 GIT_SSH_COMMAND で ssh コマンドを指定できるようになった :tada:

env GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone ...

git -c core.sshCommand を使う

EDIT: 2018-06 thanks to akirattii
EDIT: 2019-05 thanks to egtra

git 2.10.0 から git config core.sshCommand で ssh コマンドを指定できるようになった
。また、一時的に config 設定をしたい場合は git -c オプションを利用できる。

$ git -c core.sshCommand="ssh -i ~/.ssh/id_rsa_example -F /dev/null" clone ...
309
221
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
309
221