「GitHubで見つけた便利なプロジェクトを使いたい」 「チーム開発に参加することになった」 「リモートリポジトリを自分のPCで動かしたい」 「cloneって何?downloadとは違うの?」
こんな時に使うのがgit cloneコマンドです。
git cloneは、リモートリポジトリ(GitHub、GitLabなど)を丸ごと自分のPCにコピーする、Git操作の第一歩となる重要なコマンド。単なるダウンロードではなく、履歴も含めて完全なリポジトリを取得できるんです。
今回は、git cloneの基本から応用まで、実践的な使い方を完全解説していきます!
git cloneとは? – ダウンロードとの違い

基本的な説明
git cloneは、リモートリポジトリを複製(クローン)してローカルリポジトリを作成するコマンドです。
ダウンロード(ZIP)との違い
通常のダウンロード:
GitHubの「Download ZIP」ボタン
→ その時点のファイルだけ取得
→ Git履歴なし
→ Git操作不可
git clone:
git cloneコマンド
→ 全履歴を含む完全なリポジトリ
→ すべてのブランチ情報
→ Git操作可能(push、pull等)
→ 開発に参加可能
基本的な使い方
最もシンプルなclone
# HTTPSでクローン(最も一般的)
git clone https://github.com/username/repository.git
# 実例:人気のリポジトリをクローン
git clone https://github.com/facebook/react.git
何が起きるか:
- カレントディレクトリに「repository」フォルダを作成
- リポジトリの全データをダウンロード
- 自動的に「origin」という名前でリモート設定
- デフォルトブランチ(通常main/master)をチェックアウト
フォルダ名を指定してclone
# デフォルト名じゃなくて、好きな名前にしたい
git clone https://github.com/username/repository.git my-project
# 例:reactを別名でクローン
git clone https://github.com/facebook/react.git react-study
# カレントディレクトリに直接クローン(フォルダを作らない)
git clone https://github.com/username/repository.git .
認証方法別のclone
HTTPS方式(推奨:初心者向け)
# パブリックリポジトリ(認証不要)
git clone https://github.com/torvalds/linux.git
# プライベートリポジトリ(認証必要)
git clone https://github.com/username/private-repo.git
# → ユーザー名とパスワード(またはトークン)を要求される
Personal Access Token(PAT)を使う場合:
# GitHubは2021年8月からパスワード認証を廃止
# 代わりにPersonal Access Tokenを使用
# トークンをURLに含める方法(非推奨:履歴に残る)
git clone https://YOUR_TOKEN@github.com/username/repository.git
# より安全な方法:認証情報をキャッシュ
git config --global credential.helper cache
git clone https://github.com/username/repository.git
# → 初回のみトークンを入力、以降はキャッシュを使用
SSH方式(推奨:頻繁に使う人向け)
事前準備:SSHキーの設定
# 1. SSHキーを生成(未作成の場合)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 2. 公開鍵をGitHubに登録
cat ~/.ssh/id_ed25519.pub
# → この内容をGitHubのSettings > SSH and GPG keysに追加
# 3. 接続テスト
ssh -T git@github.com
SSHでclone:
# SSH URLでクローン
git clone git@github.com:username/repository.git
# 例:
git clone git@github.com:facebook/react.git
# ポート指定が必要な場合
git clone ssh://git@github.com:22/username/repository.git
どちらを選ぶべき?
HTTPS:
✅ 設定が簡単
✅ ファイアウォールを通過しやすい
❌ 毎回認証が必要(キャッシュ可能)
SSH:
✅ 一度設定すれば認証不要
✅ より安全
❌ 初期設定が必要
❌ 企業環境では使えない場合も
便利なオプション
浅いクローン(–depth)
# 最新のコミットだけ取得(履歴を含まない)
git clone --depth 1 https://github.com/username/repository.git
# 最新の10コミットだけ取得
git clone --depth 10 https://github.com/username/repository.git
# メリット:
# - ダウンロード時間短縮(大規模リポジトリで効果的)
# - ディスク容量節約
# - 履歴が不要な場合に最適
# 例:Linuxカーネル(巨大)を素早くクローン
git clone --depth 1 https://github.com/torvalds/linux.git
特定のブランチをクローン(–branch)
# masterではなく、特定のブランチをクローン
git clone --branch develop https://github.com/username/repository.git
# 短縮形
git clone -b develop https://github.com/username/repository.git
# タグを指定することも可能
git clone --branch v2.0.0 https://github.com/username/repository.git
# 単一ブランチのみクローン(他のブランチ情報を取得しない)
git clone --single-branch --branch develop https://github.com/username/repository.git
再帰的にサブモジュールも取得(–recursive)
# サブモジュールを含むリポジトリを完全にクローン
git clone --recursive https://github.com/username/repository.git
# または後からサブモジュールを初期化
git clone https://github.com/username/repository.git
cd repository
git submodule update --init --recursive
静かにクローン(–quiet)
# 進捗表示を抑制(スクリプト用)
git clone --quiet https://github.com/username/repository.git
# 短縮形
git clone -q https://github.com/username/repository.git
実践的な使用例

大規模プロジェクトを効率的にクローン
# Chromiumのような巨大プロジェクト
# 通常のclone:数GB、数時間かかる
# 効率的な方法:
git clone --depth 1 --single-branch \
--branch main \
https://chromium.googlesource.com/chromium/src.git
# 必要になったら履歴を取得
cd src
git fetch --unshallow
フォーク後の開発フロー
# 1. 自分のフォークをクローン
git clone git@github.com:myusername/repository.git
cd repository
# 2. オリジナルを「upstream」として追加
git remote add upstream https://github.com/original/repository.git
# 3. 最新情報を取得
git fetch upstream
git merge upstream/main
複数のリポジトリを一括クローン
clone_multiple.sh:
#!/bin/bash
# 複数のリポジトリを一括クローン
repos=(
"https://github.com/username/repo1.git"
"https://github.com/username/repo2.git"
"https://github.com/username/repo3.git"
)
for repo in "${repos[@]}"; do
echo "Cloning $repo..."
git clone "$repo"
done
echo "All repositories cloned!"
組織のリポジトリをすべてクローン
# GitHub CLIを使用(要インストール)
# 組織のすべてのリポジトリを一覧取得してクローン
org="your-organization"
gh repo list $org --limit 1000 | while read -r repo _; do
gh repo clone "$repo"
done
よくあるエラーと解決方法
エラー1:Permission denied (publickey)
# 原因:SSH認証の失敗
# 解決策1:HTTPSを使う
git clone https://github.com/username/repository.git
# 解決策2:SSHキーを正しく設定
ssh-keygen -t ed25519 -C "email@example.com"
# GitHubに公開鍵を登録
エラー2:Repository not found
# 原因1:URLが間違っている
# → URLを確認
# 原因2:プライベートリポジトリへのアクセス権限なし
# → アクセス権限をもらう
# 原因3:認証情報が間違っている
# → トークンを再生成
エラー3:SSL certificate problem
# 企業環境でよくある問題
# 一時的な解決(非推奨)
git config --global http.sslVerify false
# 正しい解決策:証明書を追加
git config --global http.sslCAInfo /path/to/certificate.pem
エラー4:RPC failed; HTTP 504
# 大きなリポジトリで発生
# 解決策1:バッファサイズを増やす
git config --global http.postBuffer 524288000 # 500MB
# 解決策2:浅いクローンを使う
git clone --depth 1 https://github.com/username/large-repo.git
高度なテクニック
ミラークローン
# 完全なミラー(すべてのブランチ、タグ、refs)
git clone --mirror https://github.com/username/repository.git
# ベアリポジトリとして取得(作業ディレクトリなし)
git clone --bare https://github.com/username/repository.git
部分クローン(Git 2.19以降)
# ファイルを遅延取得(blobless clone)
git clone --filter=blob:none https://github.com/username/repository.git
# 大きなファイルを除外
git clone --filter=blob:limit=1m https://github.com/username/repository.git
プロキシ経由でクローン
# HTTPプロキシを使用
git config --global http.proxy http://proxy.example.com:8080
git clone https://github.com/username/repository.git
# SOCKSプロキシを使用
git config --global http.proxy socks5://127.0.0.1:1080
ベストプラクティス

clone後の確認作業
# クローン直後に実行すべきコマンド
cd repository
# 1. リモート設定を確認
git remote -v
# 2. ブランチ一覧を確認
git branch -a
# 3. 最新のコミットを確認
git log --oneline -5
# 4. ステータス確認
git status
# 5. 設定確認
git config --list --local
効率的なワークフロー
# プロジェクトディレクトリを整理
mkdir ~/projects
cd ~/projects
# 組織ごとにフォルダ分け
mkdir -p github/personal
mkdir -p github/work
mkdir -p gitlab/projects
# クローン時に整理
cd ~/projects/github/work
git clone git@github.com:company/project.git
セキュリティのベストプラクティス
# 1. HTTPSでトークンを使う場合はキャッシュ設定
git config --global credential.helper 'cache --timeout=3600'
# 2. SSHの場合は鍵にパスフレーズを設定
ssh-keygen -t ed25519 -C "email@example.com" -f ~/.ssh/id_ed25519
# 3. 機密情報を含むリポジトリは必ず確認
git log --all --grep="password\|token\|secret"
まとめ – git cloneマスターへの道
git cloneコマンドの全機能、マスターできましたか?
基本を押さえる
✅ git clone URL
– 最も基本的な形 ✅ HTTPS vs SSH – 状況に応じて使い分け ✅ --depth 1
– 大規模リポジトリには必須 ✅ --branch
– 特定ブランチを取得
効率化のポイント
- 浅いクローンで時間短縮
- SSHで認証を自動化
- エイリアスやスクリプトで効率化
- 適切なディレクトリ構造で整理
次のステップ
- SSHキーをセットアップ
- よく使うリポジトリをクローン
- フォークして開発に参加
- 自分のワークフローを確立
git cloneは、Git journey の第一歩。ここから始まる開発の世界を楽しんでください!
コメント