Skip to content

Instantly share code, notes, and snippets.

@tailwiinder
Created February 24, 2025 10:48
Show Gist options
  • Save tailwiinder/5d6bf9dbbc215dad368163878ab8b207 to your computer and use it in GitHub Desktop.
Save tailwiinder/5d6bf9dbbc215dad368163878ab8b207 to your computer and use it in GitHub Desktop.
ZSH Functions for Managing Multiple GitHub Accounts (SSH)
# Functions to manage multiple GitHub accounts via SSH
# Example for switching to and managing my tailwiinder account
#
# Setup Required:
# 1. Configure SSH hosts in ~/.ssh/config
# 2. Set up SSH keys for your GitHub accounts
# 3. Add these functions to your .zshrc
#
# Usage:
# - clone-tom: Clone a new repository using tailwiinder account
# - git-use-tom: Switch existing repository to use tailwiinder account
# - git-who: Check which GitHub account is configured for current repository
function clone-tom() {
if [ -z "$1" ]; then
echo "Usage: clone-tom <github-url>"
echo "Example: clone-tom https://github.com/username/repo.git"
return 1
fi
# Extract repo path from URL (handles both HTTPS and SSH URLs)
repo_path=$(echo $1 | sed -e 's/.*github.com[/:]//' -e 's/\.git$//')
# Clone with SSH
git clone "[email protected]:${repo_path}.git"
# Enter directory and setup git config
repo_name=$(basename "$repo_path")
cd "$repo_name"
git config user.email "[email protected]"
git config user.name "tailwiinder"
echo "\nRepository cloned and configured for tailwiinder account ✅"
echo "Remote URL: $(git remote get-url origin)"
echo "Git email: $(git config user.email)"
}
function git-use-tom() {
# Update remote URL
current_url=$(git remote get-url origin)
new_url=$(echo $current_url | sed -e 's/github.com[^:]*:/github.com-tailwiinder:/')
git remote set-url origin "$new_url"
# Update git config
git config user.email "[email protected]"
git config user.name "tailwiinder"
echo "\nRepository configured for tailwiinder account ✅"
echo "Remote URL: $(git remote get-url origin)"
echo "Git email: $(git config user.email)"
}
# Alias for checking current git account
alias git-who="echo 'Git User:' && git config user.name && echo 'Git Email:' && git config user.email && echo 'Remote URL:' && git remote get-url origin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment