git clone コマンドは、既存の Git リポジトリを複製して新しい作業ディレクトリを作成するためのコマンドです。
リモートリポジトリを取得して作業を始めたいときに利用されます。
構文(Syntax)
git clone [オプション] <リポジトリURL> [ディレクトリ名]
主なオプション一覧
| オプション | 説明 | 使用例 |
|---|---|---|
| (なし) | リポジトリを取得して作業ディレクトリを作成 | git clone https://github.com/user/repo.git |
[ディレクトリ名] | クローン先のディレクトリ名を指定 | git clone https://github.com/user/repo.git myproject |
--branch BRANCH | 特定のブランチをチェックアウト | git clone --branch develop https://github.com/user/repo.git |
--depth N | 履歴を指定した深さで取得(浅いクローン) | git clone --depth 1 https://github.com/user/repo.git |
--bare | ベアリポジトリとしてクローン | git clone --bare https://github.com/user/repo.git |
--mirror | すべての参照を含む完全ミラーとしてクローン | git clone --mirror https://github.com/user/repo.git |
実行例
通常のリポジトリ複製
git clone https://github.com/user/repo.git
出力例:
Cloning into 'repo'...
remote: Enumerating objects: 42, done.
remote: Counting objects: 100% (42/42), done.
Receiving objects: 100% (42/42), 8.21 KiB | 8.21 MiB/s, done.
クローン先ディレクトリを指定
git clone https://github.com/user/repo.git myproject
(myproject/ ディレクトリに展開される)
特定ブランチを取得
git clone --branch develop https://github.com/user/repo.git
履歴を浅くクローン(最新コミットのみ)
git clone --depth 1 https://github.com/user/repo.git
ベアリポジトリとしてクローン
git clone --bare https://github.com/user/repo.git repo.git
エラー例(無効なURL)
git clone https://github.com/user/notfound.git
出力例:
Cloning into 'notfound'...
fatal: repository 'https://github.com/user/notfound.git/' not found
関連コマンド
git init: 新しいリポジトリを初期化する。git fetch: リモートから変更を取得する(作業ツリーは更新しない)。git pull: リモートの変更を取得してマージする。
備考
git clone実行時に.gitディレクトリが作成され、完全な Git 管理下になります。- 通常は
originという名前でリモートが登録されます。 - 大規模リポジトリでは
--depthを指定して効率的にクローンするのが有効です。
参考
- Git公式ドキュメント: https://git-scm.com/docs/git-clone

コメント