duyne

    Tip - Multiple Github Accounts with SSH

    • ssh-key
    • tip

    Small world. Big idea!

    Problem

    I have two Github accounts: duyhenryer (personal) and duydo (for work). I want to use both accounts on same computer (without typing password everytime, when doing git push or pull).

    Step 1: Generating SSH keys

    Generating a new SSH key and adding it to the ssh-agent

    π ~/opt/ ❯ mkdir -p ~/.ssh/me
    π ~/opt/ ❯ ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/me/id_ed25519

    Step 2: Configuring SSH config file

    Edit/Create ssh config file (~/.ssh/config)

    # For work
    Host github.com
       HostName github.com
       IdentityFile ~/.ssh/id_ed25519
       IdentitiesOnly yes
       StrictHostKeyChecking no
    
    # For personal
    Host github.me
       HostName github.com
       IdentityFile ~/.ssh/me/id_ed25519
       IdentitiesOnly yes
       StrictHostKeyChecking no

    Step 3: Adding your SSH key to the ssh-agent

    π ~/opt/ ❯ ssh-add ~/.ssh/id_ed25519
    π ~/opt/ ❯ ssh-add ~/.ssh/me/id_ed25519
    π ~/opt/ ❯ ssh-add -l # Optional

    Step 4: Testing the SSH connection

    Guide readers on how to test the SSH connection to ensure it's working correctly.

    π ~/opt/ ❯ ssh -T [email protected]
    Hi duydo! You've successfully authenticated, but GitHub does not provide shell access.
    
    π ~/opt/ ❯ ssh -T [email protected]
    Hi duyhenryer! You've successfully authenticated, but GitHub does not provide shell access.

    Clone you repo and modify your Git config

    π ~/opt/ ❯ git clone [email protected]:duyhenryer/testrepo.git
    π ~/opt/ ❯ cd testrepo # develop and modify git config
    π ~/opt/ ❯ git config user.name "duyhenryer"
    π ~/opt/ ❯ git config user.email "[email protected]" 

    then use normal flow to push your code

    π ~/opt/ ❯ git add .
    π ~/opt/ ❯ git commit -m "your comments"
    π ~/opt/ ❯ git push

    or you can have global git config for work.

    π ~/opt/ ❯ git config --global user.name "duydo" 
    π ~/opt/ ❯ git config --global user.email "[email protected]"