The commands should be run in a bash
command line.
1. Generate SSH Key:
Generate your SSH key using the following command:
$ ssh-keygen -t ed25519 -C "username@email.com"
This command creates a new SSH key using the provided email address as a label. The keys are saved in the default location (~/.ssh/id_ed25519
for the private key and ~/.ssh/id_ed25519.pub
for the public key).
2. Add SSH Key to SSH Agent:
To use the newly generated SSH key, you should add it to the SSH agent. Run the following commands:
# Start the SSH agent
$ eval "$(ssh-agent -s)"
# Add your private key to the SSH agent
$ ssh-add ~/.ssh/id_ed25519
3. Copy the SSH Key to the Clipboard:
To add the SSH key to your GitHub account, you need to copy the public key to the clipboard:
$ cat ~/.ssh/id_ed25519.pub | pbcopy # for macOS
# OR
$ cat ~/.ssh/id_ed25519.pub | xclip -sel clip # for Linux
# OR
$ clip < ~/.ssh/id_ed25519.pub # for Windows
If you are using Windows, you can open the public key file in a text editor and manually copy its content.
4. Add SSH Key to GitHub:
- Open your GitHub account in a web browser.
- Go to Settings > SSH and GPG keys > New SSH key.
- Add a title (e.g., "My SSH Key") and paste your key into the "Key" field.
- Click on Add SSH key.
PS: If you are adding this as an additional account on your computer, skip to the step 6.
5. Verify Connection:
To verify that everything is set up correctly, run the following command:
$ ssh -T git@github.com
If everything is configured properly, you should see a message indicating a successful connection.
Now, you should be able to clone, push, and pull repositories from your GitHub account using SSH without entering your username and password each time.
6. Configure SSH for Multiple Accounts
You need to create an SSH configuration file to handle multiple accounts.
- Open (or create) the SSH config file:
nano ~/.ssh/config
- Add the following configuration:
# Personal GitHub Account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# Work GitHub Account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
7. Clone or Configure Repositories
For personal repositories, use the default URL:
git clone git@github.com:username/repo.git
For work repositories, use the custom host (github-work) you defined in the SSH config:
git clone git@github-work:work-username/repo.git
8. Set the Global or Local Git User
For personal repositories, your global Git configuration is likely already set:
git config --global user.name "Personal Name"
git config --global user.email "personal_email@example.com"
For work repositories, set the user locally in each repository:
git config user.name "Work Name"
git config user.email "work_email@example.com"
9. Verify the Configuration
Test SSH for each account:
ssh -T git@github.com
ssh -T git@github-work
You should see a success message for each account.
Optional: Push to the Correct Account
When pushing code, ensure the remote URL matches the correct host (e.g., github-work). You can check or update it using:
git remote -v
git remote set-url origin git@github-work:work-username/repo.git
Top comments (0)