Git pushing to remote GitHub repository as wrong user

GitPermissionsGithub

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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 = git@personal.github.com:PERSONAL_GITHUB_USERNAME/project.git

or do it via command line:

git remote set-url origin git@personal.github.com:PERSONAL_GITHUB_USERNAME/project.git

Likewise to your work one:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@work.github.com:your_work_organization/project.git

or again, via command line:

git remote set-url origin git@work.github.com: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 johndoe@example.com

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

Solution 2 - Git

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.

Solution 3 - Git

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);

Solution 4 - Git

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.

Solution 5 - Git

I had the same problem. It turns out I had two accounts on GitHub using the same SSH key and GitHub defaulted to using the wrong account that did not have permission to the repo I was after. I removed the SSH key from the account I did not to use all worked as expected.

You can test which account GitHub is authenticating yourself with:

ssh -T git@github.com

For me, this originally showed the wrong username, but after removing the duplicate SSH key from that account, it then showed the correct username and my pull and push to my repo worked well.

Solution 6 - Git

I had the same issue recently cause I created a new github account. Ive tried the answers above but it didn't help. Then I saw a post somewhere about deleting github from Keychain Access (only if you are using mac). When I git push, it then ask for username and password, and it worked!

Solution 7 - Git

I know this might be a little late, but I was stuck with this for quite some time and finally fixed it like this:

example screenshot

  • Go into keychain access (osX)

  • search git (make sure you have selected All Items)

  • Here you will find the culprit credentials. Delete them.

Hope this helps!

Solution 8 - Git

This is a way to do this: you can use different ssh configurations for different ssh accounts.

Updated on Feb 22:

Check out this link: https://gist.github.com/2351996

Solution 9 - Git

If changing the SSH key associated with the account doesn't work, change the email associated with the account.

Go to Github > Account Settings > Emails and verify the email address you are using to commit matches the email on the account.

To see what email address you're using to commit, run the following command: git config --global user.email. If you need to change the email address that you are using to commit, run git config --global user.email "[email protected]".

Solution 10 - Git

I got the same issue. Below is what happen in my case:

I previously made git to not ask my credential every time I talk with remote repository by this: git config --global credential.helper wincred

I resolved the issue by running the same command with "none" replacing "wincred" git config --global credential.helper none

Then git ask my username/pass again and everything go well

Solution 11 - Git

I ran into this problem as well and none of the above solutions worked even after I deleted my ssh key and made a new one. Turns out ssh-agent was using a cached key, so I had to run killall ssh-agent and then it worked.

Found the solution here. http://fzysqr.com/2012/08/28/quick-tip-wrong-ssh-key-cached-with-github-after-changing-users-and-keys/

Solution 12 - Git

I had this problem as well but none of the other solutions worked for me. It turns out that for work we had created a .netrc file that had entries for github authentication. The git command always used the .netrc, which had my old user name and password. I had to edit the entries in my .netrc file to use the new username and password.

Solution 13 - Git

I have found a temporary solution in which first run killall ssh-agent then add the ssh keys generated for the account you need to use ssh-add ~/.ssh/id_4shameer

This is the one way in which we can work on the multiple github account when we will get the error of type ERROR: Permission to user/repo-git.git denied to username.

Solution 14 - Git

Never had any problems with git till at work they recently connected our macbooks to Active Directory & added a few admin accounts to my machine. However, after that git would work fine till i locked my screen and came back. Then I would get a vague error similar to

No user exists for uid 1927040837
fatal: Could not read from remote repository.

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

I only have one ssh key on this particular machine for my user and am using zsh in my term. The user email and name were correct so that wasn't the issue. Ergo, restarting after every time i lock my machine is futile. The solution for me was to edit my .zshrc file and uncomment the line that exports the ssh-key (which i've never had to do before and have been using zsh for years).

The line should look something like this:

# ssh
export SSH_KEY_PATH="~/.ssh/<your_rsa_id>"

Once you do this just run a reset in terminal and everything works fine.

I hope this helps someone else.

Solution 15 - Git

I would like to add - If you are working on another user's account make sure you add yourself to the collaborators area under the repositories settings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRaz FazView Question on Stackoverflow
Solution 1 - GithellatanView Answer on Stackoverflow
Solution 2 - GitRajan PatilView Answer on Stackoverflow
Solution 3 - GityuttadhammoView Answer on Stackoverflow
Solution 4 - GitWalter MundtView Answer on Stackoverflow
Solution 5 - GitRyanView Answer on Stackoverflow
Solution 6 - Gitflipjs.ioView Answer on Stackoverflow
Solution 7 - GitA.DevView Answer on Stackoverflow
Solution 8 - Gitasksw0rderView Answer on Stackoverflow
Solution 9 - GitBaubView Answer on Stackoverflow
Solution 10 - GitHung CaoView Answer on Stackoverflow
Solution 11 - GithobberwickeyView Answer on Stackoverflow
Solution 12 - GitSteven LambertView Answer on Stackoverflow
Solution 13 - GitmshameerView Answer on Stackoverflow
Solution 14 - GitAndruw HartView Answer on Stackoverflow
Solution 15 - GitDan KellyView Answer on Stackoverflow