脳汁portal

アメリカ在住(だった)新米エンジニアがその日学んだIT知識を書き綴るブログ

[Git]参照したいリポジトリのURLが変わった場合

サーバ変更やリポジトリの作成しなおし等で、Gitの大元のoriginのURLが変わってしまった時の対処方です。
(あまり無いとは思いますが・・・)


まず普通にfetchとかしてもアクセスできませんよって怒られます。

$ git remote -v
origin  https://github.com/hoge/hoge.git (fetch)
origin  https://github.com/hoge/hoge.git (fetch)

$ git fetch origin
error: The requested URL returned error: 410 Gone while accessing https://github.com/hoge/hoge.git
fatal: HTTP request failed


このままではpushもpullもできないので、gitの設定ファイルを修正します。

$ vi .git/config

  1 [core]
  2         repositoryformatversion = 0
  3         filemode = true
  4         bare = false
  5         logallrefupdates = true
  6 [remote "origin"]
  7         url = https://github.com/hoge/hoge.git
  8         fetch = +refs/heads/*:refs/remotes/origin/*
  9 [branch "master"]
 10         remote = origin
 11         merge = refs/heads/master
 12   .
 13   .
 14   .

上記の7行目のurlを新しいURLへ変更します。
今回はhoge⇒fugaへ変更します。

  6 [remote "origin"]
  7         url = https://github.com/hoge/hoge.git
  8         fetch = +refs/heads/*:refs/remotes/origin/*
   ↓
  6 [remote "origin"]
  7         url = https://github.com/fuga/fuga.git
  8         fetch = +refs/heads/*:refs/remotes/origin/*


確認してみましょう。

$ git remote -v
origin  https://github.com/fuga/fuga.git (fetch)
origin  https://github.com/fuga/fuga.git (fetch)

$ git fetch origin
・
・
・

これでリポジトリのURLも変更されてpushもpullもできるようになりました。