The git clone command is used to create a local copy of a remote Git repository. It downloads all project files along with the commit history and automatically connects your local repository to the remote source.
Developers commonly use git clone to copy repositories from platforms like GitHub, GitLab, or Bitbucket to their Linux or Ubuntu systems for development and then synchronize changes using
git pull.
Git Clone Quick Reference
| Description | Command |
|---|---|
| Clone a repository | git clone <repository-url> |
| Clone repository into a custom directory | git clone <repository-url> project-name |
| Clone repository using SSH | git clone git@github.com:user/repository.git |
| Clone specific branch | git clone --branch <branch-name> <repository-url> |
| Clone only latest commit (shallow clone) | git clone --depth 1 <repository-url> |
| Clone single branch only | git clone --single-branch -b <branch-name> <repository-url> |
| Clone repository without checking out files | git clone --no-checkout <repository-url> |
| Clone repository into current directory | git clone <repository-url> . |
Git Clone Command Syntax
The git clone command is used to create a local copy of a remote Git repository. When you clone a repository, Git downloads all project files along with the commit history and automatically sets the remote repository as origin, which you can verify using
git remote.
Basic git clone command syntax
The general syntax of the git clone command is:
git clone <repository-url>Example:
git clone https://github.com/user/project.gitThis command creates a new directory named project in the current location and downloads the repository contents.
Most common git clone options explained
Some frequently used options with git clone include:
| Option | Description |
|---|---|
--branch <branch> | Clone a specific branch |
--depth <number> | Perform a shallow clone with limited history |
--single-branch | Clone only one branch instead of all branches |
--recurse-submodules | Clone repository including submodules |
--quiet | Suppress progress output |
Example:
git clone --branch develop https://github.com/user/project.gitThis clones only the develop branch instead of the default branch, which can later be switched using git checkout or git switch.
Example git clone command in Linux
In Linux systems such as Ubuntu, Debian, Red Hat, Alma, Rocky or Kali Linux, you can clone a repository using the following command:
git clone https://github.com/golinuxcloud/sample-project.gitOutput example:
Cloning into 'sample-project'...
remote: Enumerating objects: 45, done.
remote: Counting objects: 100% (45/45), done.
Receiving objects: 100% (45/45), done.After cloning, a new directory containing the project files will be created and you can inspect branches using git branch.
Clone Git Repository in Linux or Ubuntu
Cloning a repository allows developers to download project code from a remote server such as GitHub, GitLab, or Bitbucket to a local Linux machine.
Clone repository using HTTPS URL
The easiest way to clone a repository is by using an HTTPS URL.
Example:
git clone https://github.com/user/repository.gitThis method may require entering your Git credentials or access token when accessing private repositories, especially when pushing changes using git push.
Clone repository using SSH URL
If SSH authentication is configured, cloning with SSH is more secure and avoids entering credentials repeatedly.
Example:
git clone git@github.com:user/repository.gitSSH cloning requires that your SSH public key is added to your Git account, which is commonly used when working with remote repositories configured using git remote.
Clone repository from GitHub or GitLab
Both GitHub and GitLab provide clone URLs in the repository page.
Example cloning from GitHub:
git clone https://github.com/golinuxcloud/git-demo.gitExample cloning from GitLab:
git clone https://gitlab.com/golinuxcloud/git-demo.gitBoth commands download the repository to your local system.
Verify cloned repository contents
After cloning, navigate to the repository directory and list the files:
cd git-demo
ls -lYou should also see a hidden .git directory which contains the Git metadata.
ls -aExample output:
.git
README.md
srcClone Repository into a Custom Directory
By default, Git creates a directory using the repository name. However, you can specify a different directory name.
Clone repo with custom folder name
Example:
git clone https://github.com/user/project.git myprojectThis will create a directory named myproject instead of project.
Clone repo into existing directory
You can also clone the repository into the current directory:
git clone https://github.com/user/project.git .Make sure the directory is empty before running this command.
Clone repository with specific branch
To clone a repository and checkout a specific branch immediately, use:
git clone --branch develop https://github.com/user/project.gitThis downloads the repository and checks out the develop branch.
Clone Private Repository Using SSH Key
Private repositories usually require authentication. Using SSH keys allows secure passwordless access.
Generate SSH key for Git authentication
Create a new SSH key pair using:
ssh-keygen -t rsa -b 4096This generates a public key and private key in the .ssh directory.
Example location:
~/.ssh/id_rsa
~/.ssh/id_rsa.pubAdd SSH key to GitHub or GitLab
Copy the contents of the public key:
cat ~/.ssh/id_rsa.pubThen add it to your Git platform:
- GitHub: Settings → SSH and GPG Keys → New SSH Key
- GitLab: Profile → SSH Keys → Add key
Paste the key and save.
Clone repository securely using SSH
Once the SSH key is configured, clone the repository using the SSH URL:
git clone git@github.com:user/private-repo.gitGit will authenticate using your SSH key without prompting for a password.
Clone Specific Branch or Tag
Sometimes you may not need the full repository history or all branches. Git allows cloning a specific branch or a shallow copy to reduce download time and disk usage.
Clone a specific branch using --branch
To clone and automatically checkout a specific branch, use the --branch option.
git clone --branch <branch-name> <repository-url>Example:
git clone --branch develop https://github.com/user/project.gitThis command clones the repository and checks out the develop branch instead of the default branch.
Clone only latest commit using --depth
Large repositories can have thousands of commits. You can clone only the most recent commit using the --depth option.
git clone --depth 1 <repository-url>Example:
git clone --depth 1 https://github.com/user/project.gitThis creates a shallow clone, downloading only the latest commit and significantly reducing clone time. You can inspect commits later using git log.
Shallow clone to save disk space
You can also limit the history to a specific number of commits.
git clone --depth 50 https://github.com/user/project.gitThis downloads only the last 50 commits, which reduces disk usage and speeds up cloning.
Clone Large Repository Efficiently
When working with large repositories, cloning the entire history and all branches may take time and consume disk space. Git provides options to make cloning faster and more efficient.
Use shallow clone for faster download
A shallow clone downloads only recent commits instead of the entire history.
git clone --depth 1 https://github.com/user/project.gitThis method is ideal when you only need the latest version of the project.
Clone only single branch
If you only need one branch, you can clone that branch without downloading others.
git clone --single-branch --branch main https://github.com/user/project.gitThis downloads only the main branch and ignores other branches.
Clone repository without checkout
Sometimes you may want to download the repository metadata without immediately checking out the working files.
git clone --no-checkout https://github.com/user/project.gitAfter cloning, you can manually checkout the files when needed using git checkout:
git checkoutFix Common Git Clone Errors
While cloning a repository, you may encounter common errors related to authentication, permissions, or network issues.
Host authenticity warning during clone
When connecting to a Git server for the first time, Git may display a message like:
The authenticity of host 'github.com' can't be established.
Are you sure you want to continue connecting (yes/no)?Type yes to continue. Git will add the host to the known_hosts file so that future connections are trusted.
Permission denied (publickey) error
If you see an error like:
Permission denied (publickey)It usually means your SSH key is not configured correctly.
Check whether your SSH key exists:
ls ~/.sshIf needed, generate a new SSH key:
ssh-keygen -t rsa -b 4096Then add the public key to your GitHub or GitLab account.
Repository not found error
This error appears when the repository URL is incorrect or when you do not have permission to access the repository.
Example error:
fatal: repository 'https://github.com/user/repository.git' not foundPossible fixes include fetching latest updates using git fetch:
- Verify the repository URL
- Ensure the repository exists
- Check your access permissions for private repositories
Network timeout or SSL errors
Sometimes cloning may fail due to network issues or SSL verification problems.
Example error:
fatal: unable to access 'https://github.com/user/project.git/': Failed to connectPossible solutions:
- Check internet connectivity
- Verify firewall or proxy settings
- Retry the clone command after confirming network access
Frequently Asked Questions
1. What does the git clone command do?
The git clone command creates a local copy of a remote Git repository. It downloads all repository files along with the complete commit history and automatically configures the remote connection.2. How do I clone a Git repository in Linux?
You can clone a repository in Linux using the commandgit clone <repository-url>. For example: git clone https://github.com/user/project.git.3. Can I clone a Git repository using SSH?
Yes. If SSH keys are configured, you can clone using an SSH URL such asgit clone git@github.com:user/repository.git. This allows secure authentication without entering a password each time.4. How do I clone a specific branch in Git?
You can clone a specific branch usinggit clone --branch <branch-name> <repository-url>. This downloads the repository and checks out the specified branch.5. What is a shallow clone in Git?
A shallow clone downloads only the most recent commits instead of the full history. This can be done usinggit clone --depth 1 <repository-url> to speed up cloning of large repositories.Summary
The git clone command is used to download a remote repository to your local machine. It allows developers to copy projects from platforms such as GitHub or GitLab and begin working on the code locally.
In this guide, we covered the syntax of the git clone command, cloning repositories using HTTPS or SSH, cloning specific branches, performing shallow clones, and troubleshooting common cloning errors. Understanding these options helps improve efficiency when working with Git repositories in Linux environments. For complete workflows, see
Git workflow guide.


