Git 作为错误的用户推送到远程 GitHub 存储库

我有一个工作 GitHub 账户和一个私人账户。首先,我将个人帐户用于测试项目,然后继续前进,并使用同一台计算机上的另一个帐户建立一个存储库。

现在,我想再次在我的个人帐户上创建一个新的存储库,我更改了全局和本地 user.name,并创建了一个新的 ssh 密钥对,输入到 GitHub 设置页面中。然后我设置了目录

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

但现在我知道了

错误: 拒绝个人用户/newrepo.git 的权限

我不知道其他帐户是如何连接到这一个。 .git/config显示没有 workusername相关的东西。

如果你正在使用 Windows10,请花时间阅读拉詹的答案。

80046 次浏览

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.

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

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

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.

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

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.

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!

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.

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

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.

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.

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.

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.

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!

The important part is the Email attribute in your ~/.gitconfig file. I have two separate git accounts (usernameA, email@A.com) and (usernameB, email@B.com). For some reason, my ~/.gitconfig file said

[user]
email = email@A.com
name = usernameB

This resulted in my commits being pushed from Account A. Changing the email to email@B.com solved the issue.