GitのリモートリポジトリのURLを変更する

概要

git remote add origin 〜でタイポして間違ったURLを登録してしまったり、中央リポジトリが引っ越してしまった場合に、リモートリポジトリのURLを変更する方法です。



構成

git-core @1.7.6.1_1+doc+pcre+python27
git-flow @0.4.1_0



URLの変更方法

$ git remote set-url <リポジトリの名前> <新しいリポジトリのURL>



作業ログ

変更前のリモートリポジトリの確認

リモートリポジトリのURLはgit remote -vで確認できます。

$ git remote -v
origin	git+ssh://repos.tetsuyai.com/var/git/buzzbuzz.git (fetch)
origin	git+ssh://repos.tetsuyai.com/var/git/buzzbuzz.git (push)

実際のところ、その情報は.git/configに記録されています。

$ cat .git/config
...
[remote "origin"]
	url = git+ssh://repos.tetsuyai.com/var/git/buzzbuzz.git
	fetch = +refs/heads/*:refs/remotes/origin/*


リモートリポジトリのURLの変更

上書きを期待してgit remote add origin 〜を再実行してみましたが、案の定拒否されました。やはり正しい方法で変更する必要があるようです。

$ git remote add origin git+ssh://new-repos.tetsuyai.com/var/git/buzzbuzz.git
fatal: remote origin already exists.

その正しい方法とは、、、一般的にはgit remote set-urlコマンドを使うことが多いようです。

$ git remote set-url origin git+ssh://new-repos.tetsuyai.com/var/git/buzzbuzz.git


変更後のリモートリポジトリの確認

無事に変更されました。

$ git remote -v
origin	git+ssh://new-repos.tetsuyai.com/var/git/buzzbuzz.git (fetch)
origin	git+ssh://new-repos.tetsuyai.com/var/git/buzzbuzz.git (push)

.git/configも変更されています。

$ cat .git/config
...
[remote "origin"]
	url = git+ssh://new-repos.tetsuyai.com/var/git/buzzbuzz.git
	fetch = +refs/heads/*:refs/remotes/origin/*