Skip to content

Instantly share code, notes, and snippets.

@AnthonyLzq
Last active August 12, 2023 06:22
Show Gist options
  • Save AnthonyLzq/6ae670510361109435fa2e62c9a30432 to your computer and use it in GitHub Desktop.
Save AnthonyLzq/6ae670510361109435fa2e62c9a30432 to your computer and use it in GitHub Desktop.
How to manage multiple GitHub accounts

Managing Multiple GitHub Accounts on MacOS/Linux

If you have multiple GitHub accounts and want to manage them from a single MacOS machine, follow these steps:

1. SSH Configuration:

  • MacOS/Linux has an SSH configuration file located in the .ssh directory.
  • Navigate to it by running cd ~/.ssh in your terminal.

2. Generating SSH Keys:

  • If you have, for instance, 2 GitHub accounts (one for work and one personal), you need to create SSH key pairs for each.
  • Run:
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • You'll be prompted to enter a filename to save the key. Ensure it's unique, e.g., user-1, user-2.

3. Register SSH Keys on GitHub:

  • Register the generated SSH keys on the respective GitHub accounts.
  • Follow these steps to do so.

4. Modify the SSH Config File:

  • Go back to the SSH config file in ~/.ssh and modify it as:
    # user1 account
    Host github.com-user1
       HostName github.com
       User git
       IdentityFile ~/.ssh/github-user1
       IdentitiesOnly yes
    
    # user2 account
    Host github.com-user2
       HostName github.com
       User git
       IdentityFile ~/.ssh/github-user2
       IdentitiesOnly yes

5. Clone Your Repository:

  • Run:
    git clone git@github.com-user1:user1/your-repo-name.git your-repo-name_user1

6. Configure Git Identity:

  • Open local git config using git config --local -e and add:
    [user]
        name = user1
        email = user1@gmail.com

7. Ensure Remote URL is in the Right Format:

  • Example:
    git@github.com-user1:user1/your-repo-name.git your-repo-name_user1
  • Set the remote URL using:
    git remote set-url origin git@github.com-user1:user1/your-repo-name.git your-repo-name_user1

Sample .git/config File:

Your local repository's .git/config file would look like:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true

[remote "origin"]
    url = git@github.com-user1:user1/your-repo-name.git
    fetch = +refs/heads/*:refs/remotes/origin/*

[branch "master"]
    remote = origin
    merge = refs/heads/master

[user]
    name = user1
    email = user1@gmail.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment