DEV Community

Cover image for Cool Tools: Use multiple GitHub Accounts on a mac
Amburi Roy
Amburi Roy

Posted on • Originally published at amburi.hashnode.dev

Cool Tools: Use multiple GitHub Accounts on a mac

To use multiple GitHub accounts on the same machine, we must configure our Git settings properly. Here are the steps to help set up using work and personal email addresses.

Set-up

  1. Generate SSH Keys: Using the command,

    • For work: ❯ ssh-keygen -t ed25519 -C "<work_git_email>" -f ~/.ssh/<work_ssh_file_name>
    • For personal: ❯ ssh-keygen -t ed25519 -C "<personal_git_email>" -f ~/.ssh/<personal_ssh_file_name>

    and follow the prompts and choose a secure passphrase.

  2. Add SSH Keys to GitHub/GitLab:

    • Go to GitHub/GitLab account settings ➡️ SSH and GPG keys ➡️ New SSH key
    • Then copy the contents of the public key file (usually ~/.ssh/<xxxx>.pub) and paste it into the key field. ❯ cat ~/.ssh/<xxxx>.pub
  3. Configure Git for Multiple Emails:

    • For work: git config --global user.email "<work_git_email>"
    • For personal: git config --global user.email "<personal_git_email>"
  4. Add and Configure the Config File: Create a new config file if it does not exist.

    • code ~/.ssh/config
    • Replacing the placeholders with the actual paths to your private keys and add the following configuration:
    # work account
    Host github-work
        HostName github.com
        User git
        IdentityFile ~/.ssh/<work_ssh_file_name>
    
    # personal account
    Host github-personal # Change the name of the host as you like
        HostName github.com # gitlab.com or github.com
        User git
        IdentityFile ~/.ssh/<personal_ssh_file_name> # Change with actual path
    

Yoohoo! Set up done…

Clone Repositories

Now, clone repositories using the custom host aliases defined in the .ssh/config file:

  • For work: git clone git@github-work:username/repository.git
  • For personal: git clone git@github-personal:username/repository.git

Switch GitHub Accounts Seamlessly

Here's how you can set up aliases for switching accounts quickly:

  1. Open your shell configuration file:code ~/.zshrc or code ~/.bashrc

    Note I have VSCode configured to open files in VSCode with the code command. If it's not working for you, feel free to use vi or nano.

  2. Add the aliases:

    • alias use-work='git config user.email "<work_git_email>" && git config user.name "Your Name"'
    • alias use-personal='git config user.email "<personal_git_email>" && git config user.name "Your Other Name"'

    Copy these lines into configuration file.

  3. Save and apply changes:

    • Save the change
    • Apply the change: source ~/.zshrc
  4. Using the aliases:
    Now, you can use the aliases to switch between accounts.

    • If you want to switch to the work account: use-work
    • Switch back to the personal account: use-personal

Check Current GitHub Account Configuration:

  • Check the current configuration for the repository using the following commands: git config user.email && git config user.name I suggest you add an alias.
    • code ~/.zshrc or code ~/.bashrc
    • Add alias which-git='git config user.email && git config user.name'
    • Save the change
    • Apply the change: source ~/.zshrc
  • Then use the command which-git to see the current activated git user

Push to Repositories

  1. Switch Account
  2. Perform Git Operations

Is it helpful to use? Let me know!

Top comments (0)