Git Clone Command in Linux (Examples for Ubuntu, SSH, Branch & Depth)

Git Clone Command in Linux (Examples for Ubuntu, SSH, Branch & Depth)

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

DescriptionCommand
Clone a repositorygit clone <repository-url>
Clone repository into a custom directorygit clone <repository-url> project-name
Clone repository using SSHgit clone git@github.com:user/repository.git
Clone specific branchgit clone --branch <branch-name> <repository-url>
Clone only latest commit (shallow clone)git clone --depth 1 <repository-url>
Clone single branch onlygit clone --single-branch -b <branch-name> <repository-url>
Clone repository without checking out filesgit clone --no-checkout <repository-url>
Clone repository into current directorygit 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:

bash
git clone <repository-url>

Example:

bash
git clone https://github.com/user/project.git

This 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:

OptionDescription
--branch <branch>Clone a specific branch
--depth <number>Perform a shallow clone with limited history
--single-branchClone only one branch instead of all branches
--recurse-submodulesClone repository including submodules
--quietSuppress progress output

Example:

bash
git clone --branch develop https://github.com/user/project.git

This 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:

bash
git clone https://github.com/golinuxcloud/sample-project.git

Output example:

text
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:

bash
git clone https://github.com/user/repository.git

This 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:

bash
git clone git@github.com:user/repository.git

SSH 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:

bash
git clone https://github.com/golinuxcloud/git-demo.git

Example cloning from GitLab:

bash
git clone https://gitlab.com/golinuxcloud/git-demo.git

Both commands download the repository to your local system.

Verify cloned repository contents

After cloning, navigate to the repository directory and list the files:

text
cd git-demo
ls -l

You should also see a hidden .git directory which contains the Git metadata.

bash
ls -a

Example output:

text
.git
README.md
src

Clone 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:

bash
git clone https://github.com/user/project.git myproject

This will create a directory named myproject instead of project.


Clone repo into existing directory

You can also clone the repository into the current directory:

bash
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:

bash
git clone --branch develop https://github.com/user/project.git

This 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:

bash
ssh-keygen -t rsa -b 4096

This generates a public key and private key in the .ssh directory.

Example location:

text
~/.ssh/id_rsa
~/.ssh/id_rsa.pub

Add SSH key to GitHub or GitLab

Copy the contents of the public key:

bash
cat ~/.ssh/id_rsa.pub

Then 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:

bash
git clone git@github.com:user/private-repo.git

Git 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.

bash
git clone --branch <branch-name> <repository-url>

Example:

bash
git clone --branch develop https://github.com/user/project.git

This 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.

bash
git clone --depth 1 <repository-url>

Example:

bash
git clone --depth 1 https://github.com/user/project.git

This 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.

bash
git clone --depth 50 https://github.com/user/project.git

This 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.

bash
git clone --depth 1 https://github.com/user/project.git

This 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.

bash
git clone --single-branch --branch main https://github.com/user/project.git

This 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.

bash
git clone --no-checkout https://github.com/user/project.git

After cloning, you can manually checkout the files when needed using git checkout:

bash
git checkout

Fix 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:

text
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:

text
Permission denied (publickey)

It usually means your SSH key is not configured correctly.

Check whether your SSH key exists:

bash
ls ~/.ssh

If needed, generate a new SSH key:

bash
ssh-keygen -t rsa -b 4096

Then 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:

text
fatal: repository 'https://github.com/user/repository.git' not found

Possible 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:

text
fatal: unable to access 'https://github.com/user/project.git/': Failed to connect

Possible 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 command git 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 as git 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 using git 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 using git 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.


Official Documentation

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.