Skip to content

Instantly share code, notes, and snippets.

@Nezteb
Last active July 10, 2018 17:58
Show Gist options
  • Save Nezteb/a7036430a9838eb754d3ed50bca364e4 to your computer and use it in GitHub Desktop.
Save Nezteb/a7036430a9838eb754d3ed50bca364e4 to your computer and use it in GitHub Desktop.
Simple Github account management
# This is specific to Github, but could apply to any git provider
# To start, you'll have to set up two sets of git SSH keys.
# Assuming you have your personal account set up by default:
# * ~/.ssh/id_rsa (your personal ssh private key)
# * ~/.ssh/id_rsa.pub (your personal ssh public key)
# * ~/.ssh/backup/work (your work ssh private key)
# * ~/.ssh/backup/work.pub (your work ssh public key)
# And some environment variables
export PERSONAL_EMAIL="personal@email.com" # email tied to your personal account
export WORK_EMAIL="work@email.com" # email tied to your work account
# Simple initialize
export GIT_ACCOUNT="null"
# TODO: dynamically configure multiple accounts
checkGit() {
EMAIL="$(grep -io '[A-Z0-9._%+-]\+@[A-Z0-9.-]\+\.[A-Z]\{2,4\}' ~/.ssh/id_rsa.pub)"
if [ $EMAIL = $PERSONAL_EMAIL ]; then
export GIT_ACCOUNT="personal"
echo "Current git account: Personal"
elif [ $EMAIL = $WORK_EMAIL ]; then
export GIT_ACCOUNT="work"
echo "Current git account: Work"
fi
}
gitPersonal() {
checkGit
if [ $GIT_ACCOUNT = "work" ]; then
echo "Switching to personal"
ssh-add -D
mkdir -p ~/.ssh/backup
mv ~/.ssh/id_rsa ~/.ssh/backup/work
mv ~/.ssh/id_rsa.pub ~/.ssh/backup/work.pub
mv ~/.ssh/backup/personal ~/.ssh/id_rsa
mv ~/.ssh/backup/personal.pub ~/.ssh/id_rsa.pub
git config --global user.email $PERSONAL_EMAIL
eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_rsa
if [ $? -eq 0 ]; then
echo "Successfully added personal key"
ssh -T git@github.com
if [ $? -eq 1 ]; then
echo "Successfully auth'd to personal Github"
export GIT_ACCOUNT="personal"
echo "Now using personal git account"
else
echo "Error authing to personal Github"
gitWork # Switch back
fi
else
echo "Error adding key"
gitWork # Switch back
fi
elif [ $GIT_ACCOUNT = "personal" ]; then
echo "Already using personal account."
else
echo "GIT_ACCOUNT set improperly"
fi
}
gitWork() {
checkGit
if [ $GIT_ACCOUNT = "personal" ]; then
echo "Switching to work"
ssh-add -D
mkdir -p ~/.ssh/backup
mv ~/.ssh/id_rsa ~/.ssh/backup/personal
mv ~/.ssh/id_rsa.pub ~/.ssh/backup/personal.pub
mv ~/.ssh/backup/work ~/.ssh/id_rsa
mv ~/.ssh/backup/work.pub ~/.ssh/id_rsa.pub
git config --global user.email $WORK_EMAIL
eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_rsa
if [ $? -eq 0 ]; then
echo "Successfully added work key"
ssh -T git@github.com
if [ $? -eq 1 ]; then
echo "Successfully auth'd to work Github"
export GIT_ACCOUNT="work"
echo "Now using work git account"
else
echo "Error authing to work Github"
gitPersonal # Switch back
fi
else
echo "Error adding key"
gitPersonal # Switch back
fi
elif [ $GIT_ACCOUNT = "work" ]; then
echo "Already using work account."
else
echo "GIT_ACCOUNT set improperly"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment