我和一个朋友共用我的电脑。我已经在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 add .
git commit -m "initial commit"
git config --local credential.helper ""
git push https://github.com/youraccount/repo.git --all

执行此push命令后,将打开用户名密码提示符。

其他回答

如前所述,您可以使用

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

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

git commit --amend --reset-author

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

如果你使用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密钥推送到该存储库,则该推送将被视为来自该用户。

我最喜欢这样做。

git push https://username@github.com/username/repo

这将提示输入密码,因此您不必在shell中以纯文本的形式编写密码。

git add .
git commit -m "initial commit"
git config --local credential.helper ""
git push https://github.com/youraccount/repo.git --all

执行此push命令后,将打开用户名密码提示符。

您可以使用不同的帐号进行推送。 例如,如果您的帐户是存储在.gitconfig中的帐户A,您想使用帐户B,它是您想要推送的回购的所有者。

账号B: B_user_name, B_password SSH链接示例:https://github.com/B_user_name/project.git

B账号推送为:

$ git push https://'B_user_name':'B_password'@github.com/B_user_name/project.git

查看.gitconfig中的帐户

$git配置——global——list $git配置——global -e(也可以更改帐户)