我和一个朋友共用我的电脑。我已经在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 push https://username@github.com/username/repo

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

其他回答

如果运行git push后,git要求输入用户密码,但你想以new_user的身份推送,你可能需要使用git配置remote.origin.url:

$ git push
user@my.host.com:either/branch/or/path's password:

此时使用^C退出git推送,并使用following作为new_user进行推送。

$ git config remote.origin.url
user@my.host.com:either/branch/or/path

$ git config remote.origin.url new_user@my.host.com:either/branch/or/path

$ git push
new_user@my.host.com:either/branch/or/path's password:

我刚刚添加了额外的用户:

回购的设置, 管理访问, 邀请合作者

这对我很有效。

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

来自:

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

To:

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

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

如前所述,您可以使用

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

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

git commit --amend --reset-author

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

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

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