環境
- git version 2.13.3.windows.1
行いたいこと
- fetch、push の URL を変更したい。
- 具体的には次のようにしたい。
- 修正前の状態
- origin. http://example.com/pj-a.git (fetch)
- origin. http://example.com/pj-a.git (push)
- origin. http://example.com/pj-b.git (push)
- 修正後の状態
- origin. http://example.com/pj-b.git (fetch)
- origin. http://example.com/pj-b.git (push)
- 以上を、Git のコマンドを使って行いたい。
使用コマンドまとめ
# リモートリポジトリの設定確認 git remote -v # ローカルリポジトリの設定ファイル内容表示 cat .git/config # リモートリポジトリの URL 削除 git remote set-url --delete <name> <url> # リモートリポジトリの削除 git remote remove <name> # リモートリポジトリの追加 git remote add <name> <url>
ちなみに、今回の目的から外れるが、Git コマンドを使用しないで同様のことを行いたい場合は、.git/config ファイルを編集すればよい。
具体的な作業
パターン1 .git/config に pushurl が設定されていない場合
$ # 修正前の状態
$ git remote -v
origin. http://example.com/pj-a1.git (fetch)
origin. http://example.com/pj-a1.git (push)
origin. http://example.com/pj-b1.git (push)
$ cat .git/config
[core]
... 略 ...
[remote "origin"]
url = http://example.com/pj-a1.git
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://example.com/pj-b1.git
$
$
$
$ # push の URL を削除
$ git remote set-url --delete origin http://example.com/pj-a1.git
$
$
$
$ # 修正後の状態
origin. http://example.com/pj-b1.git (fetch)
origin. http://example.com/pj-b1.git (push)
$ cat .git/config
[core]
... 略 ...
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://example.com/pj-b1.git
.git/config にもともとあった url を削除しただけの内容のため、fetch の下の url がそのまま残った形となりました。
おそらく url は最初に現れたものを優先するのでしょう。したがってこのままでよいと思います。
パターン2 .git/config に pushurl が設定されていなる場合
$ # 修正前の状態
$ git remote -v
origin. http://example.com/pj-a2.git (fetch)
origin. http://example.com/pj-a2.git (push)
origin. http://example.com/pj-b2.git (push)
$ cat .git/config
[core]
... 略 ...
[remote "origin"]
url = http://example.com/pj-a1.git
fetch = +refs/heads/*:refs/remotes/origin/*
pushurl = http://example.com/pj-a2.git
pushurl = http://example.com/pj-b2.git
$
$
$
$ # push の URL を削除できない!!!!!
$ git remote set-url --delete origin http://example.com/pj-a2.git
fatal: Will note delete all non-push URLs
$
$
$
$ # リモートリポジトリ設定自体を削除し、新しいリモートリポジトリを追加
$ # 削除
$ git remote remove origin
$ # 確認
$ cat .git/config
[core]
... 略 ...
$ # 追加
$ git remote add origin http://example.com/pj-b2.git
$
$
$
$ # 修正後の状態
origin. http://example.com/pj-b2.git (fetch)
origin. http://example.com/pj-b2.git (push)
$ cat .git/config
[core]
... 略 ...
[remote "origin"]
url = http://example.com/pj-b2.git
fetch = +refs/heads/*:refs/remotes/origin/*
一度 url を削除して追加したため、パターン 1 とは異なり、url が fetch の上に位置しています。
おわりに
次の git コマンドヘルプが参考になりました!
$ git remote -h
usage: git remote [-v | --verbose]
or: git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>
or: git remote rename <old> <new>
or: git remote remove <name>
or: git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
or: git remote [-v | --verbose] show [-n] <name>
or: git remote prune [-n | --dry-run] <name>
or: git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
or: git remote set-branches [--add] <name> <branch>...
or: git remote get-url [--push] [--all] <name>
or: git remote set-url [--push] <name> <newurl> [<oldurl>]
or: git remote set-url --add <name> <newurl>
or: git remote set-url --delete <name> <url>
-v, --verbose be verbose; must be placed before a subcommand
以上です。
