Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chsxf/ad948a825eb1709d64bdac985131af9a to your computer and use it in GitHub Desktop.
Save chsxf/ad948a825eb1709d64bdac985131af9a to your computer and use it in GitHub Desktop.
Clone a git repository and configure it for SSH

Purpose

This script is useful when you have multiple SSH keys on your system, eventually having several SSH keys tied to different accounts for the same domain.

It makes use of the core.sshCommand git configuration directive to assign a specific SSH key to a repository.

Requirements

The script will look for SSH keys in ~/.ssh

You'll also need bash and git installed on your system.

Installation

From the Terminal, move to the directory you want to install this script (I recommend a directory with your $PATH environment variable) and execute those commands:

wget https://gist.github.com/chsxf/ad948a825eb1709d64bdac985131af9a/raw/gcssh
chmod +x gcssh

Usage

gcssh <repository_ssh_uri>

The script will then guide you through the process.

#!/bin/bash
# Color ANSI escape codes:
# https://stackoverflow.com/a/5947802
WHITE="\033[1;37m"
RED="\033[0;31m"
RESET_COLOR="\033[0m"
if [ $# -ne 1 ]; then
echo -e "${RED}Invalid argument count${RESET_COLOR}\n"
echo "Usage:"
echo -e "\tgcssh repository_uri"
exit 1
fi
GIT_URI=$1
echo -e "${WHITE}Please answer those questions in order to clone and prepare your repository with SSH"
echo -e "Repository URI: ${GIT_URI}${RESET_COLOR}\n"
CWD=`pwd`
cd ~/.ssh
KEYS=`ls -1 *.pub`
cd "${CWD}"
COUNTER=0
echo "Select the SSH to use with your repository:"
for entry in $KEYS
do
[[ $entry =~ ^(.+)\.pub$ ]];
((COUNTER=COUNTER+1))
echo -e "\t$COUNTER. ${BASH_REMATCH[1]}"
done
read -p "Key #: " KEY_INDEX
if [[ $KEY_INDEX =~ ^[1-9]\d*$ && $KEY_INDEX -le $COUNTER ]]; then
((SELECTED_INDEX=$KEY_INDEX-1))
ARR=($KEYS)
[[ ${ARR[$SELECTED_INDEX]} =~ ^(.+)\.pub$ ]];
SELECTED_KEY=${BASH_REMATCH[1]}
echo -e "${WHITE}Selected key: ${SELECTED_KEY}${RESET_COLOR}"
else
echo -e "${RED}Invalid key${RESET_COLOR}"
exit 2
fi
echo ""
[[ $GIT_URI =~ /(.+).git$ ]];
DEFAULT_GIT_FOLDER=${BASH_REMATCH[1]}
read -p "Clone destination folder (default: ${DEFAULT_GIT_FOLDER}): " GIT_CLONE_FOLDER
GLOBAL_GIT_USERNAME=`git config --global user.name`
read -p "User name for the repository (global: ${GLOBAL_GIT_USERNAME}): " LOCAL_GIT_USERNAME
GLOBAL_GIT_USEREMAIL=`git config --global user.email`
read -p "User email for the repository (global: ${GLOBAL_GIT_USEREMAIL}): " LOCAL_GIT_USEREMAIL
echo ""
echo -e "${WHITE}Cloning in ..."
for i in {5..1}
do
echo $i
sleep 1
done
echo -e "${RESET_COLOR}"
if [[ -z $GIT_CLONE_FOLDER ]]; then
GIT_FOLDER=$DEFAULT_GIT_FOLDER
else
GIT_FOLDER=$GIT_CLONE_FOLDER
fi
CORESSH_COMMAND="ssh -i ~/.ssh/${SELECTED_KEY} -F /dev/null"
git -c core.sshCommand="${CORESSH_COMMAND}" clone ${GIT_URI} "${GIT_FOLDER}"
echo ""
cd "${GIT_FOLDER}"
git config --local core.sshCommand "${CORESSH_COMMAND}"
if [[ -n $LOCAL_GIT_USERNAME ]]; then
echo -e "${WHITE}Setting local user name${RESET_COLOR}"
git config --local user.name "${LOCAL_GIT_USERNAME}"
fi
if [[ -n $LOCAL_GIT_USEREMAIL ]]; then
echo -e "${WHITE}Setting local user email${RESET_COLOR}"
git config --local user.email "${LOCAL_GIT_USEREMAIL}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment