我和一个朋友共用我的电脑。我已经在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日删除了对密码验证的支持。


当前回答

如前所述,您可以使用

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

手动设置单次回购的用户名和邮箱,但必须添加此命令:

git commit --amend --reset-author

如果你在改变之前已经尝试过了。否则更改不会出现在配置文件中。

其他回答

很容易修复,您可以在~/.ssh/config处编辑该文件

来自:

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/private_key

To:

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/other_account_private_key

切换帐户时,两者交替使用。此外,为了避免每次切换时都更改git配置的全局名称和电子邮件,您可以满足于一些通用输入,或者尝试在这里进行局部作用域配置。

我一直在使用一台机器,用不同的用户名将代码推送到两个不同的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

请遵循以下步骤:

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

# 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

在git bash shell中,导航到你朋友的存储库并键入以下内容:

git config --local credential.username "friend's_username"

现在,凭据存储将使用属于她的用户名而不是您的用户名的凭据。

我不知道如何在一台机器上有第二个github身份(这些答案都不适合我),但我确实弄清楚了如何能够以我自己的身份推送多个不同的github账户。

推送相同的用户名,但不同的github帐户

为你的第二个github账户设置第二个SSH密钥(就像这样) 帐户之间的变化如下:

用我的第二个github账号推送

ssh-add -D
ssh-add ~/.ssh/ssh_key_for_my_2nd_account
git push

用我的主账号推送

ssh-add -D
ssh-add ~/.ssh/id_rsa   
git push