Git clone over ssh, push over https

42,407

You can use two or more different remotes for that. By default, when you clone a remote repository, the remote origin is automatically created for you. But you can specify a different repository on the git command line each time literally like e.g.

git push https://git-server/myrepo.git branch

but it is much more convenient to add them as named remotes if you plan to use them more than once. Here is a more complete example transcript:

git clone ssh://user1@git-server/myrepo.git
cd myrepo
git remote add push https://git-server/myrepo.git

Then you can git fetch origin or git pull to update the local checkout, and you can push with e.g. git push push branch (Note that the second push here is the name of the remote). This way, you can also specify a different ssh remote with a different user:

git remote add push2 ssh://user2@git-server/myrepo.git

Then you can do git push push2 branch in order to push via ssh as a different user.

Share:
42,407
Javi
Author by

Javi

Updated on July 17, 2022

Comments

  • Javi
    Javi almost 2 years

    I have a set of private Git repos in BitBucket. I want to clone them through SSH so that the cloning can be automated without asking for a password. However, I want to push over HTTPS because I want to push with a different user name.

    The PC is a common PC, and I want to distinguish who pushes changes but I do not care about who clone them.

    Is there any way to do this? Thanks!