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


当前回答

您可以使用不同的帐号进行推送。 例如,如果您的帐户是存储在.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(也可以更改帐户)

其他回答

我不知道如何在一台机器上有第二个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

如前所述,您可以使用

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

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

git commit --amend --reset-author

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

如果这是你的问题

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协议中使用公钥认证,您将发现两者都没有

Git配置——本地凭证。助手”“ Git配置——local user.name和Git配置——local user.email

工作,你必须指定私钥属于你想玩的用户,比如

➜  Lang git:(main) ✗ git config --local -l | cat
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=git@github.com:xyz/1A.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main
user.name=xyz
user.email=xyz@post.com
credential.helper=
➜  Lang git:(main) ✗ git push
ERROR: Permission to xyz/1A.git denied to abc.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
➜  Lang git:(main) ✗ git config --local core.sshcommand 'ssh -i ~/.ssh/xyz@post.com -F /dev/null'
➜  Lang git:(main) ✗ git push
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 30.42 KiB | 10.14 MiB/s, done.
Total 7 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:xyz/1A.git
   660593a..2972551  main -> main
➜  Lang git:(main) ✗ 

我使用自定义的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