DEV Community

Hastycode Andreh
Hastycode Andreh

Posted on

πŸ” Fixing SSH Conflicts: Using a Separate SSH Key for GitHub

When working on multiple environments, you might run into this error when pushing to GitHub:

identity_sign: private key /home/hastycode-andreh/.ssh/id_ed25519 contents do not match public
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Enter fullscreen mode Exit fullscreen mode

This usually happens when:

  • You already have an SSH key (id_ed25519) used for something else (like a server), and
  • You're trying to use GitHub with that same key, but GitHub doesn't recognize it.

πŸ’‘ Solution: Create a dedicated SSH key for GitHub, and tell your system to use it only for GitHub connections.


βœ… Step 1: Generate a New SSH Key for GitHub

Let’s create a new key and name it something distinct, like id_ed25519_github.

ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

You’ll see:

Enter file in which to save the key (/home/your-name/.ssh/id_ed25519): 
Enter fullscreen mode Exit fullscreen mode

Respond with:

/home/hastycode-andreh/.ssh/id_ed25519_github
Enter fullscreen mode Exit fullscreen mode

This generates two files:

  • ~/.ssh/id_ed25519_github (private key)
  • ~/.ssh/id_ed25519_github.pub (public key)

βœ… Step 2: Add the Key to Your SSH Agent

Start the SSH agent if it's not already running:

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

Then add your new GitHub key:

ssh-add ~/.ssh/id_ed25519_github
Enter fullscreen mode Exit fullscreen mode

βœ… Step 3: Add Your Public Key to GitHub

  1. Copy your public key:
cat ~/.ssh/id_ed25519_github.pub
Enter fullscreen mode Exit fullscreen mode
  1. Go to GitHub SSH keys page

  2. Click "New SSH Key", give it a name like β€œAndreh Laptop GitHub Key”, and paste the key.


βœ… Step 4: Create a Custom SSH Config for GitHub

Now, tell your system to use the GitHub key only when talking to GitHub.

Edit or create your SSH config file:

nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Add this:

# GitHub account
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github
  IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ This ensures your original server SSH key is untouched.


βœ… Step 5: Test the SSH Connection

Let’s verify everything is working:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

Expected output:

Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

Perfect! πŸŽ‰ You're now pushing to GitHub securely using a separate SSH key.


βœ… Optional: Clean Up Your SSH Agent (If Needed)

If your old key is causing conflicts, you can remove it:

ssh-add -d ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

This ensures your system prioritizes the GitHub key when needed.


🧠 Bonus: Multiple GitHub Accounts?

If you're juggling both personal and work GitHub accounts, you can add an alias in your SSH config:

# Personal GitHub
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal
  IdentitiesOnly yes

# Work GitHub
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

Then in Git remotes, use the alias:

git remote set-url origin git@github.com-personal:username/repo.git
Enter fullscreen mode Exit fullscreen mode

πŸš€ Wrapping Up

This setup gives you a clean and flexible way to manage multiple SSH keys on the same machine β€” no more conflicts between GitHub and your server environments.

Top comments (0)