Blog·Tanky WooABOUTRSS

Git use Specified SSH Key

15 Apr 2014

Use ~/.ssh/config

Host gitolite-as-alice
  User git
  IdentityFile /home/whoever/.ssh/id_rsa.alice

Host gitolite-as-bob
  User git
  IdentityFile /home/whoever/.ssh/id_dsa.bob

Then you just use gitolite-as-alice and gitolite-as-bob instead of the hostname in your URL:

git remote add alice git@gitolite-as-alice:whatever.git
git remote add bob git@gitolite-as-bob:whatever.git

Also can see this and this

This way is simple, but every link and every one should add a config.


$GIT_SSH is an environment variable in git, which can be seen in man git:

   If this environment variable is set then git fetch and git push will use **this
   command** instead of ssh when they need to connect to a remote system. The
   $GIT_SSH command will be given exactly two arguments: the username@host (or
   just host) from the URL and the shell command to execute on that remote system.

   To pass options to the program that you want to list in GIT_SSH you will need
   to wrap the program and options into a shell script, then set GIT_SSH to refer
   to the shell script.

   Usually it is easier to configure any desired options through your personal
   .ssh/config file. Please consult your ssh documentation for further details.

write a wapper named


if [ -z "$PKEY" ]; then
    # if PKEY is not specified, run ssh using default keyfile
    ssh "$@"
    ssh -i "$PKEY" "$@"

Then :

PKEY=~/.ssh/xxx-id_rsa GIT_SSH=/path/to/ git pull

and GIT_SSH can be set to a system environment variable and not need to set GIT_SSH every time.

export GIT_SSH=/path/to/
PKEY=~/.ssh/xxx-id_rsa git pull

Also can see this, this or this