9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

git プロトコルのかわりに ssh で git レポジトリを clone したいとき

Posted at

ネタ元: githubでhttpsのURLを指定してもgitプロトコルやssh経由にする方法 - @znz blog

ssh プロトコルでアクセスできるけれど,git プロトコルで (外から) アクセスできない環境があるとします。
そのような環境のレポジトリ,ふつうに ssh プロトコルで clone すればいいんですが,submodule をつかっていていてその submodule のレポジトリ URL が git プロトコルだったりすると,外の環境では submodule update ができなくて困ったりします。

そんなときは, git の URL insteadOf 機能を使って URL を書き換えてやると,(内側での環境と同じように) clone できます。

やりたいことは,

  • git://git.example.org/project.git という URL を
  • git@git.example.org:project.git という URL に

書き換えたい。
(後者は SCP 形式ですが,git は ssh://git@git.example.org/project.git のような URL 形式も使うことができます。今回は SCP 形式で話をすすめます)

このとき,シェルから以下のようにして git の global config を設定します。

$ git config --global url.'git@git.example.org:'.insteadOf git://git.example.org/

置き換え後の git@git.example.org: の「:」を付与するのを忘れないようにしてください。
(この点では ssh URL 形式を使ったほうが,見た目はいいかもしれませんね)

このように実行することで, ~/.gitconfig の内容に以下の内容が追加されます。

[url "git@git.example.org:"]
        insteadOf = git://git.example.org/

ポイントは

  • URL 置換はプロトコル部分だけではなく,より長い部分を指定できる
    • だから今回のようにドメイン単位で置換対象を絞り込むことができる
      • --global 指定しても安心
      • より深く,プロジェクトパス単位で置換対象を絞り込むこともできる (はず)
    • 今回は同じサーバを指定しているが,違うサーバにむけることもできる (はず)
  • 単純に置換するだけなので,もちろん逆に ssh URL を git URL に置換したり https:// に置換したりもできる
  • URL 置換は最長一致したものが使われるので,複数指定しても大丈夫
  • push の際だけ置換をおこなう pushInsteadOf もある
    • ネタ元サイトや git help config を参照してください。
9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?