「mainブランチじゃなくて、developブランチだけ欲しいんだけど…」 「機能ブランチで作業したいのに、全部cloneするの時間かかる…」
そんな悩み、ありませんか?実は、git cloneには特定のブランチだけを取得する便利なオプションがあるんです。
この記事では、ブランチを指定してcloneする方法から、容量を節約する賢い使い方まで、実例付きで分かりやすく解説していきます。
基本:-bオプションでブランチを指定する

一番シンプルな方法
特定のブランチをcloneする基本コマンドはこれです:
git clone -b ブランチ名 リポジトリURL
実例:developブランチをclone
git clone -b develop https://github.com/example/project.git
これで、developブランチがチェックアウトされた状態でcloneできます。
具体的に何が起きているの?
通常のgit cloneとの違いを見てみましょう。
通常のclone:
- すべてのブランチをダウンロード
- デフォルトブランチ(通常はmain)をチェックアウト
-bオプション付きclone:
- すべてのブランチをダウンロード
- 指定したブランチをチェックアウト
つまり、最初から作業したいブランチで始められるんです。
なぜブランチ指定が必要なの?よくある場面
場面1:開発ブランチで作業を始めたい
チーム開発では、mainブランチではなくdevelopブランチで作業することが多いです。
# developブランチで開発を始める
git clone -b develop https://github.com/会社/プロジェクト.git
# すぐに作業開始できる!
cd プロジェクト
# もうdevelopブランチにいる
場面2:特定の機能ブランチをレビューしたい
同僚から「feature/login-systemブランチ見てくれる?」と言われた時:
git clone -b feature/login-system https://github.com/会社/プロジェクト.git
わざわざclone後にcheckoutする手間が省けます。
場面3:特定のバージョンを取得したい
リリースタグを指定することもできます:
# v2.0.0のタグを取得
git clone -b v2.0.0 https://github.com/example/project.git
さらに効率的に:容量を節約する方法
–single-branchオプション:指定ブランチだけダウンロード
実は-b
だけだと、全ブランチの履歴をダウンロードしています。容量を節約したい場合は:
git clone -b develop --single-branch https://github.com/example/project.git
違いを比較:
-b
のみ:全ブランチの履歴あり(後で別ブランチに切り替え可能)--single-branch
追加:指定ブランチのみ(容量削減)
–depthオプション:履歴を制限する(Shallow Clone)
最新の履歴だけで十分な場合:
# 直近1コミットだけ取得
git clone -b develop --depth 1 https://github.com/example/project.git
メリット:
- ダウンロード時間が大幅短縮
- ディスク容量を節約
- 大規模プロジェクトで特に効果的
デメリット:
- 過去の履歴が見れない
- git blameなどが制限される
実践的な使用パターン集

パターン1:フォルダ名も指定したい
# プロジェクト名と違うフォルダ名にする
git clone -b develop https://github.com/example/project.git my-work
これで「my-work」フォルダにcloneされます。
パターン2:プライベートリポジトリの特定ブランチ
# SSHを使用
git clone -b staging git@github.com:会社/private-repo.git
# HTTPSでユーザー名を含める
git clone -b staging https://username@github.com/会社/private-repo.git
パターン3:CI/CDでの活用
自動デプロイやテストで特定ブランチだけ必要な場合:
# 最小限のデータで高速clone
git clone -b production --depth 1 --single-branch https://github.com/example/app.git
これでビルド時間を大幅に短縮できます。
パターン4:複数人で異なるブランチを作業
チームメンバーがそれぞれ違うブランチで作業する場合:
# Aさん:認証機能
git clone -b feature/auth https://github.com/team/project.git project-auth
# Bさん:決済機能
git clone -b feature/payment https://github.com/team/project.git project-payment
# Cさん:UI改修
git clone -b feature/ui-update https://github.com/team/project.git project-ui
後から他のブランチも取得したくなったら
single-branchでcloneした後に、他のブランチが必要になった
# リモートの設定を変更
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
# 全ブランチ情報を取得
git fetch origin
# 別のブランチに切り替え可能に
git checkout -b other-branch origin/other-branch
特定のブランチだけ追加で取得
# mainブランチの情報だけ追加取得
git fetch origin main:main
# 切り替え
git checkout main
よくあるトラブルと解決方法
エラー1:「Remote branch ○○ not found」
原因: 指定したブランチ名が存在しない
解決方法:
# まずブランチ一覧を確認
git ls-remote --heads https://github.com/example/project.git
# 正しいブランチ名でclone
git clone -b 正しいブランチ名 https://github.com/example/project.git
エラー2:「Permission denied」
原因: プライベートリポジトリへのアクセス権限がない
解決方法:
# SSHキーを設定済みの場合
git clone -b develop git@github.com:組織/private-repo.git
# Personal Access Tokenを使用
git clone -b develop https://TOKEN@github.com/組織/private-repo.git
エラー3:タグをブランチと間違える
注意点: タグをcloneした場合、「detached HEAD」状態になります
# タグをclone(detached HEAD状態)
git clone -b v1.0.0 https://github.com/example/project.git
# 作業用ブランチを作成
cd project
git checkout -b work-on-v1
ブランチ名の確認方法
リモートのブランチ一覧を見る
cloneする前に、どんなブランチがあるか確認したい時:
# HTTPSで確認
git ls-remote --heads https://github.com/example/project.git
# 見やすく整形
git ls-remote --heads https://github.com/example/project.git | cut -d/ -f3
デフォルトブランチを確認
git ls-remote --symref https://github.com/example/project.git HEAD
これで、mainなのかmasterなのか、他の名前なのかが分かります。
使い分けガイド:どのオプションを組み合わせる?

ケース1:レビューや確認だけ
# 最小限のデータで素早く
git clone -b feature/review --depth 1 --single-branch URL
ケース2:本格的な開発作業
# 全履歴・全ブランチアクセス可能
git clone -b develop URL
ケース3:CI/CDや自動化
# 最速・最小容量
git clone -b main --depth 1 --single-branch --no-tags URL
ケース4:特定時点のコードを調査
# タグ指定で過去のバージョン
git clone -b v2.0.0 --single-branch URL
プロのTips:知っておくと便利な技
1. エイリアスで簡略化
# ~/.gitconfigに追加
git config --global alias.cloneb 'clone -b'
# 使用例
git cloneb develop https://github.com/example/project.git
2. 環境変数でブランチ指定
# スクリプトで動的に指定
BRANCH=${1:-main}
git clone -b $BRANCH https://github.com/example/project.git
3. .gitignoreも含めて最小限に
# 不要なファイルを除外してclone
git clone -b main --filter=blob:none --sparse URL
cd project
git sparse-checkout init --cone
git sparse-checkout set src tests
まとめ:目的に応じて賢くcloneしよう
git cloneのブランチ指定は、効率的な開発に欠かせない機能です。
覚えておくべき基本コマンド:
# 基本:特定ブランチをclone
git clone -b ブランチ名 URL
# 容量節約:指定ブランチのみ
git clone -b ブランチ名 --single-branch URL
# 高速化:最新履歴のみ
git clone -b ブランチ名 --depth 1 URL
# 最速・最小:組み合わせ
git clone -b ブランチ名 --depth 1 --single-branch URL
使い分けのポイント:
- 開発作業 → 基本の
-b
のみ - 確認・レビュー →
--single-branch
追加 - CI/CD →
--depth 1
も追加 - 容量重視 → すべて組み合わせ
最初は基本の-b
オプションから始めて、必要に応じて他のオプションを組み合わせていけば大丈夫です。
これで、無駄な時間を使わず、スマートにGitを使えるようになりますよ!
コメント