84

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.

xenteros
  • 14,275
  • 12
  • 47
  • 81
Raz Faz
  • 841
  • 1
  • 7
  • 3

15 Answers15

79

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.

hellatan
  • 3,171
  • 1
  • 24
  • 36
50

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.

Rajan Patil
  • 852
  • 9
  • 15
  • 4
    This resolved my issue in while running Git Bash in Windows10. Thanks @rajan-patil – suspicious_williams Apr 16 '17 at 03:48
  • 1
    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
  • 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 03 '17 at 07:15
  • This is definitely the best answer I've come across. The rest were convoluted but this was simple and precise. – claudekennilol Oct 15 '17 at 03:41
  • This works ONCE. After you add the accounts again, the same problem continues to occur. – Megakoresh Oct 26 '17 at 06:52
  • just rm .git-credentials from user folder for terminal user. – suiwenfeng Apr 18 '21 at 01:16
38

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

to

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

yuttadhammo
  • 4,879
  • 6
  • 32
  • 44
  • 2
    +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. – lambshaanxy Apr 19 '12 at 00:12
  • 1
    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
  • 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
  • 2
    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 06:37
  • This is the best solution. Thank you! – norioumata Mar 31 '20 at 08:27
33

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.

Walter Mundt
  • 22,993
  • 5
  • 50
  • 60
  • 3
    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](http://www.kernel.org/pub/software/scm/git/docs/#_other), or perhaps [this question](http://stackoverflow.com/questions/3496037/how-to-specify-which-ssh-key-to-use-within-git-for-git-push-in-order-to-have-gito). – Cascabel Jan 12 '11 at 04:41
  • 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 04:27
  • Thanks @Motin, that's actually what I meant to say. Edited. – Walter Mundt Jan 25 '12 at 05:18
  • What if one needs both keyrings, as in they have two different github accounts with a different ssh key? – Brian Holt Mar 14 '21 at 21:48
12

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!

flipjs.io
  • 161
  • 2
  • 7
12

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.

Ryan
  • 8,298
  • 9
  • 42
  • 53
10

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!

Patrick
  • 1,615
  • 6
  • 18
  • 27
A.Dev
  • 146
  • 1
  • 7
6

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

michaelliu
  • 1,657
  • 1
  • 13
  • 13
asksw0rder
  • 986
  • 1
  • 11
  • 18
  • 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
3

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

Baub
  • 4,824
  • 14
  • 52
  • 98
2

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

Hung Cao
  • 71
  • 1
  • 2
1

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.

Steven Lambert
  • 4,942
  • 1
  • 24
  • 43
1

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.

mshameer
  • 1,831
  • 2
  • 10
  • 15
1

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.

0

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.

Dan Kelly
  • 25
  • 4
0

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/

hobberwickey
  • 5,274
  • 1
  • 24
  • 28