如何临时更改远程推送的 git ssh 用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15551409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:47:50  来源:igfitidea点击:

How to change git ssh user for a remote push temporarily?

gitsshpush

提问by Andor

Is it possible to change the ssh user temporarly for a "git push remote master" without messing up with .git/config or "git remote", or using the whole remote url?

是否可以在不搞乱 .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

回答by HiB

Have you tried using the whole remote URL?

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

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

and you will be prompted to provide the password

系统将提示您提供密码

回答by Jesuisme

Once you've done the commit, you can use the following syntax:

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

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

You'll be asked for your github password to process the push.

系统会要求您提供 github 密码以处理推送。

For example, if your github username is "foobar", the repository clone url is "https://github.com/bar/ish.git", and the local and remote branches are named "nonce", you can use the following:

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

git push https://[email protected]/bar/ish.git nonce:nonce

回答by 0oneo

I use

我用

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

It will prompt you to input username and password

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

回答by VonC

The ssh address registered with git remote probably already include the user name, so you would need to use a complete ssh url like:

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

otheruser@remote:arepo

That won't work, because ssh will use the default public/private keys (currently used by the first user for authentication).

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

You can register a new remote in your local config:

您可以在本地配置中注册一个新的遥控器:

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

You must have a $HOME/.ssh/configfile, in order to define the ssh entry 'otheruser', because ssh needs to know what public/private key it needs to use: it cannot be the default ones ($HOME/.ssh/id_rsaand $HOME/.ssh/id_rsa.pub)

您必须有一个$HOME/.ssh/config文件,才能定义 ssh 条目“otheruser”,因为 ssh 需要知道它需要使用什么公钥/私钥:它不能是默认的($HOME/.ssh/id_rsa$HOME/.ssh/id_rsa.pub

See for instance "how to add deploy key for 2 repo with 1 user on github"

例如,参见“如何在 github 上为 1 个用户的 2 个 repo 添加部署密钥

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

That supposes you have stored the public/private keys for otheruser as:

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

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

Now, you can use that new remote to push:

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

git push originOtheruser master

回答by Kartik Ranpise

For Windows User: Follow Instructions:

对于 Windows 用户:按照说明进行操作:

Control Panel >> User Account >> Credential Manager >> Windows Credential >> Generic Credential

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

You can change git credential:

您可以更改 git 凭据:

click modify>>provide uname and password

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

Or you can remove git credential. Next time when you'll push repo, it'll ask you for credential.

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