Git pushing to remote GitHub repository as wrong user

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Git pushing to remote GitHub repository as wrong user



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


user.name


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.


.git/config


workusername



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





For Windows 10, answer below. Remove credentials from credentials manager. Wasted 2 hours of time.
– xenteros
Jun 14 '17 at 5:31






@xenteros Please post this as an answer. I've lost 1h and you saved me another 1h.
– lexicore
Aug 11 at 10:27




16 Answers
16



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:


ssh-keys


~/.ssh/config


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.


IdentitiesOnly



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.


Host URL


HostName



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:


origin


remote


Host


.git/config


[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


~/.ssh/config


Host github.com



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


work.github.com



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:


user.email


user.name


$ 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.





This works swell :)
– yuttadhammo
Jun 15 '13 at 2:26





Works like a charm. Thank you!
– NorthBridge
Nov 25 '13 at 14:23





This site explains the ssh config file more fully for anyone who is interested.
– Phil
Dec 12 '13 at 15:29






Most helpful answer for sure. Thanks a bunch.
– limp_chimp
Feb 24 '14 at 16:20





The key that I was missing was specifying the ssh host in the .git/config - Thanks a lot!
– xdotcommer
Oct 29 '15 at 3:48




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.


ssh-add -l


ssh-add -d keyfile



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.


~/.ssh/config





You can also, if necessary, use the GIT_SSH environment variable to tell git to use a different command for ssh; in particular, you can have it use a specific key. See the main git man page, or perhaps this question.
– Cascabel
Jan 12 '11 at 4:41



GIT_SSH





On my system (OSX 10.5), the command to delete the key from the keyring was ssh-add -d keyfile
– Motin
Jan 23 '12 at 4:27


ssh-add -d keyfile





Thanks @Motin, that's actually what I meant to say. Edited.
– Walter Mundt
Jan 25 '12 at 5:18



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 = git@github.com:USER/PROJECT.git


url = git@github.com:USER/PROJECT.git



to



url = https://USER@github.com/USER/PROJECT.git


url = https://USER@github.com/USER/PROJECT.git



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


SSH


HTTP





+1! This is a really good solution for doing one-off pushes from loaner machines etc where you don't want to install private keys etc.
– jpatokal
Apr 19 '12 at 0:12





i think the only problem with using https is that you have to type in the user/pass every time you do a push, unless if there is some git config i'm not aware of
– hellatan
Sep 15 '12 at 14:32



https


push





I know this is super old, but THANK YOU. My machine is not setup for ssh, but it was still trying to push to my work github account instead of my personal for a one off push. Used this in my .git/config in my project directory and worked amazing. Yay! Upvote for you.
– Sady
Sep 11 '14 at 12:24





be aware you can do git push https://username:pass@github.com/user/repo.git too... if you're ok with pass in your shell history
– Plato
Sep 18 '14 at 6:37



git push https://username:pass@github.com/user/repo.git


pass



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.





This resolved my issue in while running Git Bash in Windows10. Thanks @rajan-patil
– pclrk
Apr 16 '17 at 3:48





This resolved my issue too. Thanks @Rajan
– Ankita
May 11 '17 at 5:19





thank you, it really helpful!
– sailfish009
Jun 19 '17 at 7:31





For those that don't have the windows GUI in English, you can reach the credential manager by typing control /name Microsoft.CredentialManager on the command line
– bgusach
Aug 16 '17 at 17:01



control /name Microsoft.CredentialManager





strange, but it works great only when I'm starting Git Bash (as mentioned above) but if I continue using the same terminal I started git push with error - it keeps throwing same errors all the time, even after the terminal restart. But thank you, anyway - it saved me lots of time at the end
– Kostyantyn Didenko
Oct 3 '17 at 7:15



git push



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!





@Carpetsmoker It sounds like the OP is offering a Mac-specific solution.
– Zev Spitz
Jun 28 '14 at 21:29





Please try to find the original post and add as a link.
– Zev Spitz
Jun 28 '14 at 21:29





stackoverflow.com/questions/11067818/…
– vishnu viswanath
Jul 20 '16 at 21:34



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.



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





link was moved.
– Nathan Feger
Feb 22 '12 at 18:58





@NathanFeger the link is updated!
– asksw0rder
Feb 23 '12 at 4:22





The linked Gist and first comment on the Gist page worked perfectly for me. I think this is superior to Walter Mundt's answer as you can leave multiple keys on your keyring and everything 'just works'.
– keybits
Aug 28 '13 at 12:08



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 "your_email@youremail.com".


git config --global user.email


git config --global user.email "your_email@youremail.com"



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



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.


.netrc


git


.netrc


.netrc



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


.zshrc



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.


reset



I hope this helps someone else.



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)



Hope this helps!





This was the only answer that helped me!
– Wilder Pereira
Jun 15 at 3:35



Well, I've wasted a whole morning trying to get my two github accounts to work properly. I need to both clone, commit, and push with both. So, not knowing all the ends and outs I forced a solution which seems to be working well for me.



Essentially, I change my git and ssh profile to match the account I will then use modally til I switch again.



I have two accounts now on github, dhoerl and something else. I have separate files in my .ssh directory for the id_rsa file. I had to ssh-add -D, then ssh-add git_dhoerl, and ssh-add theOtherOne.



I have two shell commands that switch me from one profile to another. Each looks like this:


cd ~/.ssh
rm id_rsa

ln git_dhoerl id_rsa

git config --global user.email "myEmailAddr"
git config --global github.user "myAccountName"
git config --global github.token "myToken"



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.



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.


killall ssh-agent



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





This link no longer works
– brianlmerritt
Jun 23 '16 at 7:03



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


killall ssh-agent


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.


ERROR: Permission to user/repo-git.git denied to username






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard