27

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!

1 Answer 1

42

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.

2
  • Thank you for your reply! I actually knew I could do that, but didn't think about it that way :) Just one question regarding adding a second ssh with different user: how does the SSH key work in this case? user2 will be able to push because I have the key assigned to user1? Or does user2 needs to deploy another SSH key? Thanks!!
    – Javi
    Jun 16, 2015 at 18:07
  • The ssh key to be used depends on which ssh keys are known on the server as authorized_keys for the different users. If both users have the same public key in their ~/.ssh/known_hosts file, then the same will work for the client. Otherwise different keys have to be used. Jun 17, 2015 at 9:54

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.