By default, the git
command uses the default SSH key pairs to communicate with GitHub.com. When you have different accounts and work on different projects, you need to do some special setup.
Generate a new SSH key specifically for this account:
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/oap_key
-
Copy your public SSH key to the clipboard (for Windows):
cat ~/.ssh/oap_key.pub | clip
For macOS:
pbcopy < ~/.ssh/oap_key.pub
For Linux (with xclip installed):
xclip -sel clip < ~/.ssh/oap_key.pub
-
Go to your GitHub account on github.com, login, and navigate:
- Go to Settings under your account
- Select SSH and GPG keys
- Click New SSH key button, paste your key there
To check your setup is correct, you can run:
ssh -T [email protected]
Configure your SSH to recognize a new profile for GitHub using a specific key:
# Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
# New config for specific account
Host oap.github.com
HostName github.com
User git
IdentityFile ~/.ssh/oap_key
IdentitiesOnly yes
Now your system can recognize oap.github.com
as an alias for github.com
.
ssh -T [email protected]
If everything is set up correctly, you'll see a message from GitHub stating that you've successfully authenticated, but GitHub does not provide shell access.
Clone your repository using the new SSH configuration:
git clone [email protected]:oap/oap_git_test.git
Open the .git/config
file in a text editor and modify it as follows:
[remote "origin"]
url = [email protected]:oap/oap_git_test.git
fetch = +refs/heads/*:refs/remotes/origin/*
To ensure that your configuration works, run:
git fetch origin