How to Use git clone on Ubuntu: A Complete Beginner’s Guide to Cloning Git Repositories

1. Introduction

In recent years, Git has become an indispensable tool in software development, research, and personal projects. As a version control system used worldwide, Git plays a vital role in code sharing and management for team development and open-source projects.

This article explains, in a beginner-friendly manner, how to duplicate (clone) a Git repository on Ubuntu, a popular Linux distribution, using the git clone command.
git clone is one of the most fundamental commands used to copy an entire remote repository—such as projects hosted on GitHub or GitLab—into your local environment.

By mastering git clone, Ubuntu users can smoothly install various open-source software projects and build their own development environments.
In addition, participation in teams and communities will become much more active.

This article provides a comprehensive overview, covering Git installation, basic usage of git clone, and how to handle common errors.
It is useful not only for those who are new to Git on Ubuntu, but also for users who want to organize and reinforce their existing knowledge.

2. Prerequisites and Environment Setup

To use git clone on Ubuntu, Git itself must be installed. In addition, initial configuration and authentication preparation for accessing remote repositories are important. This section explains the setup process step by step.

2.1 Installing Git

On Ubuntu, Git can be easily installed from the official repositories. Open a terminal and execute the following commands in order.

sudo apt update
sudo apt install git

After installation, verify that Git was installed correctly by displaying the version information.

git --version

If the version information is displayed, the installation was successful.

2.2 Setting Your Username and Email Address

Git records who made changes, when they were made, and what was changed. Therefore, when using Git for the first time, you should configure your username and email address.

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

This configuration only needs to be done once and will apply to all Git operations. If you want to set different values per project, omit --global and run the command inside the project directory.

2.3 Choosing and Preparing an Authentication Method

When accessing remote repositories with Git, there are two primary authentication methods: HTTPS and SSH.

  • HTTPS
    Access is performed using a URL, similar to a web browser. On platforms such as GitHub and GitLab, password authentication has been deprecated, and the use of Personal Access Tokens (PATs) is recommended.
  • SSH
    Authentication is performed using a public and private key pair. This method is highly secure and eliminates the need to enter a password each time, making it ideal for users who frequently use Git.

2.4 Using Git Credential Manager (Optional)

If you want to manage passwords or access tokens more securely and conveniently, consider installing Git Credential Manager (GCM).
GCM securely stores authentication information and eliminates the need for repeated input. On Ubuntu, it can be installed via official documentation or package sources.

This completes the preparation required to use git clone on Ubuntu.
In the next section, we will cover the basic steps for actually cloning a repository using the git clone command.

3. Basic Usage of git clone

The git clone command is a fundamental command used to copy the contents of a remote repository entirely into your local environment. When you want to use a repository created by someone else in an open-source project or team development, the first operation you perform is usually git clone.

3.1 Basic Command Syntax

The simplest usage is shown below.

git clone <repository URL>

For example, when cloning a public repository hosted on GitHub:

git clone https://github.com/exampleuser/sample-project.git

When this command is executed, a new folder named sample-project is created in the current directory, and all repository contents are downloaded into it.

3.2 Cloning via HTTPS

HTTPS cloning is commonly used across many services. The URL starts with https://. You may be prompted to enter authentication information (username and Personal Access Token) during the first operation.

git clone https://github.com/username/repository-name.git

3.3 Cloning via SSH

If you have an SSH key registered, you can clone securely without entering a password. SSH URLs typically use the git@github.com: format.

git clone git@github.com:username/repository-name.git

To use SSH cloning, you must register your public key with GitHub, GitLab, or another hosting service in advance.

3.4 Repository Structure After Cloning

When you run git clone, all files and directories in the repository are copied, along with the .git directory that contains version control information.

  • .git Directory
    This directory stores the repository’s history and configuration. Its presence means that all files in the folder are managed by Git.
  • Working Directory
    This contains the project’s source code and documentation that you interact with during development.

3.5 Be Careful with the Current Directory

