Skip to content

Instantly share code, notes, and snippets.

@Sotatek-NuiTran
Last active April 27, 2025 00:12
Show Gist options
  • Save Sotatek-NuiTran/a20d6827d874ca5f5b7b01e1a6d8fd01 to your computer and use it in GitHub Desktop.
Save Sotatek-NuiTran/a20d6827d874ca5f5b7b01e1a6d8fd01 to your computer and use it in GitHub Desktop.
Setup SSH keys for multiple github and gitlab account

Setup SSH keys for multiple github and gitlab account

Prepare SSH keys:

Add a new key

$ ssh-keygen -t rsa_new -C "[email protected]"

Start ssh-agent

$ eval `ssh-agent -s`

Clear cached ssh key

$ ssh-add -D

Add key

$ ssh-add ~/.ssh/id_rsa_company
$ ssh-add ~/.ssh/id_rsa_personal
...

Recheck

$ ssh-add -l

Config SSH

Redirect to .ssh folder

$ cd ~/.ssh/

Create config file if it doesn't exist

$ touch config

Edit config file

$ vi config

Add below

# Identity for company account
    Host github-company
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_company
    IdentitiesOnly yes

# Identity for personal account
    Host gitlab-personal
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal
    IdentitiesOnly yes

Clone and update repo

Clone new repo

$ git clone git@github-company:xxx/xxx.git
$ git clone git@gitlab-personal:xxx/xxx.git

Update exist repo by rename with backup

$ cd existing_repo
$ git remote rename origin old-origin
$ git remote add origin git@github-company:xxx/repo_name.git

Update exist repo by change url origin

$ cd existing_repo
$ git remote set-url origin git@github-company:repo/repo.git
@slopeonlinegame
Copy link

If I accidentally add the wrong SSH key to the Retro Bowl College agent after running ssh-add ~/.ssh/id_rsa_company, how can I troubleshoot why GitHub still rejects my push with a permission error?

@Sotatek-NuiTran
Copy link
Author

If I accidentally add the wrong SSH key to the Retro Bowl College agent after running ssh-add ~/.ssh/id_rsa_company, how can I troubleshoot why GitHub still rejects my push with a permission error?

you can check the added key to agent with command:
ssh-add -l

If the wrong key was added, remove it using:
ssh-add -d ~/.ssh/id_rsa_wrong

Then re-add the correct one:
ssh-add ~/.ssh/id_rsa_company

@freddavis980
Copy link

I once spent ages switching between GitHub and GitLab accounts. With these steps, it's a breeze.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment