DEV Community

Cover image for Add a new SSH-Key for GitHub on your computer
Gervais Yao Amoah
Gervais Yao Amoah

Posted on • Edited on

Add a new SSH-Key for GitHub on your computer

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

  1. Open (or create) the SSH config file:
nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

7. Clone or Configure Repositories

For personal repositories, use the default URL:

git clone git@github.com:username/repo.git
Enter fullscreen mode Exit fullscreen mode

For work repositories, use the custom host (github-work) you defined in the SSH config:

    git clone git@github-work:work-username/repo.git
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

For work repositories, set the user locally in each repository:

    git config user.name "Work Name"
    git config user.email "work_email@example.com"
Enter fullscreen mode Exit fullscreen mode

9. Verify the Configuration

Test SSH for each account:
Enter fullscreen mode Exit fullscreen mode
    ssh -T git@github.com
    ssh -T git@github-work
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay