Setting Up Multiple Git Accounts¶
Step 1: Create SSH Keys¶
If .ssh directory is not present, check with ls -a and create it:
Generate the first SSH key for your personal account:
When prompted for the file in which to save the key, type the full path:
Save as:
Verify with:
You should see:
id_ed25519_personalid_ed25519_personal.pub
Now generate the second SSH key for your work account:
Save as:
If you set a passphrase (for securing the keys), start the SSH agent to bind the terminal to the credentials:
Step 2: Add Keys to SSH Agent¶
Step 3: Add Keys to GitHub¶
Copy the public keys:
Or display them:
Add each key to the respective GitHub account: - Go to GitHub → Settings → SSH and GPG Keys → New SSH Key (as Authentication Key)
Step 4: Configure SSH (Very Important)¶
Edit the SSH config file:
Open it with:
Add the following:
# Personal
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
# Work
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
# Default
Host github
HostName github.com
User git
IdentityFile ~/.ssh/current_github_key
Save and exit:
-
Ctrl + X
-
Y
-
Enter
Step 5: Test the SSH Connections¶
Test the personal account:
Expected output:
Test the work account:
Expected output:
Step 6: Create Smart Git Aliases¶
These aliases switch your Git identity and remote URL for each account.
git config --global alias.use-personal '!f() { git config user.name "your-name"; git config user.email "your-personal-email"; url=$(git remote get-url origin); url=${url/github.com/github-personal}; url=${url/github-work/github-personal}; git remote set-url origin $url; }; f'
git config --global alias.use-work '!f() { git config user.name "your-name"; git config user.email "your-work-email"; url=$(git remote get-url origin); url=${url/github.com/github-work}; url=${url/github-personal/github-work}; git remote set-url origin $url; }; f'
Create switch commands (global)
[OPTIONAL - ONE CAN SKIP THIS SWITCH COMMANDS IF IN DEFAULT CASE IN STEP 4 IDENTITY FILE IS SET TO PERSONAL OR WORK ONE]
git config --global alias.ssh-personal '!ln -sf ~/.ssh/id_ed25519_personal ~/.ssh/current_github_key'
Step 7: Now before doing anything on github switch to the correct intented github account¶
[FOR DEFAULT CASE]
Switch to personal account in a repository:
Switch to work account:
check which account is correctly default is pointing
Second method - [TO BE USED IN GIT INITIALISED REPO]¶
Switch to personal one:
Switch to work account:
Check your current Git identity (used for commits):
Verify the remote URL anytime:
It should show something like:
Note:
-
git ssh-personal/git ssh-work: Switch the default SSH key before cloning a repository -
git use-personal/git use-work: Switch Git identity and remote URL within an already initialized repository -
git remote -v: Verify which account the current repository is configured with