Git pushing to remote GitHub repository as wrong user

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.


Go to Control Panel > User Accounts > Credential Manager > Generic Credentials

remove the git credentials. Then run git push. This will prompt to ask for the git credentials. Enter your correct credentials.


You can also just switch to https, rather than ssh. If you use https, it will respect the .git/config settings. So, in .git/config, change:

url = [email protected]:USER/PROJECT.git

to

url = https://[email protected]/USER/PROJECT.git

(these values are on the git project page, click on the SSH and HTTP buttons to ge tthe new values);


github identifies you by the ssh key it sees, not by any setting from git.

Therefore, you need to ensure that your work account's ssh key is not in your keyring when you try to push as your personal account and vice versa. Use ssh-add -l to determine which keys are in your keyring, and ssh-add -d keyfile to remove a key from your keyring.

Also, you may need to check ~/.ssh/config if you have configured it to present certain ssh keys to github. Finally, I don't know how github deals with two accounts having the same ssh public key, so make sure you don't do that.