ストーリー
現在の状態
graph LR
subgraph LocalSide["ローカル"]
Local["my-first-git<br/>(コミットあり)"]
end
subgraph RemoteSide["GitHub"]
Remote["my-first-repo<br/>(空)"]
end
Local -.-|"?"| Remote
style LocalSide fill:#d1fae5,stroke:#059669,stroke-width:2px,color:#064e3b
style RemoteSide fill:#dbeafe,stroke:#2563eb,stroke-width:2px,color:#1e40af
style Local fill:#f0fdf4,stroke:#16a34a,color:#14532d
style Remote fill:#fee2e2,stroke:#dc2626,color:#991b1b
まだ繋がっていない!
git remoteとは
git remote
は、ローカルリポジトリにリモートリポジトリの情報を登録するコマンドです。
「このリポジトリはGitHubのこのURLと連携するよ」と教えてあげるイメージです。
リモートを登録する
コマンドの形式
git remote add <名前> <URL>
<名前>: リモートの名前(通常はorigin)<URL>: GitHubのリポジトリURL
実際のコマンド
git remote add origin https://github.com/username/my-first-repo.git
usernameの部分はあなたのGitHubユーザー名に置き換えてください。
実際にやってみよう
Step 1: ローカルリポジトリに移動
cd ~/my-first-git
Step 2で作成したフォルダです。
Step 2: 現在のリモート設定を確認
git remote -v
出力:
(何も表示されない = リモートが設定されていない)
Step 3: リモートを追加
git remote add origin https://github.com/username/my-first-repo.git
URLはGitHubで作成したリポジトリのものに置き換えてください。
Step 4: 設定を確認
git remote -v
出力:
origin https://github.com/username/my-first-repo.git (fetch)
origin https://github.com/username/my-first-repo.git (push)
origin という名前でリモートが登録されました!
出力の意味
origin https://github.com/... (fetch) ← 取得用
origin https://github.com/... (push) ← 送信用
fetch:git pullなどで使用されるURLpush:git pushで使用されるURL
通常は同じURLが設定されます。
originとは何か
origin はリモートリポジトリの名前です。
git remote add origin https://...
^^^^^^
この部分が名前
なぜoriginなのか
- 「起源」「元」という意味
- Gitの慣習的なデフォルト名
- 最初に登録するリモートにはこの名前を使う
別の名前も使える
git remote add upstream https://...
git remote add backup https://...
今は
originだけ覚えておけばOKです。
リモート情報を詳しく確認
リモートの詳細を表示
git remote show origin
出力例:
* remote origin
Fetch URL: https://github.com/username/my-first-repo.git
Push URL: https://github.com/username/my-first-repo.git
HEAD branch: (unknown)
...
リモートの一覧を表示
git remote
出力:
origin
リモートの設定を変更する
URLを変更
git remote set-url origin <新しいURL>
例:
git remote set-url origin https://github.com/username/new-repo.git
リモートを削除
git remote remove origin
削除後は
git pushができなくなります。
リモートの名前を変更
git remote rename origin new-name
HTTPS認証の準備
GitHub に push するには認証が必要です。
個人アクセストークン(PAT)
2021年8月以降、GitHubはパスワード認証を廃止しました。代わりに個人アクセストークンを使います。
トークンの作成手順
- GitHub → Settings → Developer settings
- Personal access tokens → Tokens (classic)
- Generate new token (classic)
- Note: 「Git CLI」など分かりやすい名前
- Expiration: 有効期限を選択
- Scopes:
repoにチェック - Generate token
- トークンをコピーして安全な場所に保存!
トークンは一度しか表示されません。必ずコピーしてください。
トークンの使い方
git push
時にパスワードを求められたら、パスワードの代わりにトークンを入力します。
認証情報をキャッシュする
毎回トークンを入力するのは面倒なので、キャッシュしましょう。
Mac
git config --global credential.helper osxkeychain
Windows
git config --global credential.helper wincred
Linux
git config --global credential.helper store
これで一度入力すれば、次回以降は自動で認証されます。
よくあるエラー
「remote origin already exists」
git remote add origin https://...
エラー:
error: remote origin already exists.
→ すでに origin が登録されています。URLを変更する場合:
git remote set-url origin <新しいURL>
「repository not found」
git push
エラー:
remote: Repository not found.
fatal: repository 'https://...' not found
→ URLが間違っているか、アクセス権限がありません。
git remote -v # URLを確認
ハンズオン
以下のコマンドを順番に実行してください。
# 1. ローカルリポジトリに移動
cd ~/my-first-git
# 2. 現在のリモート設定を確認(空のはず)
git remote -v
# 3. リモートを追加(URLは自分のものに置き換え)
git remote add origin https://github.com/YOUR_USERNAME/my-first-repo.git
# 4. 設定を確認
git remote -v
# 5. 詳細を確認
git remote show origin
まとめ
| コマンド | 説明 |
|---|---|
git remote -v | 登録されているリモートを表示 |
git remote add origin URL | リモートを追加 |
git remote set-url origin URL | URLを変更 |
git remote remove origin | リモートを削除 |
接続後の状態
graph LR
subgraph LocalSide2["ローカル"]
Local2["my-first-git<br/>(コミットあり)"]
end
subgraph RemoteSide2["GitHub"]
Remote2["my-first-repo<br/>(空)"]
end
Local2 <-->|"origin"| Remote2
style LocalSide2 fill:#d1fae5,stroke:#059669,stroke-width:2px,color:#064e3b
style RemoteSide2 fill:#dbeafe,stroke:#2563eb,stroke-width:2px,color:#1e40af
style Local2 fill:#f0fdf4,stroke:#16a34a,color:#14532d
style Remote2 fill:#e0e7ff,stroke:#4f46e5,color:#312e81
繋がった!(でもまだ同期していない)
チェックリスト
-
git remote add originでリモートを登録できた -
git remote -vで設定を確認できた - 個人アクセストークンを作成できた(または方法を理解した)
次のステップへ
ローカルとリモートが接続されました!
次のセクションでは、git push
を使って、ローカルの変更をGitHubにアップロードします。
あなたのコードがついにクラウドに保存されます!
推定読了時間: 30分