始终使用 HTTPS 拉取、使用 SSH 推送、使用代替?

Fau*_*aux 1 git github

我有许多使用不同 Github URL 格式的存储库。我希望所有存储库始终通过 HTTPS 从 Github 获取,并通过 SSH 推送。这让我的生活更轻松,使用 ssh 密钥/代理转发/...

这对我来说是有意义的,但它并没有重写一些情况:

[url "git@github.com:"]
    pushInsteadOf = "https://github.com/"
[url "https://github.com/"]
    insteadOf = "git@github.com:"
Run Code Online (Sandbox Code Playgroud)

例如,对于具有三个遥控器的测试存储库:

  • hh:http 获取、http 推送
  • hs:http 获取,ssh 推送
  • sh:ssh 获取、http 推送

那是:

% git remote -v
hh  https://github.com/foo/bar (fetch)
hh  https://github.com/foo/bar (push)

hs  https://github.com/foo/bar (fetch)
hs  git@github.com:foo/bar (push)

sh  git@github.com:foo/bar (fetch)
sh  https://github.com/foo/bar (push)
Run Code Online (Sandbox Code Playgroud)

...此配置生成:

% cat .gitconfig 
[url "git@github.com:"]
    pushInsteadOf = "https://github.com/"
[url "https://github.com/"]
    insteadOf = "git@github.com:"

% HOME=. git remote -v   
hh  https://github.com/foo/bar (fetch)
hh  git@github.com:foo/bar (push)

hs  https://github.com/foo/bar (fetch)
hs  https://github.com/foo/bar (push)  # wrong

sh  https://github.com/foo/bar (fetch)
sh  https://github.com/foo/bar (push)  # wrong
Run Code Online (Sandbox Code Playgroud)

在所有情况下,什么 git 配置片段将生成我想要的“http fetch,ssh push”遥控器?

Jus*_*tin 7

尝试使用:

[url "https://github.com/"]
  insteadOf = "git@github.com:"
[url "git@github.com:"]
  pushInsteadOf = "https://github.com/"
  pushInsteadOf = "git@github.com:"
Run Code Online (Sandbox Code Playgroud)

这将强制所有拉取使用 HTTPS,并推送使用 SSH。第二个pushInsteadOf有点荒谬,因为它将 a 重写git@github.com:为相同的git@github.com:。但是,这会阻止insteadOf重写git@github.com:tohttps://github.com/来进行推送。

有了这个:

[url "https://github.com/"]
  insteadOf = "git@github.com:"
[url "git@github.com:"]
  pushInsteadOf = "https://github.com/"
  pushInsteadOf = "git@github.com:"
Run Code Online (Sandbox Code Playgroud)

重要的部分是首先pushInsteadOf重写推送 URL ,如果存在匹配,则会阻止重写。通过设置-> ,我们已经锁定了推送 URL。常规程序仍会将初始 HTTPS 重写为 SSH 以进行推送,并且仍会将SSH 重写为 HTTPS 以进行拉取。insteadOfgit@github.com:git@github.com:pushInsteadOfinsteadOf