如何临时更改 git ssh 用户以进行远程推送?

40

在不干扰.git/config或"git remote",或使用整个远程URL的情况下,是否可以临时更改ssh用户以进行"git push remote master"操作?

[root@host gitrepo]# git push otheruser@remote master # this does not work, but how great it would be
[root@host gitrepo]# USER=otheruser git push remote master # still asks password for root

我喜欢 GIT_SSH_COMMAND https://dev59.com/KGYq5IYBdhLWcg3wwDRA#27607760 - Ben Creasy
5个回答

47

你尝试使用完整的远程URL了吗?

git push ssh://<temp_user>@<host>/<repo_path> <local_branch>:<remote_branch>

然后您将被提示输入密码


是的,我已经尝试过了。通常我会从“git remote -va”中复制粘贴。虽然它会在拉取时创建一个额外的远程跟踪分支,但它仍然有效。 - Andor
1
我通常使用公钥来推送提交。即使我指定了不同的用户,我仍然会收到“Permission denied (publickey)。fatal: Could not read from remote repository.”的错误提示。如何推送以便我可以输入不同账户的密码? - nipponese
我做错了什么?我只是得到了“fatal: not a git repository (or any of the parent directories): .git” - oarfish
@oarfish 请检查一下,您是否可以只使用 git push 进行推送,可能您不在 git 存储库中。 - laplasz

21

提交代码后,您可以使用以下语法:

git push https://<username>@github.com/<github repository> <local branch name>:<remote branch name>

在推送过程中,系统会要求您输入github密码。

例如,如果您的github用户名是“foobar”,仓库克隆URL为“https://github.com/bar/ish.git”,本地和远程分支都命名为“nonce”,则可以使用以下命令:

git push https://foobar@github.com/bar/ish.git nonce:nonce

3
Github提示:如果启用了双重身份验证(2FA),当提示输入密码时,您必须输入个人访问令牌而不是密码(请参阅:https://help.github.com/articles/providing-your-2fa-authentication-code/#through-the-command-line)。 - qff

7

我使用

git push https://github.com/${userName}/${repoName}

它会提示您输入用户名和密码。

3
这是针对Github的特定情况,原问题更加通用,涉及Git。 - Owen Blacker
Github提示:如果启用了双重身份验证(2FA),当提示输入密码时,您必须输入个人访问令牌而不是密码(请参阅:https://help.github.com/articles/providing-your-2fa-authentication-code/#through-the-command-line)。 - qff

7

与git远程注册的ssh地址可能已经包含了用户名,因此您需要使用完整的ssh url,例如:

otheruser@remote:arepo

这不起作用,因为ssh将使用默认的公钥/私钥(当前用于身份验证的第一个用户)。

您可以在本地配置中注册新的远程服务:

# use the current ssh address registered for origin, changing just the user
# but you need a config file
git remote add originOtheruser otheruser:arepo

您需要有一个$HOME/.ssh/config文件,才能定义ssh条目“otheruser”,因为ssh需要知道它需要使用什么公钥/私钥:不能使用默认的那些($HOME/.ssh/id_rsa$HOME/.ssh/id_rsa.pub)。
例如,请参考“如何在Github上添加1个用户的2个repo的部署密钥”的说明
Host otheruser
HostName remote
User otheruser
IdentityFile ~/.ssh/otheruser

假设您已经将otheruser的公钥/私钥存储为:
$HOME/.ssh/otheruser
$HOME/.ssh/otheruser.pub

现在,您可以使用新的遥控器来push:
git push originOtheruser master

1
在提问之前,我已经通过Stackoverflow和Google进行了搜索。我需要一个快速/即时/临时的解决方案,而不是永久性的。用户名故意未包含在远程URL中,并且在此仓库服务器上公钥身份验证是可选的。这真的不是我想要的东西。 - Andor
@user77376 "这个仓库服务器上的公钥身份验证是可选的"?那么这不是一个ssh连接。如果您需要指定另一个用户,则需要使用$HOME/.ssh/config。这可能不是您想要的,但这是使用不同用户的ssh所需的:一种指定公钥/私钥的方法。如果您当前没有在任何地方找到id_rsa(.pub),那么我们谈论的就不是ssh(或者不是我熟悉的ssh)。 - VonC
1
这是普通的ssh连接,但也可以使用密码进行身份验证。对于普通的ssh连接,您应该首先指定用户名和密码/公钥/kerberos票据或服务器接受的任何身份验证方式。 - Andor
@user77376 好的,我会关注这个问题的进展。 - VonC
这里有一个类似的答案:https://dev59.com/wGsz5IYBdhLWcg3wZm3Q#7927828,讨论了使用`.ssh/config`的语法。 - Tim Swast

1

对于 Windows 用户:

按以下步骤操作:

控制面板 >> 用户帐户 >> 凭据管理器 >> Windows 凭据 >> 通用凭据

您可以更改 git 凭据:

单击修改 >> 提供用户名和密码

或者您可以删除 git 凭据。下次推送库时,它将要求您提供凭据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接