Using different GitHub users on same machine

Here’s a scenario – for 99% of time, I’m using my work account for GitHub. That means that out of 90 GitHub repositories that I have cloned, 88 of them are work related (numbers are made up!).

What that further means is that, I almost never need to push stuff to my personal repositories.

Well, that was true until today. Today I had to push some stuff to a new public repo I created (PHP8 Machine Learning demo project) and, when I tried pushing, I kept getting following error:

$ git push -u origin main
 Pushing to github.com:MihailoJoksimovic/php8-machine-learning.git
 ERROR: Permission to MihailoJoksimovic/php8-machine-learning.git denied to jaggaer-mjoksimovic.
 fatal: Could not read from remote repository.
 Please make sure you have the correct access rights
 and the repository exists.

Bummer!

To make long story short, for those looking for quick solution, here it comes. What I finally figured I need to do, is:

  1. Generate NEW public key (apparently my public account never used public key in the first place)
  2. Modify core.sshCommand config to use my newly generated key
  3. Profit!

In terms of code:

$ ssh-keygen
 Generating public/private rsa key pair.
 Enter file in which to save the key (/Users/mixa/.ssh/id_rsa): /Users/mixa/.ssh/tinzey_at_gmail_github_key
 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:
 Your identification has been saved in /Users/mixa/.ssh/tinzey_at_gmail_github_key.
 Your public key has been saved in /Users/mixa/.ssh/tinzey_at_gmail_github_key.pub.

$ git config sshCommand "ssh -i ~/.ssh/tinzey_at_gmail_github_key -F /dev/null"

As you can see, this did the trick:

$ git push -u origin main
 Pushing to github.com:MihailoJoksimovic/php8-machine-learning.git
 To github.com:MihailoJoksimovic/php8-machine-learning.git
  = [up to date]      main -> main
 Branch 'main' set up to track remote branch 'main' from 'origin'.
 updating local tracking ref 'refs/remotes/origin/main'
 Everything up-to-date

And that seems to be all there is.

I’ve literally tried everything before that – modifying SSH hostname, changing this and that, but frankly, this was the only one that worked.

I’m guessing that you could, probably, achieve similar thing with GPG signature, but I haven’t tried that one out.

Enjoy!

One thought on “Using different GitHub users on same machine

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top