Skip to content

Instantly share code, notes, and snippets.

@rupeshtiwari
Last active October 16, 2024 12:53
Show Gist options
  • Save rupeshtiwari/c8e3b54471746b89ccfec975f65fecfc to your computer and use it in GitHub Desktop.
Save rupeshtiwari/c8e3b54471746b89ccfec975f65fecfc to your computer and use it in GitHub Desktop.
git settings for 2 profiles switch between gits

Let's walk through the full steps to generate a new personal_rsa key for your personal projects (if it doesn't already exist), set up the SSH configuration for both accounts, and smoothly switch between them.

Step 1: Check for Existing SSH Keys

First, let's check if you already have a personal SSH key, such as personal_rsa or id_rsa.

Run this command to list the SSH keys in your ~/.ssh/ directory:

ls ~/.ssh/

You should see files named something like id_rsa, id_rsa.pub, personal_rsa, personal_rsa.pub, or fullstackmaster.

  • If you see personal_rsa, you already have a personal key and can skip to Step 3.
  • If you don't see personal_rsa, continue to the next step to generate it.

Step 2: Generate a New SSH Key (personal_rsa)

If you don’t have an SSH key for your personal projects, you can generate one as follows:

  1. Run the following command to generate a new SSH key pair named personal_rsa:

    ssh-keygen -t rsa -b 4096 -C "[email protected]"
  2. When asked where to save the key, type the following path:

    /Users/rupesh/.ssh/personal_rsa
  3. If prompted for a passphrase, either choose a secure passphrase or press Enter to leave it empty.

After this, your personal_rsa private key and personal_rsa.pub public key will be created in the ~/.ssh/ directory.

Step 3: Add Your SSH Key to the SSH Agent

  1. Start the SSH agent if it's not already running:

    eval "$(ssh-agent -s)"
  2. Add your new personal_rsa key to the SSH agent:

    ssh-add ~/.ssh/personal_rsa

Step 4: Add Your SSH Key to GitHub

Next, you need to add the newly generated public key (personal_rsa.pub) to your GitHub account.

  1. Copy the public key to your clipboard:

    cat ~/.ssh/personal_rsa.pub
  2. Go to GitHub SSH and GPG keys settings.

  3. Click New SSH Key, give it a descriptive title (e.g., "Personal RSA"), and paste the copied key.

  4. Click Add SSH Key.

Step 5: Set Up SSH Config for Multiple Keys

Now that both your fullstackmaster and personal_rsa keys are ready, let’s configure SSH to switch between them automatically based on the repository.

  1. Open or create the ~/.ssh/config file using vim:

    vim ~/.ssh/config
  2. Add the following configuration for both your fullstackmaster1 and personal GitHub accounts:

    # FullStackMaster GitHub account
    Host github-fullstackmaster
        HostName github.com
        User git
        IdentityFile ~/.ssh/fullstackmaster
    
    # Personal GitHub account
    Host github-personal
        HostName github.com
        User git
        IdentityFile ~/.ssh/personal_rsa
  3. Save and exit vim by pressing Esc, then type :wq, and press Enter.

Step 5: Update the Git Remote URLs

You need to ensure the correct remote URLs are set for each repository.

For Professional Repositories:

Update the remote URL for your professional repositories to use the github-fullstackmaster host:

git remote set-url origin git@github-fullstackmaster:FullStackMaster1/repo-name.git

For Personal Repositories:

Update the remote URL for your personal repositories to use the github-personal host:

git remote set-url origin git@github-personal:rupeshtiwari/repo-name.git

Step 6: Test the Configuration

To test the setup, check the SSH connection for both accounts:

FullStackMaster Account:

ssh -T git@github-fullstackmaster

Personal Account:

ssh -T git@github-personal

You should see successful authentication messages for both accounts.

Step 7: Push Code to Respective Accounts

Now, when you push code, Git will automatically use the correct SSH key based on the repository's remote URL.

For professional repositories:

git push origin main

For personal repositories:

git push origin main

By setting up this configuration, you can easily switch between your professional and personal GitHub accounts without manually changing SSH keys.

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