Skip to content

Instantly share code, notes, and snippets.

@adityasatrio
Created June 6, 2023 15:04
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 adityasatrio/4d314575c282ac8be988b0347a61ef90 to your computer and use it in GitHub Desktop.
Save adityasatrio/4d314575c282ac8be988b0347a61ef90 to your computer and use it in GitHub Desktop.
Git multiple account script
follow this tutorial until step 4 https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/
bash script that implements the functionality you've requested. Save this as a file named git-switch and make sure to give it executable permissions (you can do this with chmod +x git-switch).
Please replace [PATH_TO_BROWSER] with your actual browser executable path, and replace https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/ with the link you want to open when 'help' is invoked.
```
#!/bin/bash
function usage() {
echo "Possible commands:"
echo "default: Switch to the default GitHub account"
echo "PERSONAL: Switch to the PERSONAL GitHub account"
echo "help: Display this help message"
echo "clone: Print clone commands for different accounts"
echo "remote: Show git remote and print set-url commands"
echo "init: Print commands for setting remote origin for different accounts"
}
function open_help() {
echo "https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/"
[PATH_TO_BROWSER] "https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/" &> /dev/null
usage
}
function switch_default() {
ssh-add -D
ssh-add ~/.ssh/id_rsa
git config user.name "work"
git config user.email "work@work.id"
echo "git account switched"
}
function switch_ADITYA() {
ssh-add -D
ssh-add ~/.ssh/PERSONAL
git config user.name "personal"
git config user.email "personal@gmail.com"
echo "git account switched PERSONAL"
}
function print_clone() {
echo "default : git clone git@github.com:work/repo_name.git"
echo "account ADITYA_PERSONAL : git clone git@github.com-PERSONAL:personal/repo_name.git"
}
function print_remote() {
git remote -v
echo "DEFAULT : git remote set-url origin git@github.com:work/repo_name.git"
echo "ADITYA_PERSONAL : git remote set-url origin git@github.com-PERSONAL:personal/repo_name.git"
}
function print_init() {
echo "DEFAULT : git remote add origin git@github.com:work/repo_name.git "
echo "ADITYA_PERSONAL git remote add origin git@github.com-PERSONAL:personal/repo_name.git"
}
case $1 in
"default"|"")
switch_default
;;
"PERSONAL")
switch_PERSONAL
;;
"help")
open_help
;;
"clone")
print_clone
;;
"remote")
print_remote
;;
"init")
print_init
;;
*)
echo "Invalid command"
usage
;;
esac
```
To run this script from anywhere, you can add its location to your PATH. One way to do this is by adding export PATH=$PATH:/path/to/your/script/directory to your .bashrc or .bash_profile file, replacing /path/to/your/script/directory with the actual directory path that contains your git-switch script. After adding this line, run `source ~/.bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment