Switching between multiple SSH keys for different Github accounts

So I have a work and personal Github account and working locally on projects has meant I need to switch between the two SSH keys for them.

Whilst it’s not a huge time saver I decided to create a bash/terminal script function to help me easily do this.

Firstly I need to mention I use Oh My Zsh within iTerm and have a few colour variables proceeding the function. This just needs to be added to your .zshrc file:

# Reset
NC='\033[0m' # No Colour
BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[0;37m'

# # CUSTOM FUNCTIONS:

switchgit() {
	ssh-add -D -q
	echo ${PURPLE}All Github SSH identities removed.${NC}
	if [ "$1" = "work" ]
	then
		# Change this to your work ssh
		ssh-add ~/.ssh/fishtank -q
		echo ${GREEN}Work Github SSH identity active!${NC}
	elif [ "$1" = "personal" ]
	then
		# Change this to your personal ssh
		ssh-add ~/.ssh/id_rsa -q
		echo ${GREEN}Personal Github SSH identity active!${NC}
	else
		echo ${YELLOW}You need to choose work or personal.${NC}
	fi
}

Then you can run either of these to switch between:

switchgit work

or

switchgit personal

I have a few more custom bash/terminal functions I will post at a later date but credit to jexchan for the inspiration on the switching multiple Github commands 🙂