如何配置git从http中提取并在一个'remote'中推送ssh?

Tsu*_*g.W 24 git authentication git-pull git-push

从http拉到转义验证(如果我不使用ssh.keygen,我不需要输入密码).

通过身份验证推送ssh.

Amb*_*ber 19

git-config手册页:

remote.<name>.url远程存储库的URL.请参阅git-fetch(1)或git-push(1).

remote.<name>.pushurl远程存储库的推送URL.见git-push(1).

尝试将前者设置为http:url,将后者设置为git+ssh:(或只是git:)url?


Von*_*onC 10

原始答案,用于 在一个'远程'中推送ssh:这仅适用于一个 repo,当前的一个:

如果你有一个git remote -v返回" origin" 的https URL ,你可以输入:

git config remote.origin.pushurl git@github.com:aUser/aRepo
Run Code Online (Sandbox Code Playgroud)

更确切地说:

git remote set-url --push git@github.com:aUSer/aRepo
Run Code Online (Sandbox Code Playgroud)

由于这里要注意:

它在功能上并不相同,因为set-url内部最终只调用config.但是set-url如果你错误输入命令部分" git remote set-url --push" 会抱怨,而git config会默默接受输入错误的选项,但实际上无法设置遥控器的url.

请参阅git-reference(此处已改编):

$ git remote -v
origin  https://github.com/schacon/git-reference.git (fetch)
origin  https://github.com/schacon/git-reference.git (push)

$ git remote set-url --push origin git@github.com:schacon/git-reference.git

$ git remote -v
origin  https://github.com/schacon/git-reference.git (fetch)
origin  git@github.com:schacon/git-reference.git (push)
Run Code Online (Sandbox Code Playgroud)

在赏金的背景下,jww补充道:

我希望这适用于所有github.com,而不仅仅是我的回购

git remote不是答案.

url.<base>.pushInsteadOf适用于所有回购:

任何以此值开头的URL都不会被推送到; 相反,它将被重写为开头,结果URL将被推送到.一世

所以:

 git config --global url."git@github.com:".pushInsteadOf https://github.com/
 # or
 git config --global url."ssh://git@github.com/".pushInsteadOf https://github.com/
Run Code Online (Sandbox Code Playgroud)

我在2014年的" Git:如何获取公共,只读的git:// URL "中提到了这个选项.

--global选项将使其适用于所有回购,而不仅仅是当前回购.