この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:10
ページ更新者:guest
更新日時:2026-06-11 07:12:00

タイトル: リモートリポジトリをローカルリポジトリとしてクローンする方法
SEOタイトル: git clone の使い方完全版(HTTPS/SSH、--depth、--branch、認証エラー対処)

この記事の要点
  • 基本: git clone <URL> [<dir>] でリモートを丸ごとローカルへ複製
  • HTTPS vs SSH: 個人/CI は HTTPS+PAT、開発機は SSH 鍵が定番
  • 巨大リポジトリ--depth 1 shallow clone で時間と容量を大幅削減
  • 特定ブランチだけ: --branch <name> --single-branch
  • submodule あり: --recurse-submodules(または --recursive
  • 社内プロキシ環境: git config --global http.proxy http://proxy:8080

基本のクローン

# 1) HTTPS でクローン(最も基本)
git clone https://github.com/laravel/laravel.git

# 2) ディレクトリ名を指定
git clone https://github.com/laravel/laravel.git my-project

# 3) SSH でクローン(鍵認証)
git clone git@github.com:laravel/laravel.git

# 4) ローカルディレクトリをクローン(バックアップ等)
git clone /srv/git/myrepo.git
git clone file:///srv/git/myrepo.git

HTTPS と SSH の違い

項目HTTPSSSH
URL 形式https://github.com/...git@github.com:...
認証ユーザー名 + Personal Access Token (PAT)SSH 鍵ペア(公開鍵を GitHub に登録)
プロキシ越え容易(HTTP プロキシ対応)困難(22 番ポートが塞がれていることが多い)
CI / Docker環境変数で PAT を渡しやすい鍵ファイル配置が手間
推奨用途CI / 一時環境 / プロキシ環境個人の開発 PC

主要オプション

# 特定ブランチをクローン
git clone --branch develop https://github.com/foo/bar.git
git clone -b v1.2.3 https://github.com/foo/bar.git    # タグも可

# そのブランチだけ(他ブランチを取得しない、軽量化)
git clone --branch main --single-branch https://github.com/foo/bar.git

# Shallow clone(最新 1 コミットだけ、巨大リポジトリで時短)
git clone --depth 1 https://github.com/torvalds/linux.git

# Shallow + 履歴を後で追加
cd linux
git fetch --depth 100
git fetch --unshallow      # 全履歴に展開

# submodule も同時に取得
git clone --recurse-submodules https://github.com/foo/bar.git
# 旧: --recursive エイリアス

# Bare リポジトリ(作業ツリー無し、サーバー用)
git clone --bare https://github.com/foo/bar.git

# ミラー(全 ref を取得、移行用)
git clone --mirror https://github.com/foo/bar.git

認証: GitHub PAT (Personal Access Token)

2021 年以降、GitHub は HTTPS でのパスワード認証を廃止しています。PAT を発行してパスワード代わりに使うのが正しい運用です。

# GitHub: Settings → Developer settings → Personal access tokens → Generate new token
# 必要な scope: repo (private 取得時)

# クローン時にユーザー名 + PAT を入力
git clone https://github.com/foo/private-repo.git
# Username: your-username
# Password: ghp_xxxxxxxxxxxxxxxxxxxx   ← PAT を貼り付け

# URL に直接埋め込む(CI 用、履歴に残るので注意)
git clone https://ghp_xxxx@github.com/foo/private-repo.git

# 認証情報を OS のキーチェーンに記録
git config --global credential.helper store           # 平文(推奨しない)
git config --global credential.helper osxkeychain     # macOS
git config --global credential.helper manager-core    # Windows

認証: SSH 鍵セットアップ

# 1) 鍵ペア生成(Ed25519 推奨)
ssh-keygen -t ed25519 -C &quot;your_email@example.com&quot;
# パスフレーズを入力(空でも可)
# → ~/.ssh/id_ed25519 (秘密鍵) と id_ed25519.pub (公開鍵) が生成

# 2) 公開鍵を GitHub に登録
cat ~/.ssh/id_ed25519.pub
# → 内容をコピーして GitHub: Settings → SSH and GPG keys → New SSH key

# 3) 接続テスト
ssh -T git@github.com
# → Hi username! You've successfully authenticated, ...

# 4) クローン
git clone git@github.com:foo/bar.git

プロキシ環境でのクローン

# プロキシ経由で HTTPS clone
git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy http://proxy.example.com:8080

# 認証付きプロキシ
git config --global http.proxy http://user:pass@proxy.example.com:8080

# 特定ホストだけプロキシを使う/外す
git config --global http.https://github.com/.proxy http://proxy:8080

# 解除
git config --global --unset http.proxy
git config --global --unset https.proxy

# プロキシで SSL エラーが出るとき(社内 CA 等)
git config --global http.sslCAInfo /path/to/ca-bundle.crt
# やむを得ない時のみ(推奨しない)
git config --global http.sslVerify false

よくあるエラーと対処

エラー原因対処
fatal: Authentication failedパスワード認証廃止 / 期限切れ PATPAT を再発行 → credential helper を更新
Permission denied (publickey)SSH 鍵未登録 / 鍵パス間違いssh -T git@github.com で疎通確認、ssh-add
SSL certificate problem: unable to get local issuer社内 CA / プロキシCA bundle 設定(上記)
fatal: destination path 'xxx' already exists同名ディレクトリ存在別名指定 or 既存削除
fatal: repository not foundURL ミス / private に未認証URL 確認、PAT/SSH 鍵を確認
クローン途中で RPC failed大容量 / 不安定回線git clone --depth 1fetch --unshallow

クローン後の確認

cd my-project

# リモート確認
git remote -v
# origin  https://github.com/foo/bar.git (fetch)
# origin  https://github.com/foo/bar.git (push)

# ブランチ確認
git branch -a
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/develop

# 履歴確認
git log --oneline -10

FAQ

Q: HTTPS から SSH に切り替えたい
A: git remote set-url origin git@github.com:foo/bar.gitgit remote -v で確認。

Q: 巨大リポで --depth 1 したけど全履歴が欲しくなった
A: git fetch --unshallow で全履歴を後追い取得できます。

Q: 社内 Git サーバーが自己署名証明書で SSL エラー
A: 正しい運用は CA 証明書を http.sslCAInfo に設定。緊急時は GIT_SSL_NO_VERIFY=1 git clone ...