git remote add origin … を打って error: remote origin already exists. と出たら、それは「origin という名前の送り先は、もう登録されています」という意味です。壊れたわけではありません。まず、いま登録されている送り先の URL を見ます。
まずこれを打つ
git remote -v出てきた URL が、GitHub で作った自分のリポジトリと同じなら、何もしなくて大丈夫です。違っていたら、下の見分け方へ。
何が起きているか
origin は、送り先(リモート)に付けた名前です。1つのリポジトリの中で、同じ名前は1つしか登録できません。下は、送り先を登録した後に、別の URL で同じ origin をもう一度登録しようとした画面です。
hanako@MacBook-Air cafe % git remote add origin https://github.com/hanako/cafe-shop.git error: remote origin already exists. hanako@MacBook-Air cafe % git remote -v origin https://github.com/hanako/cafe.git (fetch) origin https://github.com/hanako/cafe.git (push)
断られた2回目の登録は、何も変えていません。git remote -v の (fetch) は取ってくる先、(push) は送る先です。ふつうは同じ URL が2行並びます。
よくあるきっかけは、次の3つです。
- GitHub の案内に出ているコマンドを、上から順にもう一度貼り付けた(2回目の
git remote add) - 最初の登録で URL を打ち間違えた、またはリポジトリの名前を後から変えた
- 人のリポジトリを
git cloneした。clone すると、元の場所が自動でoriginとして登録されます
原因の見分け方
git remote -v に出た URL | 状況 | 打つもの |
|---|---|---|
| 自分のリポジトリの URL と同じ | もう登録済み(2回目を打っただけ) | 何もしない。次の git push へ進む |
| 綴りが違う・古い名前・HTTPS と SSH を替えたい | 登録した URL を替えたい | git remote set-url origin 新しいURL |
| 自分のではない(clone 元の人や教材の URL) | clone したので、origin が元の場所を指している | git remote rename origin upstream のあと git remote add origin 自分のURL |
| 何も出ない | origin は登録されていない(このエラーは出ないはず) | 別のフォルダにいないか pwd で確かめる |
URL が違う時:set-url で付け替える
登録した URL を替えたい時は、消して付け直すより、set-url で付け替えるのが手短です。名前(origin)はそのままで、URL だけが替わります。
hanako@MacBook-Air cafe % git remote set-url origin https://github.com/hanako/cafe-shop.git hanako@MacBook-Air cafe % git remote -v origin https://github.com/hanako/cafe-shop.git (fetch) origin https://github.com/hanako/cafe-shop.git (push)
GitHub に表示されている URL をコピーして貼ると、打ち間違いを防げます。HTTPS の形(https://github.com/…)から SSH の形([email protected]:…)に替える時も、同じコマンドです。set-url で書き換えるのは手元の設定だけなので、GitHub の側には何も送られません。
clone した物を自分の送り先に出したい時:rename で分ける
教材や人のリポジトリを clone して、それを自分の GitHub に出したい時は、origin がもう clone 元を指しています。ここで clone 元の URL を残しておきたいなら、origin の名前を upstream(元の場所、という意味でよく使われる名前)に替えてから、自分の送り先を origin として登録します。
下は、練習のために用意したリポジトリを clone した後の画面です。
hanako@MacBook-Air cafe % git remote -v origin https://github.com/sensei/cafe (fetch) origin https://github.com/sensei/cafe (push) hanako@MacBook-Air cafe % git remote add origin https://github.com/hanako/cafe.git error: remote origin already exists. hanako@MacBook-Air cafe % git remote rename origin upstream hanako@MacBook-Air cafe % git remote add origin https://github.com/hanako/cafe.git hanako@MacBook-Air cafe % git remote -v origin https://github.com/hanako/cafe.git (fetch) origin https://github.com/hanako/cafe.git (push) upstream https://github.com/sensei/cafe (fetch) upstream https://github.com/sensei/cafe (push) hanako@MacBook-Air cafe % git branch -vv * main b2ea435 [upstream/main] first
最後の行に注意します。git remote rename は、その送り先に関係する設定もまとめて新しい名前に書き換えます(git の説明書 git help remote の rename の項にあります)。そのため、main のつなぎ先は upstream/main のままです。この状態で -u を付けずに git push を打つと、自分の origin ではなく upstream に送ろうとします。自分の送り先に初めて送る時は、-u を付けて、つなぎ先を origin に替えます。
自分の送り先に初めて送る(つなぎ先も替える)
git push -u origin main手元の練習用の送り先で試したところ、branch 'main' set up to track 'origin/main'. と出て、つなぎ先が origin に替わりました。
clone 元の URL が要らないなら、rename ではなく set-url で origin を自分の URL に付け替えるだけでも足ります。
origin という名前を使わずに、git remote add github https://… のように別の名前で足すこともできます。ただ、多くの案内は origin を前提に書かれているので、初めのうちは「自分の送り先は origin」にそろえておくと迷いにくいです。
git remote -vいま登録されている URL を見る- 合っていれば何もしないそのまま
git pushへ - 違えば
git remote set-url origin URL名前はそのまま、URL だけ替える - clone 元なら
rename origin upstream自分のをadd originしてpush -u origin main
初めての push で止まる他の原因(コミットが無い、has no upstream branch など)は git push できない(初回) にまとめています。送り先と手元のリポジトリの関係は、図解の Git 入門 で1本の流れとして説明しています。
この記事の画面は、macOS 26.6.2 / zsh 5.9 / git 2.51.0 で実際に出したものです。
Gitで、ほかによくあるつまずき
git branch -d で error: the branch 'x' is not fully merged と出て消せないbranch -d で not fully mergedgit switch で fatal: invalid reference: <name> と出て枝に移れないgit switch で invalid reference消してしまったファイルを、git で元に戻したいgit で削除したファイルを戻すremote: Repository not found. / fatal: repository '...' not foundgit clone で Repository not foundgit merge で Already up to date. と出て、取り込みたい変更が入らないmerge で Already up to date.[email protected]: Permission denied (publickey).Permission denied (publickey)