git clone ブランチ指定の完全ガイド【-b・–depth・shallow clone 2026年版】
git clone のチュートリアルを探すと、こんな手順がよく書かれている。
git clone [リポジトリURL]
git switch [ブランチ名] # Git 2.23+ 推奨(旧来は git checkout [ブランチ名])
この方法でも目的は達成できるが、実際の開発現場では非効率な場面が多い。特定のブランチだけが必要なのに、全ブランチの履歴をまるごとダウンロードしてしまうからだ。
本記事では、-b オプションによるブランチ指定、--depth による shallow clone、そしてその組み合わせまで、実際に動作を検証した内容をもとに解説する。よくあるエラーの対処法も合わせて紹介する。

前提条件
|
項目
| 内容
|
| 必要なコマンド
| git(2.x 以降推奨)
|
| 対象OS
| Linux / macOS / Windows(Git Bash)
|
| 前提知識
| コマンドライン基本操作、Git の基礎(clone・branch の概念)
|
# バージョン確認
git --version
# git version 2.43.0 以降であれば全機能が使用できる
ブランチ指定なしの clone が遅い理由
まず、なぜブランチ指定が必要なのかを理解しておきたい。
git clone をオプションなしで実行すると、リモートリポジトリの 全ブランチ・全コミット履歴 をローカルに取得する。.git/ ディレクトリの中に全ブランチのオブジェクトが格納されるため、ブランチ数が多いリポジトリや、長期間運用してきたリポジトリでは数百MB〜数GBになることもある。
# ブランチ指定なし(デフォルト)
git clone https://github.com/example/repo.git
# .git/ 以下に全ブランチの全履歴が入る
du -sh repo/.git/
# → 例: 1.2G
ブランチを指定して clone することで、不要なデータのダウンロードを省略でき、clone 時間とディスク使用量を大幅に削減できる。
ステップ1: -b オプションでブランチを指定して clone する
基本構文は以下の通りだ。
git clone -b [ブランチ名] [リポジトリURL]
具体的な使用例
# develop ブランチを指定して clone
git clone -b develop https://github.com/example/repo.git
# feature/login ブランチを指定して clone(スラッシュを含むブランチ名も可)
git clone -b feature/login https://github.com/example/repo.git
# クローン先のディレクトリ名も変更する場合
git clone -b develop https://github.com/example/repo.git my-project
clone 後の状態を確認する
cd repo
git branch
# * develop
git branch -a
# * develop
# remotes/origin/HEAD -> origin/main
# remotes/origin/develop
# remotes/origin/main
# remotes/origin/feature/login
-b を指定した場合でも、デフォルトでは リモートの全ブランチ情報はリストに含まれる。ただし、実際にチェックアウトされているのは指定したブランチのみだ。
注意点:
-b で指定するのはブランチ名だが、タグ名でも指定できる。git clone -b v2.1.0 https://... のようにタグを指定して特定リリースを取得することも可能。

✅ ステップ1完了の確認: git branch で指定したブランチがアクティブになっていればOK。
ステップ2: リモートブランチの確認方法
clone 前に存在するブランチを確認したい場合は、以下のコマンドを使う。
clone 前にブランチ一覧を確認する
# clone せずにリモートのブランチ一覧を確認
git ls-remote --heads https://github.com/example/repo.git
# 出力例
# abc1234... refs/heads/develop
# def5678... refs/heads/feature/login
# ghi9012... refs/heads/main
git ls-remote は認証が通るリポジトリであれば、clone なしでリモートのブランチ・タグ一覧を取得できる。
clone 後にブランチを切り替える
# clone 後に別のブランチに切り替える(Git 2.23+ 推奨)
git switch main
# 旧来のコマンド(Git 2.23 未満の環境でも動作)
git checkout main
# リモートブランチをローカルブランチとして作成・切り替え(Git 2.23+ 推奨)
git switch -c feature/login origin/feature/login
# 旧来のコマンド
git checkout -b feature/login origin/feature/login
✅ ステップ2完了の確認: git branch -a でリモートブランチの一覧が確認できる。
ステップ3: –depth オプションで shallow clone を行う
shallow clone は、指定した深さ(コミット数)だけ履歴を取得する clone だ。
# 最新のコミット1件だけを取得(最も一般的な使い方)
git clone --depth 1 [リポジトリURL]
–depth の仕組み
--depth 1 を指定すると、各ブランチの最新コミット1件のみをダウンロードする。コミット履歴が10年分あるリポジトリでも、--depth 1 なら最新スナップショットだけを取得するため、劇的に高速化できる。
# 通常の clone
git clone https://github.com/example/large-repo.git
# → 所要時間: 3分 / サイズ: 800MB
# shallow clone
git clone --depth 1 https://github.com/example/large-repo.git
# → 所要時間: 15秒 / サイズ: 45MB
–depth の数値を変える
# 最新から3コミット分の履歴を取得
git clone --depth 3 https://github.com/example/repo.git
# 最新から10コミット分の履歴を取得
git clone --depth 10 https://github.com/example/repo.git
shallow clone の制限事項
shallow clone には以下の制限がある。
|
操作
| 通常 clone
| shallow clone(–depth 1)
|
| git log(全履歴)
| 可能
| 不可(取得した分のみ)
|
| git blame
| 可能
| 部分的に可能
|
| git bisect
| 可能
| 不可
|
| git push
| 可能
| 可能
|
| git pull
| 可能
| 可能(最新分のみ)
|
CI/CD やデプロイ目的で使うなら --depth 1 で十分だ。ただし、コードレビューやデバッグで過去の履歴を参照する必要がある場合は shallow clone を避けるべきだ。

✅ ステップ3完了の確認: git log --oneline でコミット数が depth で指定した件数に制限されていることを確認。
ステップ4: ブランチ指定と –depth を組み合わせる
-b と --depth は同時に使用できる。これが最も実用的な組み合わせだ。
git clone --depth 1 -b [ブランチ名] [リポジトリURL]
具体的な使用例
# develop ブランチの最新コミットだけを取得
git clone --depth 1 -b develop https://github.com/example/repo.git
# main ブランチの最新3コミットを取得
git clone --depth 3 -b main https://github.com/example/repo.git
# タグを指定して shallow clone(特定バージョンのデプロイに有用)
git clone --depth 1 -b v2.1.0 https://github.com/example/repo.git
CI/CD での活用例
GitHub Actions での使用例:
# .github/workflows/deploy.yml
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 1 # --depth 1 と同等
GitLab CI での使用例:
# .gitlab-ci.yml
variables:
GIT_DEPTH: "1" # --depth 1 と同等
GIT_STRATEGY: clone
–single-branch オプションで更に最適化する
--single-branch を追加すると、指定したブランチのオブジェクトのみを取得し、他ブランチへの参照も作成しない。
git clone --depth 1 --single-branch -b develop https://github.com/example/repo.git
# --single-branch 適用後のブランチ一覧
git branch -a
# * develop
# remotes/origin/develop
# ← 他のリモートブランチが表示されない
--depth 1 --single-branch の組み合わせが、clone サイズを最小化する最も効果的な方法だ。

✅ ステップ4完了の確認: git branch -a で指定ブランチのみが存在すること、git log --oneline でコミット数が制限されていることを確認。
応用編
shallow clone を通常 clone に変換する
shallow clone 後に履歴が必要になった場合、--unshallow で完全な履歴を取得できる。
# shallow clone を解除して全履歴を取得
git fetch --unshallow
# 特定ブランチの履歴を追加で取得する
git fetch --depth 10 origin main
認証が必要なプライベートリポジトリの場合
# HTTPS + 認証情報
git clone -b develop https://username:token@github.com/example/private-repo.git
# SSH キーを使用する場合(推奨)
git clone -b develop git@github.com:example/private-repo.git
SSH キーの設定が完了していれば、-b や --depth の動作は同じだ。
bare clone でブランチを指定する
サーバーやミラー用途で bare clone する場合も -b を使える。
# bare clone(作業ディレクトリなし)でブランチ指定
git clone --bare -b main https://github.com/example/repo.git
よくあるエラーと対処法
エラー1: fatal: Remote branch [ブランチ名] not found
fatal: Remote branch develop not found in upstream origin
原因: 指定したブランチ名が存在しない、またはスペルミス。
対処法:
# まずリモートのブランチ一覧を確認する
git ls-remote --heads [リポジトリURL]
# または clone 後に確認する
git branch -a
ブランチ名の大文字・小文字、スラッシュの有無を確認すること(Feature/Login と feature/login は別ブランチ)。
エラー2: warning: --depth is ignored in local clones
warning: --depth is ignored in local clones; use file:// instead.
原因: ローカルパスに対して --depth を指定している。
対処法:
# NG: ローカルパスでは --depth が無視される
git clone --depth 1 /path/to/local/repo
# OK: file:// プロトコルを使う
git clone --depth 1 file:///path/to/local/repo
エラー3: shallow clone 後の git push が失敗する
error: shallow update not allowed
原因: shallow clone したリポジトリへのプッシュが特定条件下で拒否される。
対処法:
# --unshallow で完全な履歴を取得してからプッシュする
git fetch --unshallow
git push origin [ブランチ名]
エラー4: --depth と --no-single-branch の関係
デフォルトでは --depth を指定すると --single-branch が暗黙的に有効になる。他ブランチも取得したい場合は明示的に --no-single-branch を指定する。
# --depth 指定時に他ブランチも取得する
git clone --depth 1 --no-single-branch https://github.com/example/repo.git
# 確認
git branch -a
# * main
# remotes/origin/develop
# remotes/origin/feature/login
# remotes/origin/main
まとめ
本記事では git clone でブランチを指定する方法と、shallow clone の活用方法を解説した。
|
目的
| コマンド
|
| 特定ブランチを clone
| git clone -b [branch] [URL]
|
| 最新コミットだけ取得
| git clone --depth 1 [URL]
|
| ブランチ指定 + 軽量 clone
| git clone --depth 1 -b [branch] [URL]
|
| 最小サイズで clone
| git clone --depth 1 --single-branch -b [branch] [URL]
|
| 全履歴を後から取得
| git fetch --unshallow
|
CI/CD やデプロイ用途では --depth 1 --single-branch -b [ブランチ名] の組み合わせが最も効率的だ。開発用途では -b でブランチを指定するだけで十分なケースが多い。
用途に応じてオプションを使い分け、不要な履歴のダウンロードを省くことで、開発・デプロイのスピードを向上させよう。





