40

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
1

5 Answers 5

47

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
  • Yes, I've tried it. Usually I copy-paste from "git remote -va". It works, though it creates an additional remote tracking branch on pulls.
    – Andor
    Nov 4, 2013 at 14:34
  • 1
    I usually use a pubkey to push commits. Even though I have specified a different user, I still get Permission denied (publickey). fatal: Could not read from remote repository. How do push so that I can enter my password for the different account?
    – nipponese
    Aug 29, 2017 at 3:15
  • What am I doing wrong? I'm just getting fatal: not a git repository (or any of the parent directories): .git
    – oarfish
    Nov 25, 2020 at 14:36
  • @oarfish please check that you can push with only git push might you are not in a git repo
    – laplasz
    Nov 27, 2020 at 10:33
21

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://[email protected]/bar/ish.git nonce:nonce
1
7

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
5
  • 1
    I have searched through Stackoverflow and Google before asking this question. I need a fast/on-the-fly/ad hoc/temporary solution, not a permanent. The username is intentionally not included in the remote url and public key authentication is optional on this repo server. This is really not the thing I want.
    – Andor
    Mar 21, 2013 at 17:09
  • @user77376 The "public key authentication is optional on this repo server"? Then it isn't an ssh connection. If you need to specify another user, then $HOME/.ssh/config it is. That may not be what you want, but that is what is required to use ssh with a different user: a way to specify the public/private key. If you currently don't have an id_rsa(.pub) somewhere, then we are not talking about shh (or not the ssh I am familiar with).
    – VonC
    Mar 21, 2013 at 18:19
  • 1
    It's plain ssh, but one can authenticate with password also. For an ordinary ssh connection you should specify a username first and a password/public key/kerberos ticket or whatever authentication the server accepts.
    – Andor
    Mar 22, 2013 at 13:40
  • @user77376 ok. I will follow this question with interest then.
    – VonC
    Mar 22, 2013 at 13:42
  • There is a similar answer here stackoverflow.com/a/7927828/101923 which discusses the syntax to use for .ssh/config
    – Tim Swast
    Jun 22, 2016 at 22:10
7

I use

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

It will prompt you to input username and password

2
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.