The git clone command creates a new directory in the location where it is executed. It is recommended to move to the desired destination directory using the cd command before running it.

4. Cloning a Specific Branch

Git repositories often contain multiple branches. By default, git clone clones the default branch (such as main or master), but there are many cases where you want to clone only a specific branch. This section explains how to do that.

4.1 Using the –branch Option

The git clone command provides the --branch (or -b) option. This allows you to clone a repository with a specified branch checked out.

git clone --branch branch-name repository-URL

For example, to clone the develop branch:

git clone --branch develop https://github.com/exampleuser/sample-project.git

This creates a local repository with the develop branch checked out.

4.2 Combining with the –single-branch Option

By default, git clone downloads information for all branches. However, by combining it with the --single-branch option, only the specified branch is retrieved, reducing disk usage and network traffic.

git clone --branch branch-name --single-branch repository-URL

This is useful when you only need a specific feature branch or when working with large repositories.

4.3 Fetching Other Branches After Cloning

If you later decide that you need additional branches, run the following command inside the repository.

git fetch --all

Then switch to the desired branch with:

git checkout branch-name

4.4 Important Notes

  • Authentication is required for private or restricted repositories.
  • Specifying a non-existent branch name will result in an error. It is recommended to check available branch names on the remote repository beforehand.

5. Common Errors and How to Fix Them

When using git clone, errors may occur due to authentication, connectivity, or permission issues. This section introduces common errors encountered by Ubuntu users and their solutions.

5.1 HTTPS Authentication Errors and Using Personal Access Tokens

To improve security, platforms such as GitHub and GitLab have discontinued password authentication and now require Personal Access Tokens (PATs).
You may encounter errors like the following:

remote: Support for password authentication was removed...
fatal: Authentication failed for 'https://github.com/...'

In this case, generate a PAT on GitHub and enter your GitHub account name as the username and the PAT as the password. PATs can be created from GitHub’s Settings → Developer settings → Personal access tokens.

5.2 SSH Authentication Errors and Registering Public Keys

When cloning via SSH, you may see the error Permission denied (publickey). This usually indicates that the SSH key has not been properly registered.

Solution:

  1. Generate an SSH key if you do not already have one:
ssh-keygen -t ed25519 -C "your.email@example.com"
  1. Copy the public key:
cat ~/.ssh/id_ed25519.pub
  1. Add the public key to your GitHub or GitLab account settings.

This resolves SSH authentication errors.

5.3 Repository Access Permission Errors

When attempting to clone a private or organization-owned repository, you may see errors like the following:

fatal: repository 'https://github.com/username/repository-name.git/' not found

Solution:

  • Verify that the repository URL is correct.
  • Confirm that your account has permission to access the repository.
  • Re-enter authentication credentials if necessary.

5.4 Network-Related Errors

Errors such as Connection timed out or Could not resolve host are often caused by internet connectivity or proxy settings.

Solution:

  • Ensure that your internet connection is active.
  • Review VPN or proxy settings if applicable.
  • If the issue is caused by a temporary service outage, wait and retry later.

5.5 Other Common Errors

  • Directory Already Exists
    Cloning fails if a directory with the same name already exists. Rename the directory or delete the existing one.
  • Insufficient Disk Space
    Ensure that there is enough free disk space available.

By identifying the cause and addressing it step by step, most errors can be resolved successfully.

6. Useful git clone Options

The git clone command provides a variety of options that allow you to retrieve only the data you need or properly clone repositories with submodules. This section introduces some of the most commonly used options.

6.1 –depth Option (Shallow Clone)

If you do not need the full commit history and only want the latest commits, the --depth option is useful.
It allows you to limit the number of commits retrieved.

git clone --depth 1 https://github.com/exampleuser/sample-project.git

This example retrieves only the most recent commit. It is ideal for large repositories or CI environments where speed is important.

6.2 –single-branch Option

By default, git clone retrieves information for all branches. Using the --single-branch option allows you to clone only a specific branch.
It is often combined with the --branch option.

git clone --branch develop --single-branch https://github.com/exampleuser/sample-project.git

6.3 –recursive Option (Cloning Submodules)

Git repositories can include other repositories as submodules. When cloning such projects, use the --recursive option.

git clone --recursive https://github.com/exampleuser/sample-project.git

This clones both the main repository and its submodules. If you forgot to include --recursive, you can initialize submodules afterward:

git submodule update --init --recursive

6.4 Other Useful Options

  • –origin Option
    Used when you want to assign a remote name other than the default origin.
  • Specifying a Directory Name
    To explicitly specify the destination directory name, add it at the end of the command.
git clone https://github.com/exampleuser/sample-project.git custom-directory-name

7. Using git clone with GUI Tools

If you are not comfortable with command-line operations or prefer a more intuitive interface, GUI tools are a great alternative. On Ubuntu, several GUI tools allow you to easily perform git clone.

7.1 Popular GUI Tools

  • Gittyup
    A lightweight and simple Git client, installable via Ubuntu repositories or Flatpak.
  • Visual Studio Code (VS Code)
    A popular editor that supports intuitive Git operations through extensions.
  • GitKraken
    A feature-rich Git client with a visually intuitive interface (free for personal use).

7.2 Cloning with Gittyup

  1. Install Gittyup (e.g., sudo apt install gittyup or via Flatpak).
  2. Launch the application and click “Clone Repository.”
  3. Enter the repository URL, choose a destination folder, and execute the clone.

7.3 Cloning with Visual Studio Code

  1. Launch VS Code and click the “Source Control” icon on the left.
  2. Select “Clone Repository” from the top menu.
  3. Paste the repository URL, press Enter, and choose a destination folder.
  4. After cloning, you can immediately edit code, commit changes, and push updates within VS Code.

7.4 Advantages and Considerations of GUI Tools

  • Advantages
    No command input is required, making them beginner-friendly. Differences and history are easy to visualize.
  • Considerations
    Features and interfaces vary by tool, so choose one that fits your workflow. Starting with GUI tools and gradually learning the command line provides greater flexibility.

8. Summary

This article provided a detailed explanation of how to use git clone on Ubuntu, from basic concepts to advanced usage.
It covered Git installation, HTTPS and SSH cloning methods, working with specific branches, useful options, and GUI-based workflows.

git clone is an essential command for open-source projects, team development, and self-learning. Once you master it, you will be able to work efficiently in many scenarios.

For beginners, a good approach is to search error messages directly, consult official documentation, and gradually explore advanced options and GUI tools as you gain experience.

If you encounter questions not covered in this article, refer to FAQs, official GitHub documentation, or community resources.
Leverage the combination of Ubuntu and Git to further expand your development and learning opportunities.

9. FAQ (Frequently Asked Questions)

Q1: How do I install Git on Ubuntu?
A1: Run the following commands in the terminal:

sudo apt update
sudo apt install git

Q2: How can I clone only a specific branch?
A2: Use the --branch (or -b) option.
Example:

git clone --branch branch-name repository-URL

Q3: I get an authentication error when cloning via HTTPS. What should I do?
A3: Use a Personal Access Token instead of a password. Generate one in your GitHub settings and paste it into the password prompt.

Q4: How do I create and register an SSH key?
A4: Generate an SSH key using:

ssh-keygen -t ed25519 -C "your.email@example.com"

Q5: How can I check the remote URL of a cloned repository?
A5: Run the following command inside the repository:

git remote -v

Q6: What happens if a directory with the same name already exists?
A6: git clone will fail. Specify a different directory name or delete the existing directory.

Q7: How do I correctly clone a repository with submodules?
A7: Use the --recursive option:

git clone --recursive repository-URL

Q8: What Git GUI tools are recommended for Ubuntu?
A8: Gittyup, Visual Studio Code (with Git extensions), and GitKraken are popular choices.

Q9: How do I switch to another branch after cloning?
A9:

git fetch --all
git checkout branch-name

侍エンジニア塾