Generate an Identity
First generate an SSH key:
ssh-keygen -t ed25519 -C "machingclee@gmail.com"
Save the credential to the files named machingclee_github
. Also add this SSH key into our github account.
SSH Host Configuration
Note that the order does matter:
~/.ssh/config Host github.com-machingclee HostName github.com User machingclee PreferredAuthentications publickey IdentityFile /Users/chingcheonglee/.ssh/machingclee_github UseKeychain yes AddKeysToAgent yes # --- Sourcetree Generated --- Host github.com HostName github.com User Ching-Cheong-Lee PreferredAuthentications publickey IdentityFile /Users/chingcheonglee/.ssh/Ching-Cheong-Lee-GitHub UseKeychain yes AddKeysToAgent yes # ----------------------------
If Host github.com
record is put on top, it takes the first priority and other configs will get overriden.
On the other hand, the options
UseKeychain yes AddKeysToAgent yes
makes sure our configuration will be persisted in keychain once they have been used.
Let SSH Agent load the Private key into Memory
For the first time using our account, execute the following to ensure our new SSH Host configuration can be recognized.
ssh-add ~/.ssh/machingclee_github
Due to UseKeychain
and AddKeysToAgent
, we only need to do it once.
Change the Authentication Method (by Different Github Accounts)
-
When we
git clone ...
, the default authentication method recognized by the hostnamegithub.com
is always the default option (which is User Ching-Cheong-Lee in the configuration above). -
To
git clone
using our specified SSH key (namely, identity). we can change the ssh-path by changing the hostname beforehand:git clone git@github.com-machingclee:machingclee/repo-name.gitInstead of using the default
github.com
-
If we did not change the ssh-hostname, we might need to authenticate our
PUSH
request using another account (namely, another hostname) via:git remote set-url origin git@github.com-machingclee:machingclee/repo-name.gitThis will change the config of that repository to using the new host configuration.