我和一个朋友共用我的电脑。我已经在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推送源local-name:remote-name

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

Git远程添加origin2… 然后

Git推送origin2 master

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

其他回答

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

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

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

请遵循以下步骤:

你必须明白这一点,在提交之前定义作者! 提交已经被冻结:它们拥有为其作者和提交者设置的任何名称,并且这些名称不能更改。

# you can check what's currently:
git config user.name
git config user.email

git config user.name "your_github_username"
git config user.email "your_github_email"

# Again check what's currently:
git config user.name
git config user.email

检查你的提交被标记到谁?

git log
# once you're confirmed that it's tagged to you, then you should move to step 3

如果作者是错误的,那么您可以轻松地撤消上次提交而不会丢失更改

此外,在移动到步骤3之前,不要忘记遵循步骤1进行完整性检查。

提示自己输入github_username和github_password

git config --local credential.helper ""
git push
# it will ask you to enter your github_username and github_password

如果运行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: