1.

git「fatal: remote origin already exists」の原因と対処(remove/set-url)

編集
この記事の要点
  • git remote add origin 実行時に出るエラー「remote origin already exists
  • 原因: 既に origin が登録済み(git init 直後の clone や、過去の add の残り)
  • 対処A: URL を変えるだけなら git remote set-url origin
  • 対処B: 一旦削除して追加し直すなら git remote remove origingit remote add origin
  • 別名で追加もできる: git remote add upstream (fork 元のとき定番)

このエラーの概要

git remote add origin を実行したときに出ます:

$ git remote add origin https://github.com/user/repo.git
fatal: remote origin already exists.

# 確認すると確かにある
$ git remote -v
origin  https://github.com/old-user/old-repo.git (fetch)
origin  https://github.com/old-user/old-repo.git (push)

git にとって 「リモート」は名前付きの参照です。origin という名前が既に使われていれば add は失敗します。これは安全機構で、知らずに別 URL に上書きされるのを防いでくれます。

解決パターン3つ

やりたいことコマンド備考
origin の URL を変えたいだけgit remote set-url origin 最も安全。履歴・upstream 維持
origin を消して付け直すgit remote remove origingit remote add origin upstream 設定もリセットされる
別名で並行追加git remote add upstream fork 元参照などで定番

対処A: set-url で URL だけ変更(推奨)

# 現在の origin URL を確認
git remote -v
git remote get-url origin

# URL を変更
git remote set-url origin https://github.com/new-user/new-repo.git

# SSH ↔ HTTPS 切替
git remote set-url origin git@github.com:user/repo.git

# fetch 用と push 用で別 URL にする (ミラーリング時)
git remote set-url --push origin git@gitlab.internal:user/repo.git

# 確認
git remote -v
# origin  https://github.com/new-user/new-repo.git (fetch)
# origin  https://github.com/new-user/new-repo.git (push)

# 試しに fetch
git fetch origin

対処B: 削除して付け直し

# remove (rm) でリモート削除
git remote remove origin
# あるいは古い書き方
git remote rm origin

# 確認(何も出ない)
git remote -v

# 改めて追加
git remote add origin https://github.com/user/repo.git

# 初回 push で upstream 設定
git push -u origin main

# upstream 設定だけ後から付けることもできる
git branch --set-upstream-to=origin/main main

対処C: 別名で追加(fork 開発で多用)

OSS の fork 開発では、自分の fork を origin、本家を upstream として両方扱います:

# 自分の fork
git remote add origin git@github.com:my-user/my-fork.git

# 本家を upstream として追加
git remote add upstream https://github.com/original-author/repo.git

git remote -v
# origin    git@github.com:my-user/my-fork.git (fetch)
# origin    git@github.com:my-user/my-fork.git (push)
# upstream  https://github.com/original-author/repo.git (fetch)
# upstream  https://github.com/original-author/repo.git (push)

# 本家の最新を取り込む
git fetch upstream
git checkout main
git merge upstream/main
git push origin main

git clone 直後にこのエラーが出るのはなぜ?

git clone自動的に origin を設定します。そのため clone 直後にもう一度 git remote add origin すると重複エラーになります。

git clone https://github.com/user/repo.git
cd repo
git remote -v
# origin  https://github.com/user/repo.git (fetch)  ← 既にある
# origin  https://github.com/user/repo.git (push)

# ここで以下を実行するとエラー
git remote add origin https://github.com/user/repo.git
# fatal: remote origin already exists.

リモート一覧と詳細表示

# 一覧(URL 付き)
git remote -v

# 名前のみ
git remote

# 詳細
git remote show origin
# * remote origin
#   Fetch URL: ...
#   Push  URL: ...
#   HEAD branch: main
#   Remote branches:
#     main tracked
#   Local branch configured for 'git pull':
#     main merges with remote main
#   Local ref configured for 'git push':
#     main pushes to main (up to date)

# 名前変更(origin → upstream に rename したいとき)
git remote rename origin upstream

関連エラーと対処

エラー状況対処
error: No such remote: 'origin'削除済み or 未設定git remote add origin URL
fatal: refusing to merge unrelated histories新規リモートに既存ローカルを pushgit pull --allow-unrelated-histories origin main
error: src refspec main does not match anycommit が一つも無いgit add . && git commit -m init
! [rejected] main -> main (fetch first)リモートに先行コミットありgit pull --rebase 後 push

FAQ

Q: set-url したのに古い URL に push されている
A: --push オプション付きで別 URL を設定した可能性。git remote -v で (fetch) と (push) を両方確認してください。

Q: リモートを丸ごとリセットしたい
A: .git/config を直接編集して [remote "origin"] セクションを削除する手もありますが、git remote remove origin の方が安全です。

Q: GitHub から GitLab に移行したい
A: git remote set-url origin git@gitlab.com:user/repo.gitgit push --all + git push --tags で全ブランチとタグを移行できます。

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. fatal: remote origin already exists.
  2. fatal: '~' does not appear to be a git repository
  3. Cannot rebase: You have unstaged changes. Please commit or stash them.
  4. remote: error: denying non-fast-forward refs/heads/master (you should pull first)
  5. error: pathspec ... did not match any file(s) known to git.
  6. The following untracked working tree files would be overwritten by checkout
  7. fatal: Not a valid object name: 'master'.
  8. Unlink of file 'ファイル名' failed. Should I try again? (y/n)
  9. Another git process seems to be running in this repository, ~
  10. error: Your local changes to the following files would be overwritten by checkout: