How to change git ssh user for a remote push tempo

2019-01-07 10:52发布

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?

[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
5条回答
做个烂人
2楼-- · 2019-01-07 11:12

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.

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:

git push https://foobar@github.com/bar/ish.git nonce:nonce
查看更多
放荡不羁爱自由
3楼-- · 2019-01-07 11:18

Have you tried using the whole remote URL?

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

and you will be prompted to provide the password

查看更多
家丑人穷心不美
4楼-- · 2019-01-07 11:18

I use

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

It will prompt you to input username and password

查看更多
闹够了就滚
5楼-- · 2019-01-07 11:24

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

otheruser@remote:arepo

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

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/config file, 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_rsa and $HOME/.ssh/id_rsa.pub)

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

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
查看更多
看我几分像从前
6楼-- · 2019-01-07 11:25

For Windows User: Follow Instructions:

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

You can change git credential:

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.

查看更多
登录 后发表回答