Git remoteとは?リモートリポジトリの追加・削除・確認方法を初心者向けに解説
Git remoteの基本的な使い方から実践的な管理方法まで初心者向けに解説します。リモートリポジトリの追加・削除・URL変更、複数リモートの活用方法を学び、チーム開発やオープンソース貢献に役立てましょう。
Git remoteとは
Git remoteは、リモートリポジトリ(GitHubやGitLabなどのサーバー上のリポジトリ)を管理するコマンドです。ローカルリポジトリとリモートリポジトリを紐付けることで、git pushやgit pullでコードを共有できるようになります。
リモートリポジトリには「origin」などの名前(エイリアス)を付けて管理します。この名前を使ってURLを指定せずに操作できるため、長いURLを毎回入力する必要がありません。
リモートリポジトリの仕組み
ローカルリポジトリとリモートリポジトリの関係を図で示します。
| 用語 | 説明 |
|---|---|
| origin | デフォルトのリモート名。通常は自分のリポジトリを指す |
| upstream | フォーク元のリポジトリを指すときによく使われる名前 |
| リモート追跡ブランチ | リモートの状態を追跡するローカルブランチ(origin/mainなど) |
リモートの確認方法
登録されているリモートを一覧表示
# リモート名の一覧を表示
git remote
# URLも含めて表示
git remote -v
git remote -vの出力例:
origin https://github.com/username/repo.git (fetch)
origin https://github.com/username/repo.git (push)
fetchとpushの両方のURLが表示されます。通常は同じURLですが、別々に設定することも可能です。
リモートの詳細情報を確認
# 特定のリモートの詳細を表示
git remote show origin
出力例:
* remote origin
Fetch URL: https://github.com/username/repo.git
Push URL: https://github.com/username/repo.git
HEAD branch: main
Remote branches:
main tracked
develop 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)
この情報から、リモートブランチの状態やpull/pushの設定を確認できます。
リモートの追加方法
新しいリモートを追加する
# git remote add <リモート名> <URL>
git remote add origin https://github.com/username/repo.git
ローカルでgit initした後、GitHubにリポジトリを作成して紐付ける場合によく使います。
# 一般的なワークフロー
git init
git add .
git commit -m "初回コミット"
git remote add origin https://github.com/username/repo.git
git push -u origin main
フォーク元をupstreamとして追加
オープンソースプロジェクトにコントリビュートする場合、フォーク元のリポジトリをupstreamとして追加します。
# フォーク元を追加
git remote add upstream https://github.com/original-owner/repo.git
# 確認
git remote -v
# origin https://github.com/your-username/repo.git (fetch)
# origin https://github.com/your-username/repo.git (push)
# upstream https://github.com/original-owner/repo.git (fetch)
# upstream https://github.com/original-owner/repo.git (push)
これにより、フォーク元の最新変更を取り込めるようになります。
リモートの変更・削除
リモートURLを変更する
リポジトリの移動やHTTPS↔SSH切り替えでURLを変更する場合に使います。
# URLを変更
git remote set-url origin https://github.com/new-username/repo.git
# SSHに変更
git remote set-url origin git@github.com:username/repo.git
# 変更を確認
git remote -v
リモート名を変更する
# リモート名を変更
git remote rename origin github
# 確認
git remote -v
# github https://github.com/username/repo.git (fetch)
# github https://github.com/username/repo.git (push)
リモートを削除する
# リモートを削除
git remote remove upstream
# または
git remote rm upstream
削除しても、リモート追跡ブランチ(origin/mainなど)はローカルに残ります。不要な場合は別途削除してください。
# リモート追跡ブランチを削除
git branch -dr origin/old-branch
複数リモートの活用
オープンソース開発での活用
オープンソースプロジェクトへのコントリビュートでは、複数のリモートを使い分けます。
| リモート | 用途 | 操作 |
|---|---|---|
| origin | 自分のフォーク | push先 |
| upstream | フォーク元 | 最新変更の取得元 |
upstreamの変更を取り込む
フォーク元の最新変更を自分のリポジトリに反映する手順です。
# 1. upstreamの変更を取得
git fetch upstream
# 2. mainブランチに切り替え
git checkout main
# 3. upstreamの変更をマージ
git merge upstream/main
# 4. 自分のoriginに反映
git push origin main
fetchとpullの違いを理解しておくと、この操作がより明確になります。
複数のホスティングサービスを使う
同じリポジトリをGitHubとGitLabの両方で管理する場合:
# GitHubをoriginとして設定
git remote add origin https://github.com/username/repo.git
# GitLabをミラーとして追加
git remote add gitlab https://gitlab.com/username/repo.git
# 両方にプッシュ
git push origin main
git push gitlab main
よく使うコマンド一覧
| 操作 | コマンド |
|---|---|
| リモート一覧表示 | git remote -v |
| リモート追加 | git remote add <名前> <URL> |
| リモートURL変更 | git remote set-url <名前> <URL> |
| リモート名変更 | git remote rename <旧名> <新名> |
| リモート削除 | git remote remove <名前> |
| リモート詳細表示 | git remote show <名前> |
| 不要な追跡ブランチを削除 | git remote prune <名前> |
リモート追跡ブランチの整理
リモートで削除されたブランチがローカルの追跡ブランチに残っている場合、pruneで整理できます。
# 削除されたリモートブランチの追跡を解除
git remote prune origin
# fetch時に自動でpruneする
git fetch --prune origin
# 常に自動pruneする設定
git config --global fetch.prune true
初心者が混乱しやすいポイント
originは特別な名前ではない
originは単なるデフォルトの名前であり、特別な意味はありません。git cloneしたときに自動的に付けられる名前です。
# originという名前は変更可能
git remote rename origin github
# 任意の名前でリモートを追加できる
git remote add myserver https://myserver.com/repo.git
チームによっては、originではなく別の命名規則を使うこともあります。
リモートを追加しただけではデータは同期されない
git remote addはURLの登録だけを行います。実際にデータを取得するにはgit fetchが必要です。
# リモートを追加(URLを登録しただけ)
git remote add upstream https://github.com/original/repo.git
# データを取得
git fetch upstream
# これでupstream/mainなどが使えるようになる
git log upstream/main
HTTPSとSSHの違い
リモートURLにはHTTPSとSSHの2種類があります。
| プロトコル | URL形式 | 認証方法 |
|---|---|---|
| HTTPS | https://github.com/user/repo.git | ユーザー名・パスワード(またはトークン) |
| SSH | git@github.com:user/repo.git | SSHキー |
# 現在のプロトコルを確認
git remote -v
# HTTPSからSSHに変更
git remote set-url origin git@github.com:username/repo.git
# SSHからHTTPSに変更
git remote set-url origin https://github.com/username/repo.git
SSHを使うと、pushのたびにパスワードを入力する必要がなくなります。
fetchとpushのURLが異なる場合
特殊なケースですが、fetchとpushで異なるURLを設定できます。
# pushURLだけを別に設定
git remote set-url --push origin https://github.com/other/repo.git
# 確認
git remote -v
# origin https://github.com/username/repo.git (fetch)
# origin https://github.com/other/repo.git (push)
通常は同じURLを使いますが、読み取り専用のミラーからfetchし、別のリポジトリにpushするような構成で使われることがあります。
トラブルシューティング
リモートが存在しないエラー
fatal: 'origin' does not appear to be a git repository
原因と対処:
| 原因 | 対処 |
|---|---|
| リモートが登録されていない | git remote add origin <URL>で追加 |
| リモート名が間違っている | git remote -vで確認 |
| URLが間違っている | git remote set-urlで修正 |
認証エラー
remote: Permission denied
fatal: unable to access '...': The requested URL returned error: 403
| 原因 | 対処 |
|---|---|
| 認証情報が古い | 資格情報を更新、またはSSHに切り替え |
| リポジトリへのアクセス権がない | リポジトリの権限を確認 |
| URLが間違っている | git remote -vで確認 |
# 資格情報をクリアして再認証(macOS)
git credential-osxkeychain erase
host=github.com
protocol=https
# Windowsの場合は資格情報マネージャーから削除
リモートブランチが表示されない
# リモートの情報を更新
git fetch --all
# 追跡ブランチを確認
git branch -r
# 特定のリモートブランチを追跡するローカルブランチを作成
git checkout -b feature origin/feature
実践的な活用例
GitHubでフォークしたリポジトリの初期設定
# 1. フォークしたリポジトリをクローン
git clone https://github.com/your-username/project.git
cd project
# 2. フォーク元をupstreamとして追加
git remote add upstream https://github.com/original-owner/project.git
# 3. 確認
git remote -v
# origin https://github.com/your-username/project.git (fetch)
# origin https://github.com/your-username/project.git (push)
# upstream https://github.com/original-owner/project.git (fetch)
# upstream https://github.com/original-owner/project.git (push)
# 4. upstreamの最新を取得
git fetch upstream
ローカルプロジェクトをGitHubに公開
# 1. ローカルでGitリポジトリを初期化
git init
git add .
git commit -m "初回コミット"
# 2. GitHubでリポジトリを作成(ブラウザまたはgh CLI)
gh repo create my-project --public
# 3. リモートを追加してプッシュ
git remote add origin https://github.com/username/my-project.git
git push -u origin main
-uオプションは上流ブランチを設定し、次回からgit pushだけでプッシュできるようにします。
まとめ
Git remoteは、リモートリポジトリを管理するためのコマンドです。
| 操作 | コマンド |
|---|---|
| 一覧表示 | git remote -v |
| 追加 | git remote add <名前> <URL> |
| URL変更 | git remote set-url <名前> <URL> |
| 削除 | git remote remove <名前> |
| 詳細表示 | git remote show <名前> |
- originはデフォルトのリモート名で、特別な意味はない
- オープンソース開発ではorigin(自分のフォーク)とupstream(フォーク元)を使い分ける
- HTTPSとSSHでURLの形式が異なる
- リモートを追加した後は
git fetchでデータを取得する必要がある
リモートリポジトリの管理を理解することで、チーム開発やオープンソースへのコントリビュートがスムーズに行えるようになります。特にフォーク元との同期は、オープンソース開発で頻繁に使う操作なので、ぜひ覚えておきましょう。
編集部