通过 ssh 进行 Git 克隆,通过 https 推送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30869912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:18:17  来源:igfitidea点击:

Git clone over ssh, push over https

gitsshhttpsbitbucket

提问by Javi V

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.

我在 BitBucket 中有一组私有 Git 存储库。我想通过 SSH 克隆它们,以便无需密码即可自动克隆。但是,我想通过 HTTPS 推送,因为我想使用不同的用户名推送。

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

PC是普通PC,我想区分谁推送更改但我不关心谁克隆它们。

Is there any way to do this? Thanks!

有没有办法做到这一点?谢谢!

回答by Michael Adam

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

为此,您可以使用两个或更多不同的遥控器。默认情况下,当您克隆远程存储库时,origin会自动为您创建远程存储库。但是您每次都可以在 git 命令行上指定不同的存储库,例如

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 originor git pullto update the local checkout, and you can push with e.g. git push push branch(Note that the second pushhere is the name of the remote). This way, you can also specify a different ssh remote with a different user:

然后你可以git fetch origingit pull更新本地结账,你可以用例如推送git push push branch(注意push这里的第二个是远程的名称)。这样,您还可以为不同的用户指定不同的 ssh 远程:

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

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

然后您可以git push push2 branch以不同的用户身份通过​​ ssh 进行推送。