create ssh key according the article Set-Up Git
$ ssh-keygen -t ed25519 -C "[email protected]"
$ ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_work
*** -f filename ***
Please refer to github ssh issues for common problems.
for example, 2 keys created at:
~/.ssh/id_ed25519
~/.ssh/id_ed25519_work
then, add these two keys as following
$ ssh-add ~/.ssh/id_ed25519
$ ssh-add ~/.ssh/id_ed25519_work
you can delete all cached keys before
$ ssh-add -D
finally, you can check your saved keys
$ ssh-add -l
Reference: FYI Adding a new SSH key to your GitHub account
$ cd ~/.ssh/
$ touch config
$ vi config
- create
config
if not existed
Then added
# Personal account
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa
# Work account
Host work.github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa_work
$ ssh -T [email protected]
$ ssh -T [email protected]
Uses standard SSH key
git clone [email protected]:rails/rails.git
Uses work SSH key
git clone [email protected]:rails/rails.git
cd rails and modify git config(for display author with work email)
$ git config user.name "work"
$ git config user.email "[email protected]"
then use normal flow to push your code
$ git add .
$ git commit -m "your comments"
$ git push
Setting ./gitconfig
vi ~/.gitconfig
Add this block to bottom line
[includeIf "gitdir:~/work/"]
[user]
name = "work"
email = [email protected]"
[core]
sshCommand = "ssh -i ~/.ssh/id_rsa_work"
To select git work account we have clone repo under ~/work
directory.
That's it! 🎉