Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DiogenesAnalytics/65f4ebff6922d83c6b50cef1c176a8fd to your computer and use it in GitHub Desktop.
Save DiogenesAnalytics/65f4ebff6922d83c6b50cef1c176a8fd to your computer and use it in GitHub Desktop.
Configure Git credentials for when using multipe GitHub accounts.
git_config_author(){
git config --local user.name "${1}"
git config --local user.email "${2}"
}
git_config_sshkey(){
# get ssh hosts
local ssh_hosts="$(grep "github.com-[a-Z]*" ~/.ssh/config | \
awk '{print $2}' | sed 's/github\.com-//g' | tr '\n' ' ')"
# check for argument
if [ -z "${1}" ]; then
echo "Please provide appropriate ssh-host (e.g. github.com-USERNAME):" \
"${ssh_hosts}"
else
# determine authorship
if ! grep -sq "${1}" ~/.ssh/config; then
echo "Author ${1} not found amongst: ${ssh_hosts}"
else
# get remote URL
local remote_url=$(git remote -v | awk '{print $2}' | tail -n 1)
# set remote URL
git remote set-url origin \
$(echo ${remote_url} | sed "s/github\.com.*:/github\.com-${1}:/g")
# setup author credentials
case ${1} in
GITHUB_USER_1)
git_config_author 'John Doe' 'johndoe@example.com'
;;
GITHUB_USER_2)
git_config_author 'Jane Doe' 'janedoe@gmail.com'
;;
esac
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment