Git pushing to remote GitHub repository as wrong user

Raz Faz picture Raz Faz · Jan 12, 2011 · Viewed 68.7k times · Source

I have a work GitHub account and a personal one. First I used the personal one for test projects, then I moved on and did a repository with the other account on the same computer.

Now I wanted to create a new repository on my personal account again, I changed the global and local user.name, and did a new ssh key pair, entered in the GitHub setup page. Then I set up the directory

git init
git remote add origin <url>
git push origin

but that now tells me

ERROR: Permission to personaluser/newrepo.git denied to

I have no idea how the other account is connected to this one. .git/config shows no workusername related things.

If you're using Windows 10 take your time to read the Rajan's answer.

Answer

hellatan picture hellatan · Sep 15, 2012

this sounds very similar to my current work set up. it seems that you already have set up your separate ssh-keys so you also need to create a ~/.ssh/config file and populate it with information similar to this:

Host work.github.com
    HostName github.com
    User WORK_GITHUB_USERNAME
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_work_rsa
    IdentitiesOnly yes

Host personal.github.com
    HostName github.com
    User PERSONAL_GITHUB_USERNAME 
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_personal_rsa
    IdentitiesOnly yes

Every property sounds pretty self explanatory but the IdentitiesOnly one. I won't try to explain what that is for, but that is in my current setup and works fine.

It's also worth noting that the Host URL is just a pointer to grab the correct user settings and does not have any affect on getting the files correctly to your target HostName url.

Now you just need to make sure your origin (or any remote in general) url match the correct Host url in your respective repos depending on your user name. If you already have existing personal repos, you can edit that repo's .git/config file in your text editor:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]:PERSONAL_GITHUB_USERNAME/project.git

or do it via command line:

git remote set-url origin [email protected]:PERSONAL_GITHUB_USERNAME/project.git

Likewise to your work one:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]:your_work_organization/project.git

or again, via command line:

git remote set-url origin [email protected]:your_work_organization/project.git

Of course, you can always set one of your Host urls in your ~/.ssh/config file as just

Host github.com

I only used work.github.com to see the config relationships easier.

Once these are all set, you should be able to push to each respective remote.

EDIT

One thing to note that I just found out myself is that if you ever set global git config values for your user.email value (and i'm guessing user.name would send a different value as well), git will show your commits as that email user. To get around this, you can override the global git config settings within your local repository:

$ git config user.name "John Doe"
$ git config user.email [email protected]

This should now send up commits as the correct user for that repo.