Last active
December 15, 2023 18:46
-
-
Save RagingTiger/51b586743c2b1d7a941794ff8c13780b to your computer and use it in GitHub Desktop.
Configure Git credentials for when using multipe GitHub accounts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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