我和一个朋友共用我的电脑。我已经在Windows 7上使用git bash shell推送到GitHub。现在我们在那台电脑上做一个不同的项目,我需要她推到她的账户上。但它一直试图使用我的用户名,并说我没有访问她的存储库:

$ git push her_github_repository our_branch
ERROR: Permission to her_username/repository.git denied to my_username.
fatal: The remote end hung up unexpectedly

重要:2021年8月13日删除了对密码验证的支持。


当前回答

本例将值johndoe@example.com写入配置名user.email。它使用——global标志,所以这个值是为当前操作系统用户设置的。

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

其他回答

我一直在使用一台机器,用不同的用户名将代码推送到两个不同的GitHub帐户。假设您已经设置了一个帐户,并想添加一个新帐户:

生成新的SSH密钥SSH -keygen -t rsa -C "myaddress@example.com" 保存它,但记住不要覆盖现有的id_rsa。给它一个不同的名字,例如id_rsa_another 复制密钥的内容到你的GitHub帐户:

设置-> SSH和GPG密钥-> SSH新密钥->标签粘贴 the key ->添加SSH密钥

将密钥添加到ssh代理:ssh- Add ~/.ssh/id_rsa_another 安装一个GitHub主机:用touch ~/创建一个配置文件。Ssh /config并编辑文件,为您的帐户提供配置:

#first account
Host github.com-first
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

#another account
Host github.com-another
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_another

现在您应该能够根据您添加到ssh代理的键从不同的帐户推送,即使用您的第一个帐户 ssh-add ~ / . ssh / id_rsa。

你可能还想更改你的用户邮箱:

Git配置——全局用户。电子邮件“myaddress@example.com”

或者在向其中一个帐户发送代码时,在权限错误的情况下清除SSH密钥:

ssh-add - d

如果在Windows下,用户Git for Windows和管理凭据的管理器(又名Git- credential - manager -for Windows Link),问题是当使用OAuth令牌通过https推送到GitHub时,没有简单的方法在用户之间切换。

原因是令牌被存储为:

互联网地址:git:https://github.com 用户名:个人访问令牌 密码:OAuth_Token

Internet Address中的URL变量是无效的,例如:

转到:https://username@github.com git:https://github.com/username ...

解决方案:名称空间。这可以在Git-Credential-Manager-for-Windows的配置细节中找到:

https://github.com/Microsoft/Git-Credential-Manager-for-Windows/blob/master/Docs/Configuration.md

引用其中的话:

名称空间 为存储的凭证设置名称空间。 默认情况下,GCM对所有存储的凭证使用'git'命名空间,设置这个配置值可以控制全局或每台主机使用的命名空间。 Git配置——global credentials .namespace名称

现在,在Windows凭据管理器中存储您的凭据为:

互联网地址:git.用户名:https://github.com 用户名:个人访问令牌 密码:OAuth_Token

注意我们已经改变了:git -> git。用户名(将用户名改为实际用户名或任意你想要的唯一标识符)

现在,在你想要使用特定条目的存储库中,执行:

git config credential.namespace git.username

(再次…用你想要的值替换用户名)

你的.git/config现在将包含:

[credential]
    namespace = git.username

果不其然!正确的凭据将从Windows凭据存储中取出。

当然,这不会改变哪个用户/电子邮件正在推送。为此,您必须配置通常的user.name和user.email

如果您使用不同的windows用户,您的SSH密钥和git设置将是独立的。

如果这对你来说不是一个选择,那么你的朋友应该把你的SSH密钥添加到她的Github帐户。

虽然,之前的解决方案会让你自己推动,但它会让你推动她的回购。如果你不想在同一台电脑的不同文件夹中工作,你可以通过删除config命令的-g标志,在git的本地文件夹中设置用户名和电子邮件:

git config user.name her_username
git config user.email her_email

或者,如果你推送https协议,Github每次都会提示输入用户名/密码(除非你使用密码管理器)。

发生提交的用户id存储在配置文件中。

转到存储库的顶部 vi . /配置

更改“[remote "origin"]后列出的url行,使其具有适当的用户id

请遵循以下步骤:

你必须明白这一点,在提交之前定义作者! 提交已经被冻结:它们拥有为其作者和提交者设置的任何名称,并且这些名称不能更改。

# you can check what's currently:
git config user.name
git config user.email

git config user.name "your_github_username"
git config user.email "your_github_email"

# Again check what's currently:
git config user.name
git config user.email

检查你的提交被标记到谁?

git log
# once you're confirmed that it's tagged to you, then you should move to step 3

如果作者是错误的,那么您可以轻松地撤消上次提交而不会丢失更改

此外,在移动到步骤3之前,不要忘记遵循步骤1进行完整性检查。

提示自己输入github_username和github_password

git config --local credential.helper ""
git push
# it will ask you to enter your github_username and github_password