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
orAdd 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!