git clone – 既存リポジトリをコピーするコマンド

リポジトリ管理

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 を指定して効率的にクローンするのが有効です。

参考

Bash玄

はじめまして!Bash玄です。

エンジニアとしてシステム運用に携わる中で、手作業の多さに限界を感じ、Bashスクリプトを活用して業務を効率化したのがきっかけで、この道に入りました。「手作業は負け」「スクリプトはシンプルに」をモットーに、誰でも実践できるBashスクリプトの書き方を発信しています。

このサイトでは、Bashの基礎から実践的なスクリプト作成まで、初心者でもわかりやすく解説しています。少しでも「Bashって便利だな」と思ってもらえたら嬉しいです!

# 好きなこと
- シンプルなコードを書くこと
- コマンドラインを快適にカスタマイズすること
- 自動化で時間を生み出すこと

# このサイトを読んでほしい人
- Bashに興味があるけど、何から始めればいいかわからない人
- 定型業務を自動化したい人
- 効率よくターミナルを使いこなしたい人

Bashの世界に一歩踏み出して、一緒に「Bash道」を極めていきましょう!

Bash玄をフォローする

コメント