Skip to content

Instantly share code, notes, and snippets.

@FredrikM97
Last active February 26, 2026 09:15
Show Gist options
  • Select an option

  • Save FredrikM97/6a8bafeb0bc66944bc90ba0045c3b9d2 to your computer and use it in GitHub Desktop.

Select an option

Save FredrikM97/6a8bafeb0bc66944bc90ba0045c3b9d2 to your computer and use it in GitHub Desktop.
Github switch user
# 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