Skip to content

Instantly share code, notes, and snippets.

@imdariful
Last active November 18, 2024 04:10
Show Gist options
  • Save imdariful/02d79ada4209370721af3d21a5b5aef9 to your computer and use it in GitHub Desktop.
Save imdariful/02d79ada4209370721af3d21a5b5aef9 to your computer and use it in GitHub Desktop.
Setup multiple account for github
#!/bin/bash
# Function to handle errors
handle_error() {
echo "Error: $1. Rolling back changes..."
restore_backup
exit 1
}
# Function to create a backup of critical files (only if they exist)
create_backup() {
echo "Checking for existing configuration files to backup..."
# Backup ~/.ssh/config if it exists
if [ -f ~/.ssh/config ]; then
echo "Backing up SSH config..."
cp ~/.ssh/config ~/.ssh/config.backup || handle_error "Failed to backup SSH config."
else
echo "No ~/.ssh/config file found, skipping backup."
fi
# Backup ~/.gitconfig if it exists
if [ -f ~/.gitconfig ]; then
echo "Backing up Git config..."
cp ~/.gitconfig ~/.gitconfig.backup || handle_error "Failed to backup Git config."
else
echo "No ~/.gitconfig file found, skipping backup."
fi
echo "Backups created successfully (if files existed)."
}
# Function to restore files from backups
restore_backup() {
echo "Restoring from backup..."
# Restore ~/.ssh/config from backup if it exists
if [ -f ~/.ssh/config.backup ]; then
cp ~/.ssh/config.backup ~/.ssh/config || echo "No backup found for SSH config."
fi
# Restore ~/.gitconfig from backup if it exists
if [ -f ~/.gitconfig.backup ]; then
cp ~/.gitconfig.backup ~/.gitconfig || echo "No backup found for Git config."
fi
echo "Restoration complete."
}
# Function to check if SSH agent is running and start it if not
check_and_start_ssh_agent() {
if ! ssh-add -l &>/dev/null; then
echo "SSH agent is not running. Starting SSH agent..."
eval "$(ssh-agent -s)" || handle_error "Failed to start SSH agent."
else
echo "SSH agent is already running."
fi
}
# Function to check if directory exists and create it if not
check_and_create_directory() {
if [ ! -d "$1" ]; then
echo "Directory $1 does not exist. Creating it..."
mkdir -p "$1" || handle_error "Failed to create directory $1."
else
echo "Directory $1 already exists."
fi
}
# 1. Check if ~/.ssh directory exists, if not, create it
echo "Checking if ~/.ssh directory exists..."
check_and_create_directory "$HOME/.ssh"
# 2. Create backups of critical files only if they exist
create_backup
# 3. Generate SSH Keys
echo "Generating SSH keys for GitHub personal and work accounts..."
cd "$HOME/.ssh" || handle_error "Failed to change directory to ~/.ssh"
# Generate SSH keys for GitHub Personal and Work accounts
if [ ! -f ~/.ssh/github-work ]; then
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/github-work || handle_error "Failed to generate GitHub work SSH key."
fi
if [ ! -f ~/.ssh/github-personal ]; then
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/github-personal || handle_error "Failed to generate GitHub personal SSH key."
fi
# 4. Add SSH Keys to the SSH agent
echo "Adding SSH keys to the agent..."
ssh-add ~/.ssh/github-personal || handle_error "Failed to add GitHub personal key to SSH agent."
ssh-add ~/.ssh/github-work || handle_error "Failed to add GitHub work key to SSH agent."
# 5. Update SSH config for GitHub
echo "Updating SSH config for GitHub personal and work accounts..."
if [ -f ~/.ssh/config ]; then
echo -e "\n# Github Personal : imdariful\nHost github.com-personal\nHostName github.com\nUser git\nIdentityFile ~/.ssh/github-personal\n\n# Github Work : ariful-selise\nHost github.com-work\nHostName github.com\nUser git\nIdentityFile ~/.ssh/github-work" >> ~/.ssh/config
else
echo -e "# Github Personal : imdariful\nHost github.com-personal\nHostName github.com\nUser git\nIdentityFile ~/.ssh/github-personal\n\n# Github Work : ariful-selise\nHost github.com-work\nHostName github.com\nUser git\nIdentityFile ~/.ssh/github-work" > ~/.ssh/config
fi
# 6. Check if Code directory exists and create it if not
check_and_create_directory "$HOME/Code"
check_and_create_directory "$HOME/Code/personal"
check_and_create_directory "$HOME/Code/work"
# 7. Create Git directories and configuration files
echo "Setting up Git configuration files..."
echo -e "# ~/.gitconfig\n\n[includeIf \"gitdir:$HOME/Code/personal\"]\n path = $HOME/Code/personal/.gitconfig-personal\n\n[includeIf \"gitdir:$HOME/Code/work\"]\n path = $HOME/Code/work/.gitconfig-work\n\n[core]\n excludesfile = ~/.gitignore # valid everywhere\n\n[core]\n autocrlf = false\n editor = nano\n[init]\n defaultBranch = main" > ~/.gitconfig || handle_error "Failed to create main Git config file."
# Writing the personal Git config file
echo -e "# $HOME/Code/personal/.gitconfig.personal\n\n[user]\n email = [email protected]\n name = Ariful Islam\n\n[github]\n user = \"imdarifulrony\"\n\n[core]\n sshCommand = \"ssh -i ~/.ssh/github-personal\"" > "$HOME/Code/personal/.gitconfig-personal" || handle_error "Failed to create personal Git config file."
# Writing the work Git config file
echo -e "# $HOME/Code/work/.gitconfig.work\n\n[user]\n email = [email protected]\n name = Ariful Islam\n\n[github]\n user = \"ariful-selise\"\n\n[core]\n sshCommand = \"ssh -i ~/.ssh/github-work\"" > "$HOME/Code/work/.gitconfig-work" || handle_error "Failed to create work Git config file."
# 8. Reload Bash configuration to apply any changes (if necessary)
echo "Reloading Bash configuration..."
source ~/.bashrc || handle_error "Failed to reload Bash configuration."
# 9. Final success message
echo "Setup complete! SSH keys, Git configurations, and directories have been set up successfully."
# 10. Display SSH keys for the user to add to GitHub in a neat table
echo -e "\nNow, please add the following SSH keys to your GitHub account settings:\n"
echo -e "+-------------------+-------------------------------------------------------------+"
echo -e "| Account Name | SSH Key |"
echo -e "+-------------------+-------------------------------------------------------------+"
echo -e "| GitHub Personal | $(cat ~/.ssh/github-personal.pub) |"
echo -e "| GitHub Work | $(cat ~/.ssh/github-work.pub) |"
echo -e "+-------------------+-------------------------------------------------------------+"
echo -e "\nTo do this, follow these steps:\n"
echo -e "1. Open GitHub in your browser.\n"
echo -e "2. Go to **Settings** > **SSH and GPG keys**.\n"
echo -e "3. Click **New SSH key**.\n"
echo -e "4. Copy and paste the respective key from above into the SSH key field.\n"
echo -e "5. Click **Add SSH key**.\n"
echo -e "\nOnce added, you will be able to authenticate using these SSH keys with your respective GitHub accounts."
@imdariful
Copy link
Author

  1. Run eval "$(ssh-agent -s)"
  2. curl -sL https://gist.githubusercontent.com/imdarifulrony/02d79ada4209370721af3d21a5b5aef9/raw/c9ec078dfc29ec2726aefe06336c81f76f21ce2f/multiple-github-account-setup.sh | bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment