Setting Up Multiple Git Accounts on Machine

setup-multiple-git.mdMarkdown

setup-multiple-git.md

Setting Up Multiple Git Accounts on Mac

Managing multiple Git accounts on the same machine can be simplified with the following steps. This guide ensures you can seamlessly work with personal and work accounts.


1. Create SSH Keys for Each Account

Generate separate SSH key pairs for each GitHub account:

cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]" -f "github-personal"
ssh-keygen -t rsa -C "[email protected]" -f "github-work"
  • Replace the email addresses with the ones associated with your respective GitHub accounts.
  • The -f flag specifies the filenames for the key pairs.

2. Add SSH Keys to the SSH Agent

Add the generated keys to your SSH agent:

ssh-add --apple-use-keychain ~/.ssh/github-personal
ssh-add --apple-use-keychain ~/.ssh/github-work

For Linux or non-Mac environments:

ssh-add ~/.ssh/github-personal
ssh-add ~/.ssh/github-work

3. Add Public Keys to GitHub

Copy each public key and add them to the corresponding GitHub accounts.

pbcopy < ~/.ssh/github-personal.pub # Mac
cat ~/.ssh/github-personal.pub | xclip -selection clipboard # Linux
  1. Go to GitHub SSH Settings for each account.
  2. Click "New SSH Key", paste the public key, and save.

Repeat for the second account:

pbcopy < ~/.ssh/github-work.pub

4. Configure SSH for Multiple Accounts

Edit the SSH config file to define separate host entries for each account:

open -e ~/.ssh/config # Mac
nano ~/.ssh/config    # Linux

Add the following configuration:

# Personal account
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-personal

# Work account
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-work

5. Set Default Git Configurations

Set your global Git username and email for the default (personal) account:

git config --global user.name "Your Personal Name"
git config --global user.email "[email protected]"

For repositories requiring your work account, override the default configuration:

cd /path/to/your/repository
git config user.name "Your Work Name"
git config user.email "[email protected]"

6. Clone Repositories with Correct Accounts

Use the host alias defined in the SSH config:

  • For personal account:

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

    git clone git@github-work:username/repo.git
    

7. Update Existing Repository Remotes

To switch the account used by an existing repository:

git remote set-url origin git@github-work:username/repo.git

8. Test SSH Connection

Verify the SSH setup for each account:

ssh -T github-personal
ssh -T github-work

You should see a welcome message for each respective GitHub account.


9. Optional: Switch Accounts Easily

Check which account is in use for a repository:

git config user.name
git config user.email

Update them as necessary:

git config user.name "Your Work Name"
git config user.email "[email protected]"
Updated: 1/21/2025