gitSwitch
Who doesn’t want a secret identity right?
I wanted one, I named him thecist
, then this happened
That’s right, I did everything right, but forgot to change my user name when pushing my changes. sigh
This was meant to be a throw away repository for a take home assessment but it led me to two realizations
First, I need a new GitHub account
My old account was nearing 81 projects and, I still don’t know where the course assignments end and the abandoned projects start
I needed a new account, not for my secret identity — which I’m definitely still planning out — but for me
Graduated me — 4.0 btw — with more dedication, commitment and actual care for my public profiles
Secondly, my ssh files were very quickly becoming a mess, I get really anxious deleting my private/public pairs because it might be in use for more than one thing, that had to change
So I got to work, dusted off some old code and got this working pretty easily
import os
import subprocess
# Define your GitHub profiles
GITHUB_PROFILES = {
"profile1": {
"identity_file": r"path\to\identity\file",
"git_name": "profile name",
"git_email": "profile-email"
},
"profile1": {
"identity_file": r"path\to\identity\file",
"git_name": "profile name",
"git_email": "profile-email"
}
}
SSH_CONFIG_PATH = os.path.expanduser(r"~/.ssh/config")
def prompt_user_choice():
print("Available GitHub profiles:")
for i, key in enumerate(GITHUB_PROFILES, 1):
print(f"{i}. {key}")
choice = input("Select a profile by name: ").strip()
if choice not in GITHUB_PROFILES:
print(f"❌ Invalid choice: {choice}")
exit(1)
return choice
def update_ssh_config(profile_key: str):
identity_file = GITHUB_PROFILES[profile_key]["identity_file"]
# Read the config file
if not os.path.exists(SSH_CONFIG_PATH):
print("❌ SSH config file not found.")
exit(1)
with open(SSH_CONFIG_PATH, "r", encoding="utf-8") as f:
lines = f.readlines()
new_lines = []
in_github_block = False
identity_updated = False
for line in lines:
stripped = line.strip()
if stripped.startswith("Host "):
# Check if we are entering the config block and identity file isn't set
if in_github_block and not identity_updated:
new_lines.append(f"\tIdentityFile {identity_file}\n")
identity_updated = True
in_github_block = stripped == "Host github.com"
if in_github_block and stripped.startswith("IdentityFile"):
new_lines.append(f"\tIdentityFile {identity_file}\n")
identity_updated = True
else:
new_lines.append(line)
# If IdentityFile wasn't found, append it
if in_github_block and not identity_updated:
new_lines.append(f"\tIdentityFile {identity_file}\n")
with open(SSH_CONFIG_PATH, "w", encoding="utf-8") as f:
f.writelines(new_lines)
print(f"[✓] Updated SSH IdentityFile for '{profile_key}'")
def update_git_identity(profile_key: str):
name = GITHUB_PROFILES[profile_key]["git_name"]
email = GITHUB_PROFILES[profile_key]["git_email"]
subprocess.run(["git", "config", "--global", "user.name", name], check=True)
subprocess.run(["git", "config", "--global", "user.email", email], check=True)
print(f"[✓] Set Git identity to: {name} <{email}>")
if __name__ == "__main__":
profile = prompt_user_choice()
update_ssh_config(profile)
update_git_identity(profile)
A single script to manage my *ahem* GitHub *ahem* identity
It has three main components, the first prompts you for which user you’d like to
be, I have both Tayomide and
thecist currently, the second updates your
~/.ssh/config
file with the configurations you provide in GITHUB_PROFILES
and the third updates your github globals
If you’d like gitSwitch
to be a global CLI command on windows here are the
steps!
- Create a folder anywhere — personally my folder is this (
~/Scripts/
) - Add the folder to your environment variable Path
- Add
gitSwitch.py
into the earlier created folder - Create a file called
gitSwitch.ps1
in the same folder - Add the content below into
gitSwitch.ps1
python "$PSScriptRoot\gitSwitch.py" $args
Author’s Notes
Hop on the community discord if you have any trouble along the way! If there’s any recurring pain point, I’ll make sure to update the article to address it!
There’s the limitation of it working only for github.com
host — what’s git without
GitHub amirite? — but I figured I should write something before I nerd snipe
myself making the next identity manager with cutting edge user interface
The next blog about this will be a more fleshed out identity management script
that focuses on updating the ~/.ssh/config
file regardless of the host