git clone Command Guide
Clone a repository into a new directory
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git clone <url>Clone repositorygit clone <url> <dir>Clone to directorygit clone -b <branch> <url>Clone specific branchShallow
--depth 1Shallow clone (latest only)--shallow-since=<date>Clone since date--single-branchClone single branchOptions
--recurse-submodulesInclude submodules--bareClone as bare repo--mirrorMirror the repositoryDownloadable Image Preview
Basic Usage
The git clone command creates a copy of an existing Git repository. It downloads all files, branches, and commit history.
# Clone a repository
git clone https://github.com/user/repo.git
# Clone into a specific directory
git clone https://github.com/user/repo.git my-folder
# Clone a specific branch
git clone -b develop https://github.com/user/repo.gitClone Protocols
Git supports several protocols for cloning:
# HTTPS (most common, works through firewalls)
git clone https://github.com/user/repo.git
# SSH (requires SSH key setup)
git clone git@github.com:user/repo.git
# Git protocol (read-only, no authentication)
git clone git://github.com/user/repo.git
# Local path
git clone /path/to/local/repo
git clone file:///path/to/local/repoCommon Options
| Option | Description |
|---|---|
| -b <branch> | Clone a specific branch |
| --depth <n> | Create a shallow clone with n commits |
| --single-branch | Clone only one branch |
| --recurse-submodules | Initialize and clone submodules |
| --bare | Clone as a bare repository |
| --mirror | Create a mirror (all refs) |
Shallow Clones
Shallow clones download only recent history, useful for large repositories:
# Clone only the latest commit
git clone --depth 1 https://github.com/user/repo.git
# Clone with limited history
git clone --depth 10 https://github.com/user/repo.git
# Clone since a specific date
git clone --shallow-since="2024-01-01" https://github.com/user/repo.git
# Shallow clone of single branch
git clone --depth 1 --single-branch -b main https://github.com/user/repo.git
# Convert shallow to full clone later
git fetch --unshallowPractical Examples
# Basic clone and start working
git clone https://github.com/user/repo.git
cd repo
# Clone for CI/CD (fast, minimal)
git clone --depth 1 --single-branch https://github.com/user/repo.git
# Clone with all submodules
git clone --recurse-submodules https://github.com/user/repo.git
# Clone specific branch for feature work
git clone -b feature-branch https://github.com/user/repo.git
# Mirror a repository for backup
git clone --mirror https://github.com/user/repo.git
# Clone to a different folder name
git clone https://github.com/user/repo.git my-projectFrequently Asked Questions
What is the difference between HTTPS and SSH clone URLs?
HTTPS is simpler and works through firewalls but requires entering credentials. SSH uses key-based authentication and is more convenient once set up, especially for frequent pushes.
How do I clone a specific branch?
Use "git clone -b <branch-name> <url>" to clone a specific branch. Add --single-branch to only fetch that branch's history.
What is a shallow clone?
A shallow clone (--depth) only downloads recent history, not the entire repository history. This is faster and uses less disk space, useful for CI/CD or large repositories.
How do I clone a repository with submodules?
Use "git clone --recurse-submodules <url>" to clone the repository and all its submodules. For existing clones, run "git submodule update --init --recursive".
Can I clone a private repository?
Yes, you need proper authentication. For HTTPS, you'll be prompted for credentials (or use a personal access token). For SSH, you need your SSH key added to the remote service.
Summary
git clone is how you get a copy of a repository. Use shallow clones for speed, SSH for convenience, and remember submodules if needed.
Quick Reference
git clone <url>- Clone repositorygit clone -b <branch>- Clone specific branchgit clone --depth 1- Shallow clonegit clone --recurse-submodules- Include submodules
Official Documentation
For authoritative information, refer to the official documentation: