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


当前回答

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

来自:

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

To:

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

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

其他回答

如果您使用ssh和get

some_username/repository的权限。Git拒绝 Alice_username

虽然你不想推作为Alice_username,确保Alice_username没有你的电脑的ssh密钥添加到它的github帐户。

我从爱丽丝的github账户上删除了ssh密钥,推送成功了。

修改项目根目录下的。git目录中的config文件:[PROJECT_PATH]/.git/config

我在这个文件的末尾添加了以下部分。之后我就可以用我的新名字按遥控器了。

[user]
    name = Your Name
    email = MyEmailAddress@company.com

如果在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

我最喜欢这样做。

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: