git clone: Clone a Repository
git clone copies an existing Git repository into a new directory on your local machine. It sets up tracking branches for each remote branch and creates a remote called origin pointing back to the source.
This guide explains how to use git clone with the most common options and protocols.
Syntax
The basic syntax of the git clone command is:
git clone [OPTIONS] REPOSITORY [DIRECTORY]
REPOSITORY is the URL or path of the repository to clone. DIRECTORY is optional; if omitted, Git uses the repository name as the directory name.
Clone a Remote Repository
The most common way to clone a repository is over HTTPS. For example, to clone a GitHub repository:
Terminal
git clone https://github.com/user/repo.git
This creates a repo directory in the current working directory, initializes a .git directory inside it, and checks out the default branch.
You can also clone over SSH if you have an SSH key configured
:
Terminal
git clone [email protected]:user/repo.git
SSH is generally preferred for repositories you will push to, as it does not require entering credentials each time.
Clone into a Specific Directory
By default, git clone creates a directory named after the repository. To clone into a different directory, pass the target path as the second argument:
Terminal
git clone https://github.com/user/repo.git my-project
To clone into the current directory, use . as the target:
Terminal
git clone https://github.com/user/repo.git .
Warning
. requires the current directory to be empty.Clone a Specific Branch
By default, git clone checks out the default branch (usually main or master). To clone and check out a different branch, use the -b option:
Terminal
git clone -b develop https://github.com/user/repo.git
The full repository history is still downloaded; only the checked-out branch differs. To limit the download to a single branch, combine -b with --single-branch:
Terminal
git clone -b develop --single-branch https://github.com/user/repo.git
Shallow Clone
A shallow clone downloads only a limited number of recent commits rather than the full history. This is useful for speeding up the clone of large repositories when you do not need the full commit history.
To clone with only the latest commit:
Terminal
git clone --depth 1 https://github.com/user/repo.git
To include the last 10 commits:
Terminal
git clone --depth 10 https://github.com/user/repo.git
Shallow clones are commonly used in CI/CD pipelines to reduce download time. To convert a shallow clone to a full clone later, run:
Terminal
git fetch --unshallow
Clone a Local Repository
git clone also works with local paths. This is useful for creating an isolated copy for testing:
Terminal
git clone /path/to/local/repo new-copy
Troubleshooting
Destination directory is not empty
git clone URL . works only when the current directory is empty. If files already exist, clone into a new directory or move the existing files out of the way first.
Authentication failed over HTTPS
Most Git hosting services no longer accept account passwords for Git operations over HTTPS. Use a personal access token or configure a credential manager so Git can authenticate securely.
Permission denied (publickey)
This error usually means your SSH key is missing, not loaded into your SSH agent, or not added to your account on the Git hosting service. Verify your SSH setup before retrying the clone.
Host key verification failed
SSH could not verify the identity of the remote server. Confirm you are connecting to the correct host, then accept or update the host key in ~/.ssh/known_hosts.
Quick Reference
For a printable quick reference, see the Git cheatsheet
.
| Command | Description |
|---|---|
git clone URL |
Clone a repository via HTTPS or SSH |
git clone URL dir |
Clone into a specific directory |
git clone URL . |
Clone into the current (empty) directory |
git clone -b branch URL |
Clone and check out a specific branch |
git clone -b branch --single-branch URL |
Clone only a specific branch |
git clone --depth 1 URL |
Shallow clone: latest commit only |
git clone --depth N URL |
Shallow clone: last N commits |
git clone /path/to/repo |
Clone a local repository |
FAQ
What is the difference between HTTPS and SSH cloning?
HTTPS cloning works out of the box but prompts for credentials unless you use a credential manager. SSH cloning requires a key pair to be configured but does not prompt for a password on each push or pull.
Does git clone download all branches?
Yes. All remote branches are fetched, but only the default branch is checked out locally. Use git branch -r to list all remote branches, or git checkout branch-name to switch to one.
How do I clone a private repository?
For HTTPS, provide your username and a personal access token when prompted. For SSH, ensure your public key is added to your account on the hosting service.
Can I clone a specific tag instead of a branch?
Yes. Use -b with the tag name: git clone -b v1.0.0 https://github.com/user/repo.git. Git checks out that tag in a detached HEAD state, so create a branch if you plan to make commits.
Conclusion
git clone is the starting point for working with any existing Git repository. After cloning, you can inspect the commit history with git log
, review changes with git diff
, and configure your username and email
if you have not done so already.
