Git 以错误的用户身份推送到远程 GitHub 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4665337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:50:34  来源:igfitidea点击:

Git pushing to remote GitHub repository as wrong user

gitpermissionsgithub

提问by Raz Faz

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.

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

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

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

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

but that now tells me

但现在告诉我

ERROR: Permission to personaluser/newrepo.git denied to

错误:对personaluser/newrepo.git 的权限被拒绝

I have no idea how the other account is connected to this one. .git/configshows no workusernamerelated things.

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

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

如果您使用的是 Windows 10,请花点时间阅读 Rajan 的回答。

回答by hellatan

this sounds very similar to my current work set up. it seems that you already have set up your separate ssh-keysso you also need to create a ~/.ssh/configfile 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 IdentitiesOnlyone. 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 URLis just a pointer to grab the correct user settings and does not have any affect on getting the files correctly to your target HostNameurl.

还值得注意的是,这Host URL只是一个获取正确用户设置的指针,对将文件正确获取到目标HostNameurl没有任何影响。

Now you just need to make sure your origin(or any remotein general) url match the correct Hosturl in your respective repos depending on your user name. If you already have existing personal repos, you can edit that repo's .git/configfile in your text editor:

现在,您只需要根据您的用户名确保您的origin(或任何remote一般的)url 与Host您各自的存储库中的正确url匹配。如果您已经有现有的个人存储库,则可以.git/config在文本编辑器中编辑该存储库的文件:

[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 Hosturls in your ~/.ssh/configfile as just

当然,您始终可以Host~/.ssh/config文件中的一个url设置为

Host github.com

I only used work.github.comto 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.emailvalue (and i'm guessing user.namewould 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值设置了全局 git 配置值(我猜user.name也会发送不同的值),git 将显示您作为该电子邮件用户的提交。要解决此问题,您可以覆盖本地存储库中的全局 git 配置设置:

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

现在应该将提交作为该存储库的正确用户发送。

回答by Rajan Patil

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.

删除 git 凭据。然后运行 ​​git push。这将提示要求提供 git 凭据。输入正确的凭据。

回答by yuttadhammo

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:

您也可以只切换到 https,而不是 ssh。如果您使用 https,它将尊重 .git/config 设置。因此,在 .git/config 中,更改:

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

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

to

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

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

(these values are on the git project page, click on the SSHand HTTPbuttons to ge tthe new values);

(这些值在 git 项目页面上,单击SSHHTTP按钮以获取新值);

回答by Walter Mundt

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

github 通过它看到的 ssh 密钥来识别你,而不是通过 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 -lto determine which keys are in your keyring, and ssh-add -d keyfileto remove a key from your keyring.

因此,当您尝试将推送作为您的个人帐户时,您需要确保您的工作帐户的 ssh 密钥不在您的密钥环中,反之亦然。使用ssh-add -l以确定哪些键是在你的钥匙圈,并ssh-add -d keyfile删除从你的钥匙圈的关键。

Also, you may need to check ~/.ssh/configif 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是否已将其配置为向 github 提供某些 ssh 密钥。最后,我不知道 github 如何处理具有相同 ssh 公钥的两个帐户,因此请确保您不要这样做。

回答by flipjs.io

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!

我最近遇到了同样的问题,因为我创建了一个新的 github 帐户。我试过上面的答案,但没有帮助。然后我在某处看到了一篇关于从Keychain Access 中删除 github 的帖子(仅当您使用mac 时)。当我 git push 时,它会询问用户名和密码,并且成功了!

回答by Ryan

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.

我有同样的问题。事实证明,我在 GitHub 上有两个帐户使用相同的 SSH 密钥,而 GitHub 默认使用错误的帐户,该帐户没有我所追求的存储库的权限。我从我没有使用的帐户中删除了 SSH 密钥,一切都按预期工作。

You can test which account GitHub is authenticating yourself with:

您可以测试 GitHub 使用哪个帐户进行身份验证:

ssh -T [email protected]

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.

对我来说,这最初显示了错误的用户名,但是从该帐户中删除重复的 SSH 密钥后,它显示了正确的用户名,并且我对我的 repo 的拉取和推送运行良好。

回答by A.Dev

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.
  • 进入钥匙串访问 (osX)

  • 搜索 git(确保您已选择所有项目)

  • 在这里你会找到罪魁祸首的凭据。删除它们。

Hope this helps!

希望这可以帮助!

回答by asksw0rder

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

这是一种方法:您可以为不同的 ssh 帐户使用不同的 ssh 配置。

Updated on Feb 22:

2月22日更新:

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

查看此链接:https: //gist.github.com/2351996

回答by Baub

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

如果更改与帐户关联的 SSH 密钥不起作用,请更改与帐户关联的电子邮件。

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

转到 Github > 帐户设置 > 电子邮件并验证您用于提交的电子邮件地址是否与帐户中的电子邮件匹配。

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

要查看您使用提交的电子邮件地址,运行以下命令:git config --global user.email。如果您需要更改用于提交的电子邮件地址,请运行git config --global user.email "[email protected]".

回答by Hung Cao

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

我以前让 git 每次与远程存储库交谈时都不询问我的凭据: 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

我通过使用“none”替换“wincred” git config --global credential.helper none 运行相同的命令解决了这个问题

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

然后 git 再次询问我的用户名/密码,一切顺利