Your Complete Guide to Git Clone Command
git clone command followed by the repository’s URL to clone a Git repository. This creates a complete local copy of the remote repository, including its entire commit history, branches, and tags. This local copy is a fully functional Git repository. It maintains a connection to its original source.What Does Git Clone Actually Do?
The git clone command performs a fundamental operation in Git. It makes an exact, independent replica of an existing remote repository onto your local machine. This replica isn’t just a simple copy of files. It’s a fully functional Git repository itself, containing all the project’s historical data. This includes every commit, every branch, and every tag from the original source. It also automatically configures a remote tracking connection, usually named “origin,” pointing back to the original repository.
After cloning, you have a complete development environment ready to go. You can make changes and commit them locally. You can also push them back to the remote repository (if you have the necessary permissions). It’s the standard method for initiating work on projects hosted on platforms like GitHub, GitLab, or Bitbucket. It provides a solid foundation for collaborative version control.
How to Use the Git Clone Command
When you clone a repository, you typically specify the remote repository’s URL. There are two primary protocols for this: HTTPS and SSH. We generally recommend SSH for authenticated access due to its convenience with SSH keys. However, HTTPS is often simpler for public repositories or when you use personal access tokens.
Cloning via HTTPS
HTTPS is a common method, especially for public repositories or when you’re comfortable entering your username and password (or a personal access token) for authentication. Here is the basic command:
git clone https://github.com/username/repository-name.git
When you run this command, Git will download the repository. If it’s a private repository or if you’re pushing changes, Git will prompt you for your username and password or a personal access token. This method is generally straightforward and works through most firewalls.
Cloning via SSH
SSH provides a secure way to access a Git repository without repeatedly entering credentials, relying on an SSH key pair. If you have an SSH key set up and added to your Git hosting service (like GitHub or GitLab), this is often my preferred method for daily development.
git clone git@github.com:username/repository-name.git
With SSH, Git uses your local SSH agent to authenticate. You typically don’t need to enter a password once you configure your key. If your SSH key is password-protected, Git might prompt you for the passphrase for your key the first time you use it in a session.
Cloning a Specific Branch or Directory
By default, git clone downloads the entire repository with all branches and tags. However, it only checks out the default branch (usually main or master). You might only need a specific branch or want to place the repository in a different location.
How to Clone a Specific Branch
If you only need a particular branch and its history, you can use the --branch (or -b) option. Combining this with --single-branch ensures that Git only downloads the history leading to the tip of that specific branch. This can save bandwidth and local storage for very large repositories.
git clone --branch <branch-name> --single-branch https://github.com/username/repository-name.git
Replace <branch-name> with the actual name of the branch you want to clone. After running this, your local repository will have only that specific branch checked out. Its history will reflect only commits on that branch.
Cloning to a Specific Directory
By default, Git clones the repository into a new directory with the same name as the repository. If you want to clone it into a different directory name or an existing empty directory, you can specify the target directory as the last argument:
git clone https://github.com/username/repository-name.git my-project-folder
This command will clone the repository-name.git repository into a new directory named my-project-folder in your current working directory. You can clone into an existing directory only if that directory is empty.
Understanding Shallow Clones and Submodules
For very large repositories with extensive history, a full clone can be slow and consume significant disk space. Git offers options to optimize this, such as shallow clones and handling submodules.
Why Git Clone Might Be Slow: Shallow Clones
If your git clone operation is slow, especially for older or very active projects, it’s likely due to the large amount of history Git downloads. A shallow clone truncates the commit history to a specified number of commits. This reduces download size and time. This is particularly useful for CI/CD pipelines or when you only need the latest version of the code.
git clone --depth 1 https://github.com/username/repository-name.git
The --depth 1 option downloads only the most recent commit, significantly speeding up the process. However, shallow clones have limitations: you’ll have restricted access to the full history for certain Git operations like rebasing or bisecting, as the complete commit graph isn’t available. If you later need the full history, you can “unshallow” the repository using git fetch --unshallow.

Cloning with Submodules (Recursive Clone)
Many projects use Git submodules to include other repositories within their own. By default, git clone will download the main repository but won’t initialize or update its submodules. To clone a repository and all its submodules in one go, you need the --recurse-submodules option:
git clone --recurse-submodules https://github.com/username/repository-name.git
This command ensures that after cloning the main repository, Git also clones each submodule into its designated directory. Without this, you would need to navigate into the cloned repository and run git submodule update --init --recursive manually.
Why Git Clone Is Not Working: Troubleshooting Common Issues
While git clone is generally reliable, you might encounter issues. Here are some common reasons it might not work as expected and how to address them.
-
- Incorrect Repository URL: The most frequent issue is a typo in the URL. Double-check that the URL is correct and matches what’s provided by your Git hosting service (e.g., GitHub, GitLab).
fatal: repository 'https://github.com/user/repo.git' not foundThis error typically indicates a wrong URL or that the repository is private and you lack access.
- Authentication Problems: For private repositories, you need proper authentication. If using HTTPS, ensure your username and password (or personal access token) are correct. If using SSH, verify your SSH key is correctly set up, added to your SSH agent, and registered with your Git hosting service.
Cloning into 'repository-name'... remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead. fatal: Authentication failed for 'https://github.com/username/repository-name.git/'This GitHub-specific message indicates password authentication is no longer supported for HTTPS; you need a personal access token.
For SSH, errors like “Permission denied (publickey)” suggest an issue with your SSH key setup.
- Network Connectivity Issues: Ensure you have a stable internet connection and that no firewall is blocking access to the Git server. Try pinging the Git hosting service’s domain (e.g.,
ping github.com) to check connectivity. - Existing Non-Empty Directory: If you specify a target directory, it must be empty or not exist. Git will refuse to clone into a non-empty directory to prevent accidental overwrites.
fatal: destination path 'my-project-folder' already exists and is not an empty directory.To fix this, either choose a different directory name, or empty the existing directory before cloning.
- Incorrect Repository URL: The most frequent issue is a typo in the URL. Double-check that the URL is correct and matches what’s provided by your Git hosting service (e.g., GitHub, GitLab).
How to Confirm It Is Fixed
After running git clone, you should see a new directory created in your current working directory (or the specified target directory). Navigate into this new directory and confirm it’s a valid Git repository.
- Change into the cloned directory:
cd repository-name -
Check the Git status:
git statusYou should see output indicating you are on a specific branch (e.g., “On branch main”) and that your branch is up to date with ‘origin/main’. This confirms the repository was successfully cloned and Git recognizes it.
-
Verify remote connection:
git remote -vThis command should show the fetch and push URLs for the ‘origin’ remote, confirming the connection to the original remote repository.
The latest stable version of Git, 2.56.0, released on September 28, 2026, includes various improvements and bug fixes, but the core functionality of git clone remains consistent across versions.
Related guides
Sources: Git
Frequently asked questions
What is the ‘origin’ remote in Git?
When you use git clone, Git automatically sets up a default remote connection named “origin.” This ‘origin’ is simply an alias for the URL of the remote repository from which you cloned, allowing you to refer to the original repository by a simple name for commands like git fetch origin or git push origin.
Can I clone a repository without an internet connection?
No, you cannot clone a remote repository without an internet connection because git clone needs to connect to the remote server to download the repository’s data. However, once cloned, you can work locally offline and commit changes to your local repository.
Is ‘git clone’ a backup solution?
While git clone creates a full copy of a repository’s history and files, it is not designed to be a complete backup solution on its own. It may omit certain metadata. This includes remote tracking branches that aren’t checked out locally or platform-specific information from your hosting service. For comprehensive backups, consider dedicated backup strategies alongside cloning.