Skip to content

Instantly share code, notes, and snippets.

@michaeltreat
Last active September 21, 2022 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeltreat/0987a0d6f3cf9494fedf838deec2bdc0 to your computer and use it in GitHub Desktop.
Save michaeltreat/0987a0d6f3cf9494fedf838deec2bdc0 to your computer and use it in GitHub Desktop.
A quick and simple script to change your Github user.email and user.name config details. Also a decent intro into scripting.
This is a simple script file that will allow a user to quickly switch their current github user.name and user.email. This is useful if you have multiple github accounts.
I use gh auth login to manage my github accs. Gh auth login will allow your device to push changes to a repo, and it seems that it will allow any user account on the device to do that.
But, if you don't want one account to be making changes on another repo it's important to manage the current github user credentials. This script helps with that.
Scripting:
First, read up on how to make script files on a mac here:
https://support.apple.com/guide/terminal/about-shell-scripts-apd53500956-7c5b-496b-a362-2845f2aab4bc/mac
https://www.hastac.org/blogs/joe-cutajar/2015/04/21/how-make-simple-bash-script-mac
File starts here:
#!/bin/bash
# Description: A quick and simple script to swtich between two GitHub Accounts.
# Usage: Call the script and pass in the flag that indicates which account you want to switch to.
# For example, if I want to switch my primary account I enter: ckgh main
# If I want to switch to my work account I type: ckgh work
# With no argument the script will just return the current github --global config
if [ -z "$1" ]; then
echo git config --global
echo user.email: $(git config --global user.email)
echo user.name: $(git config --global user.name)
exit 1
fi
# Use this as a template. Enter your own email and name into this file.
if [ $1 = "main" ]; then
git config --global user.email "someMainEmail@gmail.com"
git config --global user.name "main name"
echo git config user.email is now set to $(git config --global user.email)
echo git config user.name is now set to $(git config --global user.name)
exit 1
# Use this elif as a second template to add any secondary accounts. You can use the same template multiple times.
elif [ $1 = "work" ]; then
git config --global user.email "myWorkEmail@gmail.com"
git config --global user.name "work name"
echo git config user.email is now set to $(git config --global user.email)
echo git config user.name is now set to $(git config --global user.name)
exit 1
# Fail out if the wrong flag is added.
else
echo Command failed, no profile associated with "$1".
exit 0
fi
# End of file, the rest are insturctions!
# Instructions:
# Place this script in some directory you want to use to hold any scripts. No need to panic about where scripts have to go or are supposed to go, any directory will do, you just need to know the path to this dir.
# Go to your .bashrc or .zshrc where we will add an alias. This alias is a 'quick command' that we can use in our terminal at anytime so we don't have to navigate to our scripts folder every time.
# For example, I decided to store my scripts in a new directory called scripts, located at ~/scripts.
# Then in my .zshrc file I added the entire line after the # mark at the end of the file:
# alias ckgh="~/scripts/ckgh.txt"
# note that the alias can be named whatever you want. I used ckgh because when I want to run this I think of 'Chec-k Git-Hub'.
# after the alias, you need to point to the script file that you want to run. If you were to type ~/scripts/ckgh.txt at any point it would run the ckgh.txt file at, but here we just put that command into an alias so it's easier to type. When we type ckgh it will actually replace it with ~/scripts/ckgh.txt.
# reset the terminal and then when you can type ckgh to run this script.
# Now you can run this script at anytime by typing: ckgh <flag>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment