【问题标题】:How to change git ssh user for a remote push temporarily?如何临时更改 git ssh 用户进行远程推送?
【发布时间】:2013-03-11 04:36:34
【问题描述】:

是否可以暂时将 ssh 用户更改为“git push remote master”而不弄乱 .git/config 或“git remote”,或者使用整个远程 url?

[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 push


【解决方案1】:

使用 git remote 注册的 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 条目“其他用户”,因为 ssh 需要知道它需要使用什么公钥/私钥:它不能是默认值那些($HOME/.ssh/id_rsa$HOME/.ssh/id_rsa.pub

参见例如“how to add deploy key for 2 repo with 1 user on github

Host otheruser
HostName remote
User otheruser
IdentityFile ~/.ssh/otheruser

假设您已将其他用户的公钥/私钥存储为:

$HOME/.ssh/otheruser
$HOME/.ssh/otheruser.pub

现在,您可以使用新的遥控器进行推送:

git push originOtheruser master

【讨论】:

  • 在问这个问题之前,我已经通过 Stackoverflow 和 Google 进行了搜索。我需要一个快速/即时/临时/临时解决方案,而不是永久解决方案。用户名故意不包含在远程 url 中,并且公钥身份验证在此 repo 服务器上是可选的。这真的不是我想要的。
  • @user77376 “在这个 repo 服务器上公钥认证是可选的”?那么它不是一个ssh连接。如果您需要指定另一个用户,那么$HOME/.ssh/config 是。这可能不是您想要的,但这是对不同用户使用 ssh 所需的:一种指定公钥/私钥的方法。如果您目前在某处没有id_rsa(.pub),那么我们不是在谈论 shh(或者不是我熟悉的 ssh)。
  • 这是普通的 ssh,但也可以使用密码进行身份验证。对于普通的 ssh 连接,您应该首先指定用户名和密码/公钥/kerberos 票证或服务器接受的任何身份验证。
  • @user77376 好的。那么我会感兴趣地关注这个问题。
  • 这里有一个类似的答案stackoverflow.com/a/7927828/101923,它讨论了用于 .ssh/config 的语法
【解决方案2】:

您是否尝试过使用整个远程 URL?

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

系统会提示您提供密码

【讨论】:

  • 是的,我试过了。通常我从“git remote -va”复制粘贴。它可以工作,尽管它会在拉取时创建一个额外的远程跟踪分支。
  • 我通常使用 pubkey 来推送提交。即使我指定了不同的用户,我仍然得到Permission denied (publickey). fatal: Could not read from remote repository. 如何推送以便我可以输入不同帐户的密码?
  • 我做错了什么?我刚收到fatal: not a git repository (or any of the parent directories): .git
  • @oarfish 请检查您是否只能使用 git push 推送,可能您不在 git repo 中
【解决方案3】:

完成提交后,您可以使用以下语法:

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

【讨论】:

【解决方案4】:

我用

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

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

【讨论】:

【解决方案5】:

对于 Windows 用户: 遵循说明:

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

您可以更改 git 凭据:

点击修改>>提供uname和密码

或者您可以删除 git 凭据。 下次你推送 repo 时,它会要求你提供凭证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    相关资源
    最近更新 更多