5.

git clone の使い方完全版(HTTPS/SSH、--depth、--branch、認証エラー対処)

編集
この記事の要点
  • 基本: git clone [] でリモートを丸ごとローカルへ複製
  • HTTPS vs SSH: 個人/CI は HTTPS+PAT、開発機は SSH 鍵が定番
  • 巨大リポジトリ--depth 1 shallow clone で時間と容量を大幅削減
  • 特定ブランチだけ: --branch --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 "your_email@example.com"
# パスフレーズを入力(空でも可)
# → ~/.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 ...

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. 用語一覧
  2. エラー一覧
  3. git本体のインストール(Linux)
  4. Linuxサーバーへのgit導入とクライアントのセットアップ
  5. リモートリポジトリをローカルリポジトリとしてクローンする方法
  6. リモートとローカルのリポジトリを同期(pull)する方法
  7. 設定の確認
  8. gitユーザー名とemailの設定
  9. リモートリポジトリの作成
  10. ローカルリポジトリの作成
  11. 新規ファイル/ディレクトリをインデックスに登録
  12. インデックスの登録状態を確認
  13. ローカルリポジトリの変更をコミット
  14. コミット履歴の確認
  15. クライアントからリモートリポジトリの接続設定、確認、削除
  16. リポジトリへのプッシュ
  17. リモートリポジトリからクライアントへのSSHクローン
  18. リモートとローカルの差分表示
  19. バージョンの確認
  20. プロキシの設定
  21. ローカルをリモートリポジトリの状態に戻す
  22. ブランチの作成, 一覧表示, 切り替え
  23. ブランチのマージと削除
  24. リベース
  25. .gitignoreの書き方
  26. .gitignoreの設定が反映されない場合
  27. 特定のファイルをgitの管理から外す方法
  28. 参照(ORIG_HEAD, HEAD, FETCH_HEAD)
  29. git rm [-r --cached] の取り消し
  30. 一部のディレクトリ/ファイルのみをリポジトリから復元する方法
  31. ローカルとリモートリポジトリの有無を同期
  32. pushの取消し方法
  33. マージツールの起動方法
  34. Gitで「MERGING」の状態a