我和一个朋友共用我的电脑。我已经在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协议中使用公钥认证,您将发现两者都没有

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) ✗ 

其他回答

这应该是有效的: Git推送源local-name:remote-name

更好的是,对于GitLab,我使用了第二个“origin”,即“origin2”:

Git远程添加origin2… 然后

Git推送origin2 master

传统的(短的)git推送应该隐式地工作,就像第一个“origin”一样。

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

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

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

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

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

如果您使用ssh和get

some_username/repository的权限。Git拒绝 Alice_username

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

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

我一直在使用一台机器,用不同的用户名将代码推送到两个不同的GitHub帐户。假设您已经设置了一个帐户,并想添加一个新帐户:

生成新的SSH密钥SSH -keygen -t rsa -C "myaddress@example.com" 保存它,但记住不要覆盖现有的id_rsa。给它一个不同的名字,例如id_rsa_another 复制密钥的内容到你的GitHub帐户:

设置-> SSH和GPG密钥-> SSH新密钥->标签粘贴 the key ->添加SSH密钥

将密钥添加到ssh代理:ssh- Add ~/.ssh/id_rsa_another 安装一个GitHub主机:用touch ~/创建一个配置文件。Ssh /config并编辑文件,为您的帐户提供配置:

#first account
Host github.com-first
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

#another account
Host github.com-another
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_another

现在您应该能够根据您添加到ssh代理的键从不同的帐户推送,即使用您的第一个帐户 ssh-add ~ / . ssh / id_rsa。

你可能还想更改你的用户邮箱:

Git配置——全局用户。电子邮件“myaddress@example.com”

或者在向其中一个帐户发送代码时,在权限错误的情况下清除SSH密钥:

ssh-add - d

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