Skip to content

Instantly share code, notes, and snippets.

@vianho
Last active October 11, 2021 07:15
Show Gist options
  • Save vianho/4e47c64728a7e716c1afc29b17eaae0c to your computer and use it in GitHub Desktop.
Save vianho/4e47c64728a7e716c1afc29b17eaae0c to your computer and use it in GitHub Desktop.
Git init then select the correct github account for git config

This is a simple bash script for people that uses more than 1 github account on 1 machine to pick which account to use when a git repository is initiated. This script uses the git config --local command to set the username and email for the current repository.

How to use:

  1. Create the script file. I created a new directory on home called Scripts to store my scripts.
  2. Copy and paste gini script to the new file.
  3. Fill in the appropriate usernames and emails, and feel free to add more elif statements if you have more accounts.
  4. Allow users to execute the gini script by changing the permission with chmod +x gini
  5. Open a file where you store the aliases for your shell, e.g. .bashrc, bash_aliases, .zshrc, etc.
  6. Set an alias for gini on the file that you've opened with alias gini=/path/to/script.
  7. Run source .zshrc or source .bashrc to reload your shell settings.
  8. Move to your project directory and run gini to test it out.

Sorry I was too lazy to come up with a good name, so I'll just call this gini for now.

#!/usr/bin/bash
echo "Hello from GINI"
git init .
touch .gitignore
echo "You have the following github accounts."
echo "1. MAIN_USERNAME - MAIN_EMAIL@email.com"
echo "2. ALT_USERNAME - ALT_EMAIL@email.com"
echo "Which one would you use for this project? (1/2)"
read -p "> " account
if [[ $account -eq 1 ]]
then
echo "user=MAIN_USERNAME"
git config --local user.name "MAIN_USERNAME"
git config --local user.email "MAIN_EMAIL@email.com"
elif [[ $account -eq 2 ]]
then
echo "user=ALT_USERNAME"
git config --local user.name "ALT_USERNAME"
git config --local user.email "MAIN_EMAIL@email.com"
else
echo "Invalid input!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment