Gitクローンコマンド完全ガイド|リポジトリを複製する全手法

「GitHubのプロジェクトをダウンロードしたいけど、どうすればいいの?」
「git cloneってどう使うの?」
「HTTPSとSSH、どっちを使えばいいの?」

開発現場に入ったとき、まず最初に使うのがgit cloneコマンドです。

この記事では、git cloneコマンドの基本から応用まで、初心者の方にも分かりやすく解説していきます。

スポンサーリンク
  1. git cloneとは?
    1. cloneの意味
    2. 何がコピーされるの?
    3. いつ使うの?
  2. 基本的な使い方
    1. 基本コマンド
    2. 実際の手順
    3. クローン先のディレクトリを指定
  3. HTTPSとSSHの違い
    1. HTTPS方式
    2. SSH方式
    3. どちらを選ぶべき?
  4. 特定のブランチをクローン
    1. -bオプション
    2. 確認
    3. 他のブランチを確認
    4. 特定のタグをクローン
  5. 浅いクローン(Shallow Clone)
    1. –depthオプション
    2. 使用例
    3. メリットとデメリット
    4. 特定のブランチを浅くクローン
  6. 単一ブランチのクローン
    1. –single-branchオプション
    2. -bとの違い
    3. –depthとの組み合わせ
  7. サブモジュールを含むクローン
    1. サブモジュールとは?
    2. 通常のクローン
    3. –recurse-submodulesオプション
    4. すでにクローンしてしまった場合
  8. その他の便利なオプション
    1. -o:リモート名を変更
    2. -q:静かにクローン
    3. –bare:ベアリポジトリ
    4. –mirror:完全なミラー
    5. -n:チェックアウトしない
  9. よくあるエラーと解決方法
    1. エラー1:Permission denied (publickey)
    2. エラー2:Repository not found
    3. エラー3:fatal: destination path already exists
    4. エラー4:Timeout / Failed to connect
    5. エラー5:SSL certificate problem
  10. 実践的な使用例
    1. ケース1:新しいプロジェクトに参加
    2. ケース2:Gitのリポジトリを試してみる
    3. ケース3:CI/CDパイプライン
    4. ケース4:複数の環境で作業
    5. ケース5:フォークしたリポジトリ
  11. クローン後の確認事項
    1. 1. リモートURLの確認
    2. 2. ブランチの確認
    3. 3. 最新の状態か確認
    4. 4. 履歴の確認
    5. 5. サブモジュールの確認
  12. よくある質問Q&A
    1. Q1:git cloneとgit pullの違いは?
    2. Q2:クローンしたリポジトリは自動で更新されますか?
    3. Q3:大きなリポジトリのクローンが遅いです
    4. Q4:クローンしたディレクトリ名を変更できますか?
    5. Q5:プライベートリポジトリをクローンするには?
    6. Q6:クローンを中断してやり直せますか?
    7. Q7:クローンしたら.gitディレクトリが見えません
  13. まとめ:git cloneを使いこなそう

git cloneとは?

まずは、git cloneの基本を理解しましょう。

cloneの意味

clone(クローン)= 複製

Gitにおけるcloneとは、リモートリポジトリの内容を、自分のローカル環境に丸ごとコピーすることです。

イメージ:

リモートリポジトリ(GitHub)
      ↓ git clone
ローカルリポジトリ(あなたのPC)

何がコピーされるの?

git cloneで取得されるもの:

  • ✅ 全てのファイル
  • ✅ 全てのブランチ
  • ✅ 全てのコミット履歴
  • ✅ 全てのタグ
  • ✅ .gitディレクトリ(リポジトリ情報)

つまり、完全なGitリポジトリのコピーが作成されます。

いつ使うの?

典型的な使用シーン:

  1. 新しいプロジェクトに参加
  • チームのリポジトリをローカルに複製
  1. オープンソースプロジェクトを利用
  • GitHubの公開リポジトリをダウンロード
  1. 他の人のコードを参照
  • 学習や研究のためにコードを取得
  1. 自分のリポジトリを別PCで
  • 別のマシンで作業を続ける

基本的な使い方

最もシンプルな形から見ていきましょう。

基本コマンド

$ git clone <リポジトリのURL>

実行例:

$ git clone https://github.com/user/repository.git

実際の手順

ステップ1:リポジトリのURLを取得

GitHubの場合:

  1. リポジトリのページを開く
  2. 緑色の「Code」ボタンをクリック
  3. HTTPSのURLをコピー

例:https://github.com/numpy/numpy.git

ステップ2:クローンしたい場所に移動

# デスクトップに移動
$ cd ~/Desktop

# 任意のプロジェクトフォルダに移動
$ cd ~/projects

ステップ3:cloneコマンドを実行

$ git clone https://github.com/numpy/numpy.git
Cloning into 'numpy'...
remote: Enumerating objects: 50000, done.
remote: Counting objects: 100% (1000/1000), done.
remote: Compressing objects: 100% (500/500), done.
remote: Total 50000 (delta 400), reused 900 (delta 350)
Receiving objects: 100% (50000/50000), 25.00 MiB | 5.00 MiB/s, done.
Resolving deltas: 100% (35000/35000), done.

ステップ4:確認

# ディレクトリができているか確認
$ ls
numpy

# 中に入る
$ cd numpy

# ファイルを確認
$ ls
README.md  setup.py  numpy/  tests/  ...

# Gitリポジトリか確認
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean

クローン先のディレクトリを指定

$ git clone <リポジトリURL> <ディレクトリ名>

例:

# 「my-project」という名前でクローン
$ git clone https://github.com/user/repository.git my-project

# 結果
$ ls
my-project

リポジトリ名と違う名前で作成したい場合に便利です。

HTTPSとSSHの違い

リポジトリをクローンする方法は2種類あります。

HTTPS方式

URL形式:

https://github.com/user/repository.git

メリット:

  • ✅ 設定が簡単
  • ✅ すぐに使える
  • ✅ ファイアウォールを通過しやすい
  • ✅ 初心者にも優しい

デメリット:

  • ❌ 毎回パスワード入力が必要(対策あり)
  • ❌ 認証情報をネットワーク経由で送信

使い方:

$ git clone https://github.com/user/repository.git
Username: your-username
Password: your-password (または personal access token)

パスワード入力を省略する方法:

認証情報をキャッシュする:

# 15分間キャッシュ
$ git config --global credential.helper cache

# 1時間キャッシュ
$ git config --global credential.helper 'cache --timeout=3600'

# 永続的に保存(Windowsの場合)
$ git config --global credential.helper wincred

# 永続的に保存(Macの場合)
$ git config --global credential.helper osxkeychain

SSH方式

URL形式:

git@github.com:user/repository.git

メリット:

  • ✅ パスワード入力不要
  • ✅ より安全(暗号化された鍵認証)
  • ✅ 認証情報がネットワークに流れない
  • ✅ 鍵を無効化しやすい

デメリット:

  • ❌ 初期設定が必要
  • ❌ SSHキーの管理が必要

使い方:

まずSSHキーの設定が必要です。

ステップ1:SSHキーを生成

$ ssh-keygen -t ed25519 -C "your-email@example.com"
Enter file in which to save the key: [Enter]
Enter passphrase: [Enter or type passphrase]

ステップ2:公開鍵をGitHubに登録

# 公開鍵を表示
$ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3... your-email@example.com
  1. GitHubの Settings → SSH and GPG keys
  2. 「New SSH key」をクリック
  3. 公開鍵を貼り付けて保存

ステップ3:SSHでクローン

$ git clone git@github.com:user/repository.git

接続テスト:

$ ssh -T git@github.com
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

どちらを選ぶべき?

初心者の方:
まずは HTTPS から始めましょう。

慣れてきたら:
SSH に切り替えると便利です。

企業・チーム開発:
多くの場合 SSH が推奨されます。

特定のブランチをクローン

デフォルトブランチ(mainやmaster)以外をクローンしたい場合。

-bオプション

$ git clone -b <ブランチ名> <リポジトリURL>

例:

# developブランチをクローン
$ git clone -b develop https://github.com/user/repository.git

確認

$ cd repository
$ git branch
* develop

注意点:

-bオプションを使っても、全てのブランチ情報はダウンロードされます

ただし、チェックアウトされるのは指定したブランチだけです。

他のブランチを確認

# リモートブランチを表示
$ git branch -r
origin/HEAD -> origin/main
origin/main
origin/develop
origin/feature/new-ui

# 全ブランチを表示
$ git branch -a
* develop
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/develop

特定のタグをクローン

タグ(バージョン)を指定することもできます:

$ git clone -b v1.0.0 https://github.com/user/repository.git

浅いクローン(Shallow Clone)

履歴が大量にある大規模プロジェクトで有効です。

–depthオプション

$ git clone --depth <深さ> <リポジトリURL>

深さの意味:

  • --depth 1:最新のコミットだけ
  • --depth 10:最新10個のコミットだけ
  • --depth 100:最新100個のコミットだけ

使用例

最新のコミットだけ取得:

$ git clone --depth 1 https://github.com/user/repository.git

通常のクローン:

Receiving objects: 100% (50000/50000), 500 MiB
時間: 10分

浅いクローン:

Receiving objects: 100% (100/100), 5 MiB
時間: 10秒

効果は絶大!

メリットとデメリット

メリット:

  • ✅ ダウンロード時間が大幅に短縮
  • ✅ ディスク容量の節約
  • ✅ CIビルドに最適

デメリット:

  • ❌ 履歴が見られない
  • ❌ 一部のGitコマンドが制限される
  • git blamegit logが不完全

推奨シーン:

  • CI/CDパイプライン
  • 一時的なビルド環境
  • 単にコードを見たいだけの場合

非推奨シーン:

  • 通常の開発作業
  • 履歴を参照する必要がある場合

特定のブランチを浅くクローン

組み合わせも可能:

$ git clone --depth 1 -b develop https://github.com/user/repository.git

これで、developブランチの最新コミットだけが取得されます。

単一ブランチのクローン

本当に1つのブランチだけが欲しい場合。

–single-branchオプション

$ git clone --single-branch --branch <ブランチ名> <リポジトリURL>

例:

$ git clone --single-branch --branch develop https://github.com/user/repository.git

-bとの違い

-b だけの場合:

$ git clone -b develop https://github.com/user/repository.git
$ git branch -r
origin/HEAD -> origin/main
origin/main
origin/develop
origin/feature/...
# ← 全ブランチの情報がある

–single-branch の場合:

$ git clone --single-branch -b develop https://github.com/user/repository.git
$ git branch -r
origin/develop
# ← developだけ!

–depthとの組み合わせ

最強の組み合わせ:

$ git clone --depth 1 --single-branch --branch main https://github.com/user/repository.git

これで:

  • mainブランチだけ
  • 最新のコミットだけ

超高速&最小サイズ!

サブモジュールを含むクローン

プロジェクトがサブモジュールを使っている場合。

サブモジュールとは?

サブモジュール:
別のGitリポジトリを、現在のリポジトリ内に含める機能。

例えば:

my-project/
├── src/
├── lib/
│   └── external-library/  ← これがサブモジュール
└── README.md

通常のクローン

$ git clone https://github.com/user/project-with-submodules.git
$ cd project-with-submodules
$ ls lib/external-library
# 空っぽ!

サブモジュールは自動ではクローンされません。

–recurse-submodulesオプション

$ git clone --recurse-submodules https://github.com/user/project-with-submodules.git

これで、サブモジュールも含めて全て取得されます。

すでにクローンしてしまった場合

後からサブモジュールを取得:

# サブモジュールを初期化して取得
$ git submodule update --init --recursive

その他の便利なオプション

実務で役立つオプション集です。

-o:リモート名を変更

デフォルトではoriginという名前が付きますが、変更できます:

$ git clone -o upstream https://github.com/user/repository.git

確認:

$ git remote -v
upstream    https://github.com/user/repository.git (fetch)
upstream    https://github.com/user/repository.git (push)

-q:静かにクローン

進捗表示を省略:

$ git clone -q https://github.com/user/repository.git

スクリプトで使う際に便利です。

–bare:ベアリポジトリ

作業ディレクトリなしのリポジトリ:

$ git clone --bare https://github.com/user/repository.git

用途:

  • サーバー上での共有リポジトリ
  • ミラーリポジトリ
  • バックアップ

通常のクローンとの違い:

通常:repository/.git/(リポジトリ情報)
      repository/(作業ファイル)

bare:repository.git/(リポジトリ情報のみ)

–mirror:完全なミラー

全ての参照を含む完全なコピー:

$ git clone --mirror https://github.com/user/repository.git

bareとの違い:

  • --mirrorは全ての参照(refs)を含む
  • 全てのブランチ、タグ、リモート追跡ブランチ

用途:

  • リポジトリの完全バックアップ
  • 別のGitホスティングサービスへの移行

-n:チェックアウトしない

.gitディレクトリだけ取得:

$ git clone -n https://github.com/user/repository.git

作業ディレクトリにファイルは展開されません。

用途:

  • 特定のファイルだけ後で取り出したい
  • 大規模リポジトリで一部だけ必要な場合

よくあるエラーと解決方法

実際によく遭遇するエラーです。

エラー1:Permission denied (publickey)

エラー内容:

$ git clone git@github.com:user/repository.git
Permission denied (publickey).
fatal: Could not read from remote repository.

原因:
SSHキーが設定されていない、または認識されていない

解決方法:

方法1:HTTPSに切り替え

$ git clone https://github.com/user/repository.git

方法2:SSHキーを設定

# SSHキーを生成
$ ssh-keygen -t ed25519 -C "your-email@example.com"

# 公開鍵を表示してGitHubに登録
$ cat ~/.ssh/id_ed25519.pub

# SSH接続をテスト
$ ssh -T git@github.com

エラー2:Repository not found

エラー内容:

$ git clone https://github.com/user/repository.git
fatal: repository 'https://github.com/user/repository.git/' not found

原因:

  • URLのタイポ
  • リポジトリが存在しない
  • プライベートリポジトリで権限がない

解決方法:

  1. URLを再確認
# 正しいURL形式か確認
https://github.com/<username>/<repository-name>.git
  1. 権限を確認
  • プライベートリポジトリへのアクセス権があるか
  • 招待を受け入れたか
  1. 認証情報を確認
# HTTPSの場合、正しい認証情報か確認
Username: <your-username>
Password: <personal-access-token>

エラー3:fatal: destination path already exists

エラー内容:

$ git clone https://github.com/user/repository.git
fatal: destination path 'repository' already exists and is not an empty directory.

原因:
同名のディレクトリが既に存在する

解決方法:

方法1:別名でクローン

$ git clone https://github.com/user/repository.git repository-new

方法2:既存ディレクトリを削除

$ rm -rf repository
$ git clone https://github.com/user/repository.git

方法3:既存ディレクトリをリネーム

$ mv repository repository-old
$ git clone https://github.com/user/repository.git

エラー4:Timeout / Failed to connect

エラー内容:

$ git clone https://github.com/user/repository.git
fatal: unable to access 'https://github.com/user/repository.git/': 
Failed to connect to github.com port 443: Connection timed out

原因:

  • ネットワーク問題
  • ファイアウォール
  • プロキシ設定

解決方法:

ネットワークを確認:

$ ping github.com

プロキシを設定:

$ git config --global http.proxy http://proxy.example.com:8080
$ git config --global https.proxy https://proxy.example.com:8080

プロキシを解除:

$ git config --global --unset http.proxy
$ git config --global --unset https.proxy

エラー5:SSL certificate problem

エラー内容:

fatal: unable to access 'https://...': SSL certificate problem

原因:
SSL証明書の検証エラー

一時的な解決:

$ git -c http.sslVerify=false clone https://github.com/user/repository.git

恒久的な解決(非推奨):

$ git config --global http.sslVerify false

推奨される解決:

# CA証明書を更新
$ sudo apt-get install ca-certificates  # Ubuntu/Debian
$ brew install ca-certificates          # macOS

実践的な使用例

実際の開発シーンでの使い方です。

ケース1:新しいプロジェクトに参加

シナリオ:
チームに参加して、最初にコードを取得する

手順:

# プロジェクトディレクトリに移動
$ cd ~/projects

# HTTPSでクローン
$ git clone https://github.com/company/project.git

# または SSHでクローン
$ git clone git@github.com:company/project.git

# プロジェクトに入る
$ cd project

# ブランチを確認
$ git branch -a

# 開発用ブランチにチェックアウト
$ git checkout develop

# 依存関係をインストール
$ npm install  # または yarn install, pip install など

ケース2:Gitのリポジトリを試してみる

シナリオ:
人気のOSSプロジェクトのコードを読みたい

手順:

# 一時ディレクトリに移動
$ cd /tmp

# 浅いクローンで高速取得
$ git clone --depth 1 https://github.com/facebook/react.git

# コードを閲覧
$ cd react
$ ls -la

ケース3:CI/CDパイプライン

シナリオ:
自動ビルドスクリプト

スクリプト例:

#!/bin/bash

# 高速に最新コードを取得
git clone --depth 1 --single-branch --branch main https://github.com/company/project.git build-dir

cd build-dir

# ビルド実行
npm ci
npm run build
npm test

# ビルド成果物をデプロイ
# ...

# クリーンアップ
cd ..
rm -rf build-dir

ケース4:複数の環境で作業

シナリオ:
自宅のPCと会社のPC、両方で開発したい

自宅PCで:

$ cd ~/projects
$ git clone git@github.com:username/my-project.git
$ cd my-project
# 作業...
$ git commit -am "Add feature"
$ git push

会社PCで:

$ cd ~/work
$ git clone git@github.com:username/my-project.git
$ cd my-project
# 最新を取得
$ git pull
# 続きを作業...

ケース5:フォークしたリポジトリ

シナリオ:
OSSに貢献したい

手順:

# 1. GitHubでリポジトリをフォーク

# 2. 自分のフォークをクローン
$ git clone git@github.com:yourusername/project.git
$ cd project

# 3. 元のリポジトリをupstreamとして追加
$ git remote add upstream https://github.com/original/project.git

# 4. リモートを確認
$ git remote -v
origin    git@github.com:yourusername/project.git (fetch)
origin    git@github.com:yourusername/project.git (push)
upstream  https://github.com/original/project.git (fetch)
upstream  https://github.com/original/project.git (push)

# 5. upstreamから最新を取得
$ git fetch upstream
$ git merge upstream/main

クローン後の確認事項

クローンした後にチェックすべきポイントです。

1. リモートURLの確認

$ git remote -v
origin  https://github.com/user/repository.git (fetch)
origin  https://github.com/user/repository.git (push)

2. ブランチの確認

# ローカルブランチ
$ git branch
* main

# 全ブランチ
$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/develop

3. 最新の状態か確認

$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean

4. 履歴の確認

# 最近のコミット
$ git log --oneline -10

# グラフ表示
$ git log --oneline --graph --all -10

5. サブモジュールの確認

# サブモジュールがあるか確認
$ cat .gitmodules

# サブモジュールの状態
$ git submodule status

よくある質問Q&A

Q1:git cloneとgit pullの違いは?

A:用途が全く違います。

git clone:

  • 初回にリポジトリ全体を取得
  • 新しいディレクトリが作成される
  • 一度だけ実行

git pull:

  • 既存のリポジトリを更新
  • 既にクローンした後に使う
  • 何度でも実行可能

使い分け:

# 最初の1回だけ
$ git clone https://github.com/user/repo.git

# その後の更新
$ cd repo
$ git pull

Q2:クローンしたリポジトリは自動で更新されますか?

A:いいえ、手動で更新が必要です。

リモートが更新されても、ローカルは自動では更新されません。

更新方法:

$ git pull

定期的にgit pullを実行しましょう。

Q3:大きなリポジトリのクローンが遅いです

A:浅いクローンを使いましょう。

# 最新だけ
$ git clone --depth 1 https://github.com/user/large-repo.git

# 特定のブランチだけ
$ git clone --depth 1 --single-branch --branch main https://github.com/user/large-repo.git

これで、ダウンロード時間とサイズが大幅に削減されます。

Q4:クローンしたディレクトリ名を変更できますか?

A:はい、2つの方法があります。

方法1:クローン時に指定

$ git clone https://github.com/user/repository.git my-custom-name

方法2:後でリネーム

$ git clone https://github.com/user/repository.git
$ mv repository my-custom-name

どちらでも動作します。

Q5:プライベートリポジトリをクローンするには?

A:認証が必要です。

HTTPSの場合:

$ git clone https://github.com/user/private-repo.git
Username: your-username
Password: your-personal-access-token

注意:
GitHubではパスワードではなく、Personal Access Tokenを使用します。

SSHの場合:

# SSHキーを設定後
$ git clone git@github.com:user/private-repo.git

Q6:クローンを中断してやり直せますか?

A:はい、Ctrl+Cで中断後、再実行できます。

$ git clone https://github.com/user/large-repo.git
^C  # Ctrl+Cで中断

# 部分的なディレクトリを削除
$ rm -rf large-repo

# 再実行
$ git clone https://github.com/user/large-repo.git

Gitは自動的に続きから再開しません。最初からやり直します。

Q7:クローンしたら.gitディレクトリが見えません

A:隠しファイルです。ls -aで表示できます。

# 通常の ls
$ ls
README.md  src/  ...

# 隠しファイルも表示
$ ls -a
.  ..  .git  .gitignore  README.md  src/  ...

.gitディレクトリにGitの全情報が含まれています。

まとめ:git cloneを使いこなそう

長い記事を読んでいただき、ありがとうございました!

この記事の重要ポイントをまとめます:

基本コマンド

# 最もシンプル
git clone <URL>

# ディレクトリ名指定
git clone <URL> <ディレクトリ名>

便利なオプション

# 特定のブランチ
git clone -b <ブランチ名> <URL>

# 浅いクローン
git clone --depth 1 <URL>

# 単一ブランチ
git clone --single-branch -b <ブランチ名> <URL>

# サブモジュール含む
git clone --recurse-submodules <URL>

HTTPSとSSH

  • HTTPS:簡単だが毎回認証が必要
  • SSH:設定は必要だが、以降は認証不要

推奨設定

  • 初心者:HTTPS
  • 慣れたら:SSH
  • CI/CD:–depth 1

よく使うシーン
✅ 新規プロジェクトに参加
✅ OSSコードを読む
✅ 自分のリポジトリを別PCで
✅ フォークして貢献

git cloneは、Git開発の第一歩です。

この記事を参考に、効率的にリポジトリを取得して、開発をスムーズに始めてくださいね!

コメント

タイトルとURLをコピーしました