Many of these solutions looked enticing. However, I found the generic git-wrapping-script approach at the following link to be the most useful:
How to Specify an ssh Key File with the git command
The point being that there is no git command such as the following:
git -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git
Alvin's solution is to use a well-defined bash-wrapper script that fills this gap:
git.sh -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git
Where git.sh is:
#!/bin/bash
# The MIT License (MIT)
# Copyright (c) 2013 Alvin Abad
# https://alvinabad.wordpress.com/2013/03/23/how-to-specify-an-ssh-key-file-with-the-git-command
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 "$@"
I can verify that this solved a problem I was having with user/key recognition for a remote bitbucket repo with git remote update, git pull, and git clone; all of which now work fine in a cron job script that was otherwise having trouble navigating the limited-shell. I was also able to call this script from within R and still solve the exact same cron execute problem
(e.g. system("bash git.sh -i ~/.ssh/thatuserkey.pem pull")).
Not that R is the same as Ruby, but if R can do it... O:-)
-ioption likesshdoes. – Nick T Apr 18 '16 at 10:55git config core.sshCommand 'ssh -i private_key_file'. See my answer below – VonC Jul 20 '16 at 6:48