如何临时更改SSH的git用户对远程推送?(How to change git ssh user f

2019-07-22 09:26发布

是否有可能即暂时更改SSH用户的“混帐推远程主”不带的.git / config或“远程Git”搞乱,或使用整个远程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

Answer 1:

您可以选择使用整个远程URL试过吗?

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

你会被提示输入密码



Answer 2:

一旦你已经做了承诺,你可以使用下面的语法:

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

你会被要求输入密码的GitHub处理推。

例如,如果你的GitHub用户名是“foobar的”资料库克隆的网址为“ https://github.com/bar/ish.git ”,以及本地和远程分支命名为“现时”,你可以使用下面的:

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


Answer 3:

我用

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

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



Answer 4:

使用Git登记的SSH远程地址可能已经包括用户名,所以你需要使用一个完整的SSH网址,如:

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

例如,见“ 如何跟1个用户在github上增加部署键2回购 ”

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

这假设你已经存储的公开/私有密钥otheruser为:

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

现在,您可以使用新的远程推:

git push originOtheruser master


Answer 5:

对于Windows用户:请说明:

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

你可以改变的git凭证:

单击修改>>提供UNAME和密码

或者你可以删除的Git凭证。 下一次,当你推回购,它会问你的凭据。



文章来源: How to change git ssh user for a remote push temporarily?
标签: git ssh push