This is working for different hosts like github.com & bitbucket.org.
If it is required to manage multiple accounts from the same host like github.com activate the key that is currently required by the following commands (eg. for username_2):
$ ssh-add -D //removes all ssh entries from the ssh-agent
$ ssh-add ~/.ssh/id_rsa_username_2 // Adds the relevant ssh key
Found here (step 5): https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/
create ssh key with non default location
$ ssh-keygen -t rsa -C "your_email@youremail.com"
for example, 2 keys created at:
~/.ssh/id_rsa_username_1
~/.ssh/id_rsa_username_2
then, add these two keys to the ssh agent as following
$ ssh-add ~/.ssh/id_rsa_username_1
$ ssh-add ~/.ssh/id_rsa_username_2
you can delete all cached keys before
$ ssh-add -D
finally, you can check your saved keys
$ ssh-add -l
$ cd ~/.ssh/
$ touch config
$ subl -a config
Note: If ~/.ssh/
does not exisit. Run ssh
for the first time and it will be created.
https://linuxize.com/post/using-the-ssh-config-file/
Then add
#username_1 account
Host github.com:username_1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_username_1
#username_2 account
Host github.com:username_2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_username_2
When working on a repository located in another users git account, this should also be registered in the ssh config file. When the ssh URL of the repo looks like this
git@bitbucket.org:different_username/repository_name.git
and your Git account "username_1" is allowed to edit this, it should be configured like this
#different_username account
Host github.com:different_username
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_username_1
Visit: https://github.com/settings/keys
Help: https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account
Visit: https://bitbucket.org/account/settings/ssh-keys/
Help: https://support.atlassian.com/bitbucket-cloud/docs/set-up-an-ssh-key/
clone your repo
$ git clone git@github.com:username_1/gfs.git gfs_username_2
cd into repo and modify the local git config
$ git config user.name "username_2"
$ git config user.email "username_2@gmail.com"
or you can change the global git config
$ git config --global user.name "username_2"
$ git config --global user.email "username_2@gmail.com"
$ git remote set-url origin git@<host>:<username>/<repo>
This is the same as editing your .git/config file.
host-in-ssh-config -> "github.com-username_1"
Then use normal flow to push your code
$ git add .
$ git commit -m "your comments"
$ git push