Last active
February 26, 2026 09:15
-
-
Save FredrikM97/6a8bafeb0bc66944bc90ba0045c3b9d2 to your computer and use it in GitHub Desktop.
Github switch user
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
| # This is a script for .gitconfig and .ssh/config to manage and switch between user accounts. If first argument to ctx is left empty it is considered default otherwise we append to github.com-* | |
| ``` | |
| [alias] | |
| ctx = "!f() { LABEL=$1; EMAIL=$2; NAME=$3; CURRENT=$(git remote get-url origin); FROM=$(echo \"$CURRENT\" | grep -oE 'git@github\\.com(-[^:]*)?'); if [ -z \"$LABEL\" ]; then TO=\"git@github.com\"; elif echo \"$FROM\" | grep -q -- \"-$LABEL\"; then TO=\"git@github.com\"; else TO=\"git@github.com-$LABEL\"; fi; NEW=$(echo \"$CURRENT\" | sed \"s|$FROM|$TO|\"); git remote set-url origin \"$NEW\"; git config user.email \"$EMAIL\" && git config user.name \"$NAME\" && echo \"* Switched to ${LABEL:-default}: $EMAIL\"; }; f" | |
| whoami = "!echo \"Current email: $(git config user.email)\" && git remote -v | head -n1" | |
| private = "!git ctx private '<private email>' '<private name>'" | |
| default = "!git ctx '' '<work email>' '<work name>'" | |
| ``` | |
| Apply it on the .ssh/config: | |
| ``` | |
| Host github.com gist.github.com | |
| HostName github.com | |
| User git | |
| IdentityFile ~/.ssh/keys/id_ed12344 | |
| RequestTTY no | |
| IdentitiesOnly yes | |
| Host github.com-private | |
| HostName github.com | |
| User git | |
| IdentityFile ~/.ssh/keys/id_ed12345 | |
| RequestTTY no | |
| IdentitiesOnly yes | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment