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


当前回答

我使用自定义的IdentityFile设置了一个ssh别名,并重写了起源,以使用我的自定义me-github主机名。

#when prompted enter `id_rsa_user1` as filename
ssh-keygen -t rsa

# ~/.ssh/config
Host user1-github
HostName github.com
Port 22
User git
IdentityFile ~/.ssh/id_rsa_user1

#check original remote origin url
git remote -v
origin git@github.com:user1/my-repo.git

#change it to use your custom `user1-github` hostname
git remote rm origin
git remote add origin git@user1-github:user1/my-repo.git

其他回答

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

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

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

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

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

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

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

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

如果这是你的问题

remote: Permission to username1/repo.git denied to username2.
fatal: unable to access 'https://github.com/username1/repo.git/':
The requested URL returned error: 403

除了使用git配置从终端更改用户名和电子邮件:

$ git config --global user.name "Bob"
$ git config --global user.email "bob@example.com"

您需要从钥匙串中删除授权信息。我花了好几个小时才想出这个解决办法。我发现我的钥匙链里也有证书。

打开钥匙链访问,点击所有项目,搜索git。删除所有keychain

如果你使用SSH URL (git@github.com:username/projectname.git)而不是HTTPS URL,你可以使用$GIT_SSH_COMMAND环境变量临时指定一个不同的SSH密钥:

$ GIT_SSH_COMMAND="ssh -i different_private_key" git push

据我所知,对于SSH url, GitHub并不关心用户名,只关心密钥:如果用户帐户有权访问存储库,并且该帐户有SSH密钥(参见帐户设置中的SSH密钥页面),那么如果您使用该SSH密钥推送到该存储库,则该推送将被视为来自该用户。

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

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