How I Fixed the “Permission Denied” Error with Multiple GitHub Accounts


For a long time, I only used Atlassian tools like Bitbucket for work. Managing my accounts there was easy. Later, I joined a company that used GitHub for everything. I set up my work computer, added my company SSH key, and started working.

Everything was fine until I tried to work on my own personal project. I typed a simple git push, and I got this big error:

git push
ERROR: Permission to zudheer/site.git denied to ksudheer.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

The problem was that GitHub saw I was trying to change my personal code (zudheer/site.git) using my work account (ksudheer). Because the accounts did not match, it blocked me.

I needed a way to use both my work account and my personal account on the same computer without constantly logging in and out or copying passwords.

After doing some research, I found a great trick using the SSH config file. Here is exactly how I fixed the problem for good.

1. Make Two Separate SSH Keys

First, I needed two different “keys” on my computer. One key tells GitHub, “I am working for the company,” and the other key tells GitHub, “I am working on my personal stuff.”

I opened my terminal and created them like this:

For the company account:

ssh-keygen -t ed25519 -C "your-company-email@company.com" -f ~/.ssh/id_company_key

For the personal account:

ssh-keygen -t ed25519 -C "your-personal-email@gmail.com" -f ~/.ssh/id_sudheer

Note: After you make these keys, you have to copy the public parts (the .pub files) and paste them into the SSH and GPG keys section on the GitHub website for each account.

2. Set Up the SSH Config File

This is the most important step. I had to tell my computer which key to use at the right time.

I opened a special file called the SSH config file:

nano ~/.ssh/config

Then, I pasted this text into it:

# Company Account (This is the default)
Host github.com
  HostName github.com
  User git
  IdentityFile /home/sudheer/.ssh/id_company_key

# Personal Account
Host github.com-zudheer
  HostName github.com
  User git
  IdentityFile /home/sudheer/.ssh/id_sudheer

Here is what this means in plain English:

  • If I use the normal github.com address, my computer will use my work key.
  • If I use the made-up address github.com-zudheer, my computer will still go to GitHub, but it will use my personal key.

3. Update the Personal Folders

Because of the change in Step 2, I had to change how my personal projects connect to GitHub. Instead of the normal link (git@github.com:zudheer/site.git), I now have to use the made-up link (git@github.com-zudheer:zudheer/site.git).

I already had a lot of personal projects downloaded in one main folder. I did not want to open every single folder and change the link by hand.

Instead, I wrote a small script to do it all at once. If you run this code inside your main folder, it will go through all your project folders and automatically update the links for you:

for d in */ ; do
  if [ -d "$d.git" ]; then
    echo "Updating link for folder: $d"
    cd "$d"
    
    # Get the old GitHub link
    OLD_URL=$(git remote get-url origin)
    
    # Swap out the normal address for the new personal one
    NEW_URL=${OLD_URL/github.com/github.com-zudheer}
    
    # Save the new link
    git remote set-url origin "$NEW_URL"
    
    echo "Changed from $OLD_URL"
    echo "          to $NEW_URL"
    echo "----------------------------------------"
    cd ..
  fi
done

4. Fix Your Name and Email

There was one last thing to fix. My computer was set up to put my company email on all my code changes (commits). I needed it to use my personal email when I was working on my own projects.

I just opened my terminal, went into my personal project folder, and typed these two lines:

git config user.name "Sudheer"
git config user.email "your-personal-email@gmail.com"

The Result

Since I set this up, I have not seen that “Permission denied” error again. I can do my company work all day, and then easily push my personal code at night on the exact same computer. It just works automatically in the background.

Comments

Popular posts from this blog

Fixing ‘fatal: early EOF’ and ‘index-pack failed’ Errors While Cloning Large GitHub Repositories

Building a Simple HTTP Server in Python: A Step-by-Step Guide

From Mistakes to Mastery: How SSH Config Rescued Me from Command Catastrophes in Production