「大きなリポジトリのcloneに時間がかかりすぎる…」「必要なブランチだけ取得したい」「間違ったブランチをcloneしてしまった」こんな経験はありませんか?
実は、git cloneには特定のブランチだけを効率的に取得する方法があります。適切なオプションを使えば、数分かかっていたcloneが数秒で完了することも珍しくありません。
この記事では、Git cloneでブランチを指定するすべての方法を、初心者から上級者まで理解できるように徹底解説します。基本の-bオプションから、高速化テクニック、トラブル対処まで完全網羅します。
- 通常のgit cloneの問題点
- 基本:-b オプションでブランチ指定
- 高速化:–single-branch で指定ブランチのみ取得
- さらに高速化:–depth で履歴を制限
- タグを指定してclone
- 実践的な使用例
- clone後に他のブランチを追加する方法
- フォルダ名を指定してclone
- プライベートリポジトリでのブランチ指定
- よくあるトラブルと解決方法
- パフォーマンス比較表
- Git エイリアス設定
- よくある質問(FAQ)
- Q1:-bオプションなしでcloneした場合、どのブランチが取得されますか?
- Q2:–single-branchでcloneした後、やっぱり他のブランチも欲しくなりました。
- Q3:–depth 1でcloneした後、古い履歴も見たくなりました。
- Q4:タグとブランチ、両方に同じ名前があったらどうなりますか?
- Q5:clone中にキャンセルしました。途中から再開できますか?
- Q6:複数のブランチを同時に指定してcloneできますか?
- Q7:–single-branchとPRブランチの相性は?
- Q8:大文字小文字を間違えた場合は?
- Q9:cloneに失敗した場合、リトライは自動的にされますか?
- Q10:社内プロキシ経由でcloneする場合は?
- まとめ:最適なcloneコマンドの選び方
通常のgit cloneの問題点

デフォルトのgit cloneは全部取得する
まず、通常のgit cloneが何をしているか理解しましょう。
通常のclone:
git clone https://github.com/user/repository.git
何が起こるか:
取得されるもの:
✅ デフォルトブランチ(mainまたはmaster)
✅ すべてのブランチの情報
✅ すべてのタグ
✅ 完全な履歴(初回コミットから最新まで)
結果:
📁 数百MB~数GBのダウンロード
⏱️ 数分~数十分の待ち時間
💾 大量のディスク使用
具体例:大規模リポジトリの場合
# Linux Kernelをclone(約3.5GB、数千万行)
$ time git clone https://github.com/torvalds/linux.git
Cloning into 'linux'...
remote: Enumerating objects: 9000000, done.
remote: Counting objects: 100%...
Receiving objects: 100% (9000000/9000000), 3.48 GiB | 10.00 MiB/s, done.
Resolving deltas: 100%...
real 15m32.123s # 15分以上!
問題点:
- ❌ 時間がかかりすぎる
- ❌ 不要なブランチも全部取得
- ❌ ディスク容量を圧迫
- ❌ ネットワーク帯域を無駄に消費
ブランチ指定が必要な典型的シーン
シーン1:レビュー用に一時的に確認
同僚:「feature/new-uiブランチ、レビューお願い!」
あなた:「すぐ見ます」
→ mainブランチは不要、feature/new-uiだけ欲しい
シーン2:本番環境のソースを確認
障害発生:「本番で動いてるコード確認したい」
→ productionブランチだけ必要、開発ブランチは不要
シーン3:CI/CDでの自動デプロイ
# デプロイスクリプト
git clone [repository]
→ 毎回全ブランチ取得は非効率
シーン4:特定バージョンのソース確認
「バージョン2.0のコード見たい」
→ タグv2.0.0だけ取得したい
基本:-b オプションでブランチ指定
基本構文
git clone -b <ブランチ名> <リポジトリURL>
具体例:
# developブランチを指定してclone
git clone -b develop https://github.com/user/repository.git
動作の詳細
実行すると何が起こるか:
$ git clone -b feature/login https://github.com/user/app.git
Cloning into 'app'...
remote: Enumerating objects: 1000, done.
remote: Counting objects: 100% (1000/1000), done.
Receiving objects: 100% (1000/1000), done.
# 結果
$ cd app
$ git branch
* feature/login # ← チェックアウト済み
$ git branch -a
* feature/login
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/develop
remotes/origin/feature/login
# ↑ 全ブランチの情報は取得されている
重要ポイント:
- ✅ 指定ブランチがチェックアウトされた状態
- ⚠️ 全ブランチの情報は取得される(サイズ削減なし)
- ✅ 他のブランチにも切り替え可能
実例:よくあるブランチ名
# 開発ブランチ
git clone -b develop https://github.com/company/project.git
# 本番ブランチ
git clone -b production https://github.com/company/project.git
# ステージング環境
git clone -b staging https://github.com/company/project.git
# 機能開発ブランチ
git clone -b feature/payment-system https://github.com/team/app.git
# バグ修正ブランチ
git clone -b fix/issue-123 https://github.com/team/app.git
# リリースブランチ
git clone -b release/v2.0 https://github.com/company/product.git
-bオプションの使い分け
| 状況 | -bの使用 |
|---|---|
| 他のブランチにも切り替える予定 | ✅ 適している |
| 容量・速度を重視 | ❌ 不十分(他の手法を) |
| 一時的な作業 | △ 可能だが過剰 |
| 長期的な開発 | ✅ 最適 |
高速化:–single-branch で指定ブランチのみ取得
–single-branch の威力
git clone -b <ブランチ名> --single-branch <リポジトリURL>
通常との違い:
【-b のみ】
取得:全ブランチの情報 + 履歴
サイズ:100%
【-b + --single-branch】
取得:指定ブランチのみ
サイズ:約20-50%(リポジトリによる)
実例:速度比較
テストリポジトリ:Node.js(約1.5GB)
# パターン1:通常のclone
$ time git clone https://github.com/nodejs/node.git
real 3m15.234s
# パターン2:-bのみ
$ time git clone -b main https://github.com/nodejs/node.git
real 3m12.108s # ほぼ変わらない
# パターン3:-b + --single-branch
$ time git clone -b main --single-branch https://github.com/nodejs/node.git
real 1m8.456s # 約3倍速い!
サイズ比較:
# 通常
du -sh node/
1.5G node/
# --single-branch
du -sh node-single/
580M node-single/ # 約60%削減!
clone後の状態確認
$ git clone -b develop --single-branch https://github.com/user/repo.git
$ cd repo
# ローカルブランチ
$ git branch
* develop
# リモート追跡ブランチ
$ git branch -a
* develop
remotes/origin/develop
# ↑ developのみ!他のブランチ情報なし
# 他のブランチに切り替えようとすると…
$ git checkout main
error: pathspec 'main' did not match any file(s) known to git
重要: 他のブランチには切り替えられません(後述の方法で追加可能)。
–single-branch の使い分け
✅ 使うべき場面:
- 一時的なレビュー・確認作業
- CI/CDでの自動ビルド
- 容量・速度を優先したい
- 特定ブランチだけで完結する作業
❌ 避けるべき場面:
- 複数ブランチを切り替える予定
- mainとの比較が必要
- チーム開発で全体像を把握したい
さらに高速化:–depth で履歴を制限
Shallow Clone(浅いクローン)とは
git clone -b <ブランチ名> --depth 1 <リポジトリURL>
通常のcloneとの違い:
【通常のclone】
取得する履歴:
初回コミット → ... → 最新(全履歴)
├─ commit 1 (2020年)
├─ commit 2
├─ commit 3
├─ ...
└─ commit 5000 (2025年) ← 最新
【--depth 1】
取得する履歴:
└─ commit 5000 (2025年) ← 最新のみ
実例:劇的な高速化
テストリポジトリ:Docker CLI(約180MB)
# パターン1:全履歴
$ time git clone -b 24.0 https://github.com/docker/cli.git
Receiving objects: 100% (423695/423695), 181.86 MiB
real 0m23.687s
# パターン2:--single-branch
$ time git clone -b 24.0 --single-branch https://github.com/docker/cli.git
Receiving objects: 100% (82325/82325), 45.84 MiB
real 0m6.311s
# パターン3:--depth 1
$ time git clone -b 24.0 --depth 1 https://github.com/docker/cli.git
Receiving objects: 100% (3845/3845), 6.29 MiB
real 0m2.273s # 10倍以上速い!
サイズ比較:
全履歴: 181.86 MiB
--single-branch: 45.84 MiB (75%削減)
--depth 1: 6.29 MiB (96%削減!)
最強の組み合わせ
# 最速・最小
git clone -b main --depth 1 --single-branch https://github.com/user/repo.git
使用例:
# CI/CDでの高速デプロイ
git clone -b production \
--depth 1 \
--single-branch \
https://github.com/company/app.git
# レビュー用の一時確認
git clone -b feature/new-feature \
--depth 1 \
--single-branch \
https://github.com/team/project.git
–depth のオプション
# 最新1コミットのみ
git clone --depth 1 <URL>
# 最新10コミット
git clone --depth 10 <URL>
# 最新100コミット
git clone --depth 100 <URL>
どの値を選ぶべきか:
| depth | 用途 |
|---|---|
| 1 | CI/CD、一時確認、ビルドのみ |
| 10 | 直近の変更を確認したい |
| 50 | 少し古い履歴も見たい |
| 100+ | 本格的な開発には全履歴を |
shallow cloneの制限事項
⚠️ できないこと:
# 1. 古いコミットへのcheckout
$ git checkout abc123 # 古いコミット
fatal: reference is not a tree: abc123
# 2. git blameで全履歴確認
$ git blame file.js
# → 最新のコミットしか見えない
# 3. 完全なgit log
$ git log
# → --depthで指定した分のみ
# 4. 一部のrebase操作
$ git rebase origin/main
fatal: unrelated histories
解決方法:後から履歴を取得
# 全履歴を取得
git fetch --unshallow
# または特定の深さまで取得
git fetch --depth=100
タグを指定してclone
タグ指定の基本
git clone -b <タグ名> --single-branch <リポジトリURL>
実例:
# バージョン2.0.0のソースを取得
git clone -b v2.0.0 --single-branch https://github.com/user/project.git
# Node.js の v18.0.0
git clone -b v18.0.0 --single-branch https://github.com/nodejs/node.git
# React の v18.2.0
git clone -b v18.2.0 --single-branch https://github.com/facebook/react.git
タグ + –depth 1 の組み合わせ
# 特定バージョンを最速取得
git clone -b v2.0.0 --depth 1 --single-branch https://github.com/user/project.git
使用例:
# 本番環境で動いているバージョンを確認
git clone -b v1.5.3 --depth 1 --single-branch \
https://github.com/company/production-app.git
# 過去のバグを再現するため旧バージョンを取得
git clone -b v1.2.0 --depth 1 --single-branch \
https://github.com/team/app.git
タグ一覧の確認方法
cloneせずにタグを確認:
git ls-remote --tags https://github.com/user/repository.git
出力例:
abc123... refs/tags/v1.0.0
def456... refs/tags/v1.1.0
789ghi... refs/tags/v2.0.0
ブラウザで確認:
https://github.com/user/repository/tags
実践的な使用例

ケース1:チーム開発での並行作業
# Aさん:認証機能の実装
git clone -b feature/authentication \
https://github.com/team/app.git ~/work/app-auth
# Bさん:決済機能の実装
git clone -b feature/payment \
https://github.com/team/app.git ~/work/app-payment
# Cさん:UI改修
git clone -b feature/ui-redesign \
https://github.com/team/app.git ~/work/app-ui
ポイント:
- 各メンバーが独立したディレクトリで作業
- 不要なブランチを取得しない
- ディスク容量を節約
ケース2:CI/CDパイプライン
GitHub Actions例:
name: Deploy to Production
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout production branch
uses: actions/checkout@v3
with:
ref: production
fetch-depth: 1 # --depth 1と同じ
- name: Build and Deploy
run: |
npm install
npm run build
./deploy.sh
GitLab CI例:
deploy_production:
stage: deploy
script:
- git clone -b production --depth 1 --single-branch $CI_REPOSITORY_URL
- cd $(basename $CI_REPOSITORY_URL .git)
- npm install
- npm run build
only:
- main
ケース3:複数環境への同時デプロイ
#!/bin/bash
# deploy-all.sh
# 開発環境
git clone -b develop --depth 1 --single-branch \
https://github.com/company/app.git ~/deploy/dev
cd ~/deploy/dev && npm install && npm run build:dev
# ステージング環境
git clone -b staging --depth 1 --single-branch \
https://github.com/company/app.git ~/deploy/staging
cd ~/deploy/staging && npm install && npm run build:staging
# 本番環境
git clone -b production --depth 1 --single-branch \
https://github.com/company/app.git ~/deploy/prod
cd ~/deploy/prod && npm install && npm run build:prod
ケース4:レビュー専用の軽量clone
# プルリクエストのレビュー用
git clone -b feature/pr-123 \
--depth 1 \
--single-branch \
https://github.com/team/project.git review-pr-123
cd review-pr-123
code . # VS Codeで開く
# レビュー完了後
cd ..
rm -rf review-pr-123 # 削除
ケース5:Dockerビルドでの活用
Dockerfile例:
FROM node:18
# 特定ブランチを高速取得
RUN git clone -b production \
--depth 1 \
--single-branch \
https://github.com/company/app.git /app
WORKDIR /app
RUN npm install && npm run build
CMD ["npm", "start"]
ビルド時間の比較:
通常のclone: docker build → 5分30秒
--depth 1使用: docker build → 1分45秒(3倍速!)
clone後に他のブランチを追加する方法
問題:–single-branchで他のブランチが見えない
$ git clone -b develop --single-branch https://github.com/user/repo.git
$ cd repo
$ git branch -a
* develop
remotes/origin/develop
# ↑ developしかない
$ git checkout main
error: pathspec 'main' did not match any file(s)
解決方法1:設定変更で全ブランチ取得
# リモート設定を変更
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
# 全ブランチ情報を取得
git fetch origin
# 確認
git branch -a
* develop
remotes/origin/develop
remotes/origin/main
remotes/origin/feature/new-ui
# 他のブランチに切り替え可能に
git checkout main
Switched to branch 'main'
詳細説明:
# 変更前の設定
$ git config --get remote.origin.fetch
+refs/heads/develop:refs/remotes/origin/develop
# ↑ developのみ追跡
# 変更後の設定
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
# ↑ すべてのブランチを追跡
解決方法2:特定ブランチだけ追加
# mainブランチの情報を取得
git fetch origin main:main
# または
git fetch origin main
git checkout -b main origin/main
# 確認
git branch
develop
* main
複数ブランチを追加:
# 複数のブランチを一度に取得
git fetch origin main:main develop:develop staging:staging
# または個別に
git fetch origin main
git checkout -b main origin/main
git fetch origin staging
git checkout -b staging origin/staging
解決方法3:一時的に別ブランチを確認
# mainブランチを一時的にfetch
git fetch origin main
# 直接確認(checkoutせず)
git log origin/main
git diff develop..origin/main
git show origin/main:README.md
フォルダ名を指定してclone
デフォルトのフォルダ名
# リポジトリ名がフォルダ名になる
git clone https://github.com/user/my-project.git
# 結果
ls
my-project/ # ← リポジトリ名
カスタムフォルダ名を指定
git clone -b develop <URL> <フォルダ名>
実例:
# プロジェクト名と違うフォルダ名
git clone -b develop \
https://github.com/user/my-project.git \
work-dir
# 結果
ls
work-dir/ # ← 指定したフォルダ名
# 複数環境を並行管理
git clone -b develop https://github.com/user/app.git app-dev
git clone -b staging https://github.com/user/app.git app-staging
git clone -b production https://github.com/user/app.git app-prod
ls
app-dev/
app-staging/
app-prod/
プライベートリポジトリでのブランチ指定
HTTPSでの認証
# パターン1:認証情報を入力
git clone -b develop https://github.com/company/private-repo.git
Username: your-username
Password: your-token # パスワードではなくトークン
# パターン2:URLに含める(非推奨)
git clone -b develop https://username:token@github.com/company/private-repo.git
SSHでの接続
# SSH接続(推奨)
git clone -b develop git@github.com:company/private-repo.git
SSH鍵の設定(初回のみ):
# 1. SSH鍵を生成
ssh-keygen -t ed25519 -C "your_email@example.com"
# 2. 公開鍵をコピー
cat ~/.ssh/id_ed25519.pub
# 3. GitHubに登録
# Settings → SSH and GPG keys → New SSH key
# コピーした公開鍵を貼り付け
# 4. 接続テスト
ssh -T git@github.com
Personal Access Token(PAT)の使用
GitHub の場合:
# 1. トークンを生成
# Settings → Developer settings → Personal access tokens → Generate new token
# 必要な権限を選択(repo など)
# 2. 使用
git clone -b develop https://github.com/company/private-repo.git
Username: your-username
Password: ghp_xxxxxxxxxxxxxxxxxxxx # トークンを入力
認証情報をキャッシュ:
# 認証情報を保存(15分間)
git config --global credential.helper cache
# 永続的に保存(セキュリティリスクあり)
git config --global credential.helper store
よくあるトラブルと解決方法
エラー1:「fatal: Remote branch XXX not found」
症状:
$ git clone -b feature/new-ui https://github.com/user/repo.git
fatal: Remote branch feature/new-ui not found in upstream origin
原因: 指定したブランチが存在しない
解決方法:
# ステップ1:ブランチ一覧を確認
git ls-remote --heads https://github.com/user/repo.git
# 出力例
abc123... refs/heads/main
def456... refs/heads/develop
789ghi... refs/heads/feature/login
# ↑ feature/new-ui は存在しない
# ステップ2:正しいブランチ名でclone
git clone -b feature/login https://github.com/user/repo.git
よくある間違い:
# ❌ 間違い
git clone -b Feature/Login # 大文字小文字が違う
git clone -b feature-login # ハイフンとスラッシュを間違えた
git clone -b features/login # sがついている
# ✅ 正解
git clone -b feature/login
エラー2:「warning: remote HEAD refers to nonexistent ref」
症状:
$ git clone -b old-branch https://github.com/user/repo.git
warning: remote HEAD refers to nonexistent ref, unable to checkout.
原因: デフォルトブランチが変更・削除されている
解決方法:
# ステップ1:デフォルトブランチを確認
git ls-remote --symref https://github.com/user/repo.git HEAD
# 出力例
ref: refs/heads/main HEAD
# ↑ デフォルトブランチはmain
# ステップ2:存在するブランチを指定
git clone -b main https://github.com/user/repo.git
エラー3:「fatal: could not read Username」
症状:
$ git clone -b develop https://github.com/company/private-repo.git
fatal: could not read Username for 'https://github.com': terminal prompts disabled
原因: プライベートリポジトリで認証情報がない
解決方法:
パターンA:SSHを使う(推奨)
git clone -b develop git@github.com:company/private-repo.git
パターンB:認証情報を含める
git clone -b develop https://username:token@github.com/company/private-repo.git
パターンC:credential helperを設定
git config --global credential.helper store
git clone -b develop https://github.com/company/private-repo.git
# ↑ 認証情報を入力(次回から不要)
エラー4:「Detached HEAD state」
症状:
$ git clone -b v2.0.0 --depth 1 https://github.com/user/repo.git
Note: switching to 'abc123...'.
You are in 'detached HEAD' state.
原因: タグを指定したため、特定のコミットにいる
対処:
そのまま作業する場合:
# ブランチを作成
git switch -c my-work
# または
git checkout -b my-work
最新の状態に移動する場合:
# mainブランチに移動
git fetch origin main
git checkout -b main origin/main
detached HEADが問題ない場合:
- 読み取り専用の確認
- ビルド・デプロイのみ
- 一時的な作業
エラー5:「RPC failed; curl transfer closed」
症状:
$ git clone -b main https://github.com/user/large-repo.git
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: the remote end hung up unexpectedly
原因: リポジトリが大きすぎる、ネットワークが不安定
解決方法:
方法1:バッファサイズを増やす
git config --global http.postBuffer 524288000 # 500MB
# 再試行
git clone -b main https://github.com/user/large-repo.git
方法2:shallow cloneを使う
# まず最新だけ取得
git clone -b main --depth 1 https://github.com/user/large-repo.git
# 必要なら徐々に履歴を増やす
cd large-repo
git fetch --depth=10
git fetch --depth=100
方法3:分割して取得
# SSHで試す(HTTPより安定)
git clone -b main git@github.com:user/large-repo.git
# または時間をかけて段階的に
git clone -b main --depth 1 https://github.com/user/large-repo.git
cd large-repo
git fetch --depth=50
git fetch --depth=100
# ...徐々に深くしていく
パフォーマンス比較表

実測値:React リポジトリ
| オプション | 時間 | サイズ | 備考 |
|---|---|---|---|
| 通常 | 45秒 | 280MB | すべて |
-b main | 43秒 | 280MB | 効果なし |
--single-branch | 18秒 | 85MB | 2.5倍速 |
--depth 1 | 12秒 | 45MB | 3.8倍速 |
--depth 1 --single-branch | 8秒 | 12MB | 5.6倍速 |
コマンド別推奨表
| 目的 | 推奨コマンド | 理由 |
|---|---|---|
| レビュー | git clone -b [branch] --depth 1 --single-branch | 最速・最小 |
| CI/CD | git clone -b [branch] --depth 1 --single-branch | ビルド時間短縮 |
| 本格開発 | git clone -b [branch] | 全履歴が必要 |
| 一時確認 | git clone -b [branch] --depth 1 | 履歴不要 |
| チーム開発 | git clone | 全ブランチ必要 |
Git エイリアス設定
便利なエイリアス
# ~/.gitconfig に追加
# ブランチ指定clone cloneb = clone -b # 高速clone clonefast = clone –depth 1 –single-branch -b # 軽量clone clonelight = clone –depth 1 -b # ブランチ一覧確認 branches = ls-remote –heads
使用例:
# 通常
git clone -b develop https://github.com/user/repo.git
# エイリアス使用
git cloneb develop https://github.com/user/repo.git
# 高速clone
git clonefast main https://github.com/user/large-repo.git
# ブランチ確認
git branches https://github.com/user/repo.git
シェルエイリアス
~/.bashrc または ~/.zshrc に追加:
# 高速cloneの短縮コマンド
alias gclf='git clone --depth 1 --single-branch -b'
# 使用例
gclf main https://github.com/user/repo.git
よくある質問(FAQ)
Q1:-bオプションなしでcloneした場合、どのブランチが取得されますか?
A:デフォルトブランチ(通常はmainまたはmaster)が取得されます。
# デフォルトブランチを確認
git ls-remote --symref https://github.com/user/repo.git HEAD
出力例:
ref: refs/heads/main HEAD
Q2:–single-branchでcloneした後、やっぱり他のブランチも欲しくなりました。
A:設定変更で追加できます。
# 全ブランチ取得可能に変更
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
# または特定ブランチだけ
git fetch origin develop:develop
Q3:–depth 1でcloneした後、古い履歴も見たくなりました。
A:後から履歴を取得できます。
# 全履歴を取得
git fetch --unshallow
# または段階的に
git fetch --depth=100
git fetch --depth=1000
Q4:タグとブランチ、両方に同じ名前があったらどうなりますか?
A:ブランチが優先されます。
# ブランチv1.0.0とタグv1.0.0が両方ある場合
git clone -b v1.0.0 https://github.com/user/repo.git
# → ブランチv1.0.0がcloneされる
# タグを明示的に指定
git clone -b refs/tags/v1.0.0 https://github.com/user/repo.git
Q5:clone中にキャンセルしました。途中から再開できますか?
A:いいえ、最初からやり直しです。
# 中途半端なフォルダを削除
rm -rf partially-cloned-repo
# 最初からclone
git clone -b main https://github.com/user/repo.git
大きなリポジトリの場合:
# 段階的にcloneすると安全
git clone -b main --depth 1 https://github.com/user/large-repo.git
cd large-repo
git fetch --depth=10
git fetch --depth=100
# ...徐々に深くする
Q6:複数のブランチを同時に指定してcloneできますか?
A:できません。複数回cloneするか、clone後にfetchしてください。
# パターン1:別々のディレクトリにclone
git clone -b develop https://github.com/user/repo.git repo-develop
git clone -b staging https://github.com/user/repo.git repo-staging
# パターン2:clone後に追加
git clone -b develop https://github.com/user/repo.git
cd repo
git fetch origin staging:staging
git fetch origin main:main
Q7:–single-branchとPRブランチの相性は?
A:問題なく使えます。
# プルリクエスト用ブランチをclone
git clone -b feature/pr-123 --single-branch https://github.com/user/repo.git
GitHubのプルリクエストも通常のブランチなので、同じように指定できます。
Q8:大文字小文字を間違えた場合は?
A:エラーになります。
# ❌ 間違い
$ git clone -b Feature/Login https://github.com/user/repo.git
fatal: Remote branch Feature/Login not found
# ✅ 正解
$ git clone -b feature/login https://github.com/user/repo.git
Gitのブランチ名は大文字小文字を区別します。
Q9:cloneに失敗した場合、リトライは自動的にされますか?
A:いいえ、自動リトライはありません。
手動でリトライするか、以下のようなスクリプトを使います:
#!/bin/bash
# clone-retry.sh
MAX_RETRIES=3
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
echo "Attempt $((RETRY_COUNT + 1))..."
if git clone -b main --depth 1 https://github.com/user/repo.git; then
echo "Clone successful!"
exit 0
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
sleep 5
done
echo "Clone failed after $MAX_RETRIES attempts"
exit 1
Q10:社内プロキシ経由でcloneする場合は?
A:プロキシ設定が必要です。
# HTTPプロキシ設定
git config --global http.proxy http://proxy.company.com:8080
git config --global https.proxy https://proxy.company.com:8080
# 認証が必要な場合
git config --global http.proxy http://username:password@proxy.company.com:8080
# 特定のリポジトリのみ
git config --local http.proxy http://proxy.company.com:8080
# プロキシ解除
git config --global --unset http.proxy
git config --global --unset https.proxy
まとめ:最適なcloneコマンドの選び方
シチュエーション別推奨コマンド
1. 通常の開発(複数ブランチを使う)
git clone -b develop https://github.com/user/project.git
- ✅ 他のブランチにも切り替え可能
- ✅ 完全な履歴
- ⚠️ 時間・容量はかかる
2. レビュー・一時確認
git clone -b feature/new-feature --depth 1 --single-branch https://github.com/user/project.git
- ✅ 最速・最小
- ✅ 必要最小限
- ❌ 他のブランチ不可
3. CI/CD・自動デプロイ
git clone -b production --depth 1 --single-branch --no-tags https://github.com/user/project.git
- ✅ ビルド時間最小化
- ✅ ディスク使用量削減
- ✅ タグ不要
4. 特定バージョンの調査
git clone -b v2.0.0 --depth 1 --single-branch https://github.com/user/project.git
- ✅ 過去のバージョン
- ✅ 読み取り専用確認
- ❌ 開発には不向き
5. 大規模リポジトリ
# ステップ1:最新だけ取得
git clone -b main --depth 1 https://github.com/user/large-project.git
# ステップ2:必要に応じて履歴を追加
cd large-project
git fetch --depth=100
- ✅ 段階的取得
- ✅ ネットワークエラー回避
- ✅ 柔軟性
速度・容量の目安
| コマンド | 速度 | 容量 | 柔軟性 |
|---|---|---|---|
git clone | ★☆☆ | 100% | ★★★ |
git clone -b | ★☆☆ | 100% | ★★★ |
--single-branch | ★★☆ | 30-50% | ★★☆ |
--depth 1 | ★★★ | 5-20% | ★☆☆ |
両方併用 | ★★★ | 3-10% | ★☆☆ |
最後に:迷ったらこれ!
汎用的な推奨コマンド:
# 開発用(デフォルト)
git clone -b develop https://github.com/user/project.git
# 高速・軽量(CI/レビュー)
git clone -b main --depth 1 --single-branch https://github.com/user/project.git
この2パターンを覚えておけば、ほとんどの状況に対応できます!

コメント