Created
April 30, 2025 03:40
-
-
Save musichen/2a43de508b0e0c6c4addc6933a638c51 to your computer and use it in GitHub Desktop.
switch between git user Accounts
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
| #!/bin/zsh | |
| # | |
| # Script to switch between different GitHub accounts | |
| # Usage: gitaccswitch userX OR gitaccswitch userY | |
| # | |
| # Author: Alex Musichen | |
| # Date: 2025-04-19 | |
| set -e # Exit immediately if a command fails | |
| trap 'echo "Error: Script execution failed at line $LINENO"; exit 1' ERR | |
| if [ -z "$1" ]; then | |
| echo "Please provide an account name: musichen or webboxesdeveloper" | |
| exit 1 | |
| fi | |
| echo "========================================" | |
| echo "π SSH KEY CONFIGURATION" | |
| echo "========================================" | |
| # Set SSH keys regardless of git repo (affects all git operations) | |
| case "$1" in | |
| "userX") | |
| echo "Switching to GitHub account: userX ([email protected])" | |
| # Set SSH key | |
| ssh-add -D # Clear all identities first | |
| ssh-add ~/.ssh/id_rsa_userX # Assuming this is the correct key path | |
| # Store git user info for later use | |
| GIT_USER_NAME="User X" | |
| GIT_USER_EMAIL="[email protected]" | |
| ;; | |
| "userY") | |
| echo "Switching to GitHub account: userY ([email protected])" | |
| # Set SSH key | |
| ssh-add -D # Clear all identities first | |
| ssh-add ~/.ssh/id_rsa_userY # Assuming this is the correct key path | |
| # Store git user info for later use | |
| GIT_USER_NAME="User Y" | |
| GIT_USER_EMAIL="[email protected]" | |
| ;; | |
| *) | |
| echo "Unknown account: $1. Available options are: userX, userY" | |
| exit 1 | |
| ;; | |
| esac | |
| echo "β SSH key updated successfully!" | |
| echo "" | |
| echo "========================================" | |
| echo "π§ GIT CONFIGURATION" | |
| echo "========================================" | |
| # Check if we're in a git repository before configuring local settings | |
| if git rev-parse --is-inside-work-tree &>/dev/null; then | |
| echo "π Current directory is a git repository." | |
| echo "Configuring local git repository settings..." | |
| # Configure git user for current repository | |
| git config --local user.name "$GIT_USER_NAME" | |
| git config --local user.email "$GIT_USER_EMAIL" | |
| # Display current git configuration to confirm the change | |
| echo "" | |
| echo "Current Git configuration (local):" | |
| echo "π€ User name: $(git config --local user.name)" | |
| echo "π§ User email: $(git config --local user.email)" | |
| echo "========================================" | |
| else | |
| echo "β οΈ Notice: Not in a git repository." | |
| echo "SSH keys have been updated, but git config remains unchanged." | |
| echo "" | |
| echo "To set repository-specific git config, run this command when inside a git repository." | |
| echo "" | |
| # Show current global git config | |
| echo "Current global git configuration:" | |
| echo "π€ User name: $(git config --global user.name 2>/dev/null || echo "(not set)")" | |
| echo "π§ User email: $(git config --global user.email 2>/dev/null || echo "(not set)")" | |
| echo "" | |
| # Optionally, you could set global config instead | |
| echo "Would you like to update your global git config? (y/N)" | |
| read -r response | |
| if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then | |
| git config --global user.name "$GIT_USER_NAME" | |
| git config --global user.email "$GIT_USER_EMAIL" | |
| echo "" | |
| echo "β Global Git configuration updated:" | |
| echo "π€ User name: $(git config --global user.name)" | |
| echo "π§ User email: $(git config --global user.email)" | |
| else | |
| echo "Global git configuration unchanged." | |
| fi | |
| echo "========================================" | |
| fi | |
| # End of script | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment