Last active
April 22, 2025 07:43
-
-
Save adityasatrio/4d314575c282ac8be988b0347a61ef90 to your computer and use it in GitHub Desktop.
Git multiple account script
This file contains hidden or 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
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 "[email protected]" | |
echo "git account switched" | |
} | |
function switch_ADITYA() { | |
ssh-add -D | |
ssh-add ~/.ssh/PERSONAL | |
git config user.name "personal" | |
git config user.email "[email protected]" | |
echo "git account switched PERSONAL" | |
} | |
function print_clone() { | |
echo "default : git clone [email protected]:work/repo_name.git" | |
echo "account ADITYA_PERSONAL : git clone [email protected]:personal/repo_name.git" | |
} | |
function print_remote() { | |
git remote -v | |
echo "DEFAULT : git remote set-url origin [email protected]:work/repo_name.git" | |
echo "ADITYA_PERSONAL : git remote set-url origin [email protected]:personal/repo_name.git" | |
} | |
function print_init() { | |
echo "DEFAULT : git remote add origin [email protected]:work/repo_name.git " | |
echo "ADITYA_PERSONAL git remote add origin [email protected]: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