git remote
リモートリポジトリの追加・削除・確認・URLの変更などを管理します。GitHub や GitLab などの接続先を設定するときに使います。
構文
git remote <subcommand> [<options>] git remote はリモートリポジトリの接続先(URL)を管理するコマンドです。GitHub や GitLab のリポジトリを「名前」で扱えるようにします。origin という名前が最もよく使われます。
リモートの一覧を確認する
# リモートの名前だけ表示
git remote
# 名前と URL を一緒に表示(フェッチ用・プッシュ用)
git remote -v
実行例:
origin https://github.com/yourname/your-repo.git (fetch)
origin https://github.com/yourname/your-repo.git (push)
リモートを追加する
git remote add <名前> <URL>
# 例: origin という名前で GitHub のリポジトリを追加
git remote add origin https://github.com/yourname/your-repo.git
# 例: フォーク元を upstream として追加
git remote add upstream https://github.com/original/repo.git
origin は慣習的な名前です。プッシュやプル時に毎回 URL を入力しなくても済むようになります。
リモートを削除する
git remote remove <名前>
# 例
git remote remove upstream
リモートの名前を変更する
git remote rename <旧名前> <新名前>
# 例: origin を main-repo に変更
git remote rename origin main-repo
リモートの URL を変更する
git remote set-url <名前> <新しいURL>
# 例: HTTPS から SSH に切り替え
git remote set-url origin [email protected]:yourname/your-repo.git
# 例: リポジトリの移転先に URL を更新
git remote set-url origin https://github.com/yourname/new-repo.git
HTTPS 認証から SSH 鍵認証に変えるとき、またはリポジトリの URL が変わったときによく使います。
リモートの詳細を確認する
git remote show <名前>
# 例
git remote show origin
実行すると、トラッキングブランチの状態やプッシュ先の情報などが詳しく表示されます。
よくある使い方の流れ
# 1. ローカルリポジトリを作成
git init
# 2. GitHub に作成したリポジトリを origin として登録
git remote add origin https://github.com/yourname/your-repo.git
# 3. 確認
git remote -v
# 4. 初回プッシュ
git push -u origin main