Working with Multiple GitHub Accounts from the Same Machine

A step by step to link multiple github accounts

Overview

When you need to work with different GitHub accountsβ€”like one for personal projects and another for workβ€”things can get tricky. How do you manage them without running into permission issues? πŸ€”

In this post, we’ll walk through how to configure Git on your machine so you can seamlessly switch between multiple GitHub accounts without any headaches! πŸ€•

Let suppose you have two github accounts, https://github.com/github-work and https://github.com/github-personal. Now you want to setup your machine to easily talk to both the github accounts.

Let’s dive in!🐬

Step 1: Remove Global πŸ‘Œ

This step is recommended, but optional. I will prefer not to have a default user for all projects, but it is up to you. Now we are going to open the global config using the command:

git config --edit --global

Remove all [credential] and [user] configurations:

[user]
[core]
[core]
[core]
  autocrlf = false
  editor = code --wait
[color]
  ui = auto
[credential]
[merge]
  tool = kdiff3
[mergetool "kdiff3"]
  path = C:/Program Files/KDiff3/kdiff3.exe
...

Step 2: Generate SSH KeysπŸ”‘ for all accounts

  • First make sure your current directory is your .ssh folder.

    cd ~/.ssh
    
  • Generating unique ssh key for both accounts:

    ssh-keygen -t ed25519 -C "github-personal@gmail.com" -f "github-personal"
    ssh-keygen -t ed25519 -C "github-work@company.com" -f "github-work"
    

    here:

    • -C stands for comment to help identify your ssh key
    • -f stands for the file name where your ssh key get saved

Step 3: Add Your SSH Keys to the SSH Agent

  • Ensure the ssh-agent is running. If not start it manually:
    # start the ssh-agent in the background
    eval "$(ssh-agent -s)"
    
  • Add your SSH private key to the ssh-agent.
    ssh-add ~/.ssh/github-personal
    ssh-add ~/.ssh/github-work
    

Step 4: Add SSH Keys to GitHub

  • Copy the SSH public key to your clipboard.

    # Copies the contents of the id_ed25519.pub file to your clipboard
    clip < ~/.ssh/github-username.pub
    
  • In the upper-right corner of any page, click your profile photo, then click Settings

  • In the Access section of the sidebar, click SSH and GPG keys.

  • Click New SSH key or Add SSH key.

  • In the Title field, add a descriptive label for the new key. For example, if you’re using a personal laptop, call this key “Personal Laptop”.πŸ’‘

  • Paste your key into the Key field.

  • Click Add SSH key.

Step 4: Create separate directories (one personal and one work)

  • We are going to define a path where our projects are going to live and create a .gitconfig file for each user profile, as many as we need.

    C:
    β”œβ”€β”€ Users 
          β”œβ”€β”€ <username>
                  β”œβ”€β”€ .gitconfig <-- global
    D:              
    β”œβ”€β”€ Repos
          └── Developer/
                  β”œβ”€β”€ personal/
                  β”‚   β”œβ”€β”€ project_1/
                  β”‚   β”œβ”€β”€ project_2/
                  β”‚   β”œβ”€β”€ project_#/
                  β”‚   └── .gitconfig <-- personal
                  └── work/
                      β”œβ”€β”€ project_1/
                      β”œβ”€β”€ project_2/
                      β”œβ”€β”€ project_#/
                      └── .gitconfig <-- work
    
  • Personal:

    # ~/Developer/github-personal/.gitconfig
    
    [core]
        sshCommand = ssh -i ~/.ssh/github-personal -F /dev/null
    [credential]
        username = github-personal
    [user]
        name = github-personal
        email = github-personal@gmail.com
    
  • Work:

    # ~/Developer/github-work/.gitconfig
    
    [core]
        sshCommand = ssh -i ~/.ssh/github-work -F /dev/null
    [credential]
        username = github-work
    [user]
        name = github-work
        email = github-work@company.com
    

Step 5: Update the Global config

  • Now we are going to open the global config using git config --edit --global command and point to the local configs:

    [includeIf "gitdir/i:D:/Repos/Developer/github-personal/"]
        path = D:/Repos/Developer/github-personal/.gitconfig
    
    [includeIf "gitdir/i:D:/Repos/Developer/github-work/"]
        path = D:/Repos/Developer/github-work/.gitconfig
    

Step 6: Voila πŸŽ‰ switch to personal/work folder and work

  • It will take the user configuration profile per path, and you can create or clone projects inside each profile path without dealing with manual configurations and avoid using the amend command to fix mistakes.

Wrapping It Up 🎁

Now you can work with both your personal and work GitHub accounts from the same machine without any issues. When you push or pull, Git will automatically use the correct SSH key and account based on the folder you’re working on!

Avijit Chatterjee
Avijit Chatterjee

Reactive programming enthusiast keen on learning new technologies

Related