Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git clone over ssh, push over https

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!

like image 842
Javi Avatar asked Jun 16 '15 14:06

Javi


People also ask

Should I clone with HTTPS or SSH?

While SSH is usually considered more secure, for basic usage of Github, HTTPS authentication with a password is acceptable enough. In fact, Github themselves defaults to and recommends most people use HTTPS.

Does Git push use SSH or HTTPS?

Git used SSH protocol to securely transfer repository data over the internet. Uses public key encryption to secure data. Git with HTTPS uses public-key encryption-based authentication for doing every action like git push, git clone, git fetch and git pull, etc.


1 Answers

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.

like image 107
Michael Adam Avatar answered Oct 17 '22 11:10

Michael Adam