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

1. Introduction

In recent years, “Git” has become an essential tool in software development, research, and personal projects. Especially as a version control system used worldwide, Git plays a major role in team development and open-source projects by enabling efficient code sharing and management.

This article provides a beginner-friendly guide on how to clone (duplicate) a Git repository using the git clone command on Ubuntu, one of the most popular Linux distributions.
The git clone command is the most basic way to copy a remote repository (such as projects hosted on GitHub or GitLab) to your local environment.

By mastering git clone as an Ubuntu user, you’ll be able to smoothly install various open-source software and set up your own development environment.
It also helps you become more active in team and community collaboration.

This article covers everything from installing Git, basic usage of git clone, to troubleshooting common errors.
Whether you’re new to Git on Ubuntu or want to organize your existing knowledge, you’ll find practical tips here.

2. Prerequisites & Environment Setup

To use git clone on Ubuntu, you’ll first need to install Git itself. You’ll also need to configure initial settings and authentication for accessing remote repositories. This section walks you through each setup step.

2.1 How to Install Git

On Ubuntu, Git can be easily installed from the official repository. Open your terminal and run the following commands in order:

sudo apt update
sudo apt install git

After installation, check if Git is installed correctly by displaying its version information:

git --version

If the version information appears, Git is successfully installed.

2.2 Configuring Username and Email

Git saves “who did what and when” every time a file is changed. So, for first-time users, it’s important to set your username and email address:

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

This global setting will apply to all Git operations. If you want project-specific settings, omit --global and run the command in your project directory.

2.3 Choosing and Setting Up Authentication

There are two main authentication methods for accessing remote repositories with Git:

  • HTTPS
    Connects using a URL, just like your browser. GitHub and GitLab now require a Personal Access Token (PAT) instead of a password for security reasons.
  • SSH
    Uses public and private keys for authentication. It’s more secure and, once set up, lets you skip password entry each time—recommended if you use Git frequently.

2.4 Using Git Credential Manager (GCM, optional)

If you want safer and easier management of passwords and tokens, consider installing Git Credential Manager (GCM).
GCM securely stores your credentials and saves you from typing them every time. It’s available for Ubuntu via the official docs or package managers.

That’s it for preparing your Ubuntu environment to use git clone.
In the next section, you’ll learn the basic steps to actually clone a repository using the git clone command.

3. Basic Usage of git clone

The git clone command is the primary way to duplicate a remote repository’s contents into your local environment. When you want to use someone else’s repository for open-source projects or team development, cloning is the first step.

3.1 Basic Command Syntax

The simplest usage is:

git clone <repository URL>

For example, to clone a public repository on GitHub:

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

This creates a new folder called sample-project in your current directory, containing all the repository’s files.

3.2 Cloning via HTTPS

Most services support cloning via HTTPS. The URL starts with “https://”. You may be prompted for authentication (username, Personal Access Token, etc.) the first time.

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

3.3 Cloning via SSH

If you’ve registered an SSH key, you can clone more securely and without passwords. The SSH URL format looks like “git@github.com:username/repository.git”.

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

Register your public key with GitHub or GitLab before using SSH cloning.

3.4 Repository Structure After Cloning

When you run git clone, all files and directories—including the .git directory (which stores version control data)—are duplicated.

  • .git directory
    Stores the repository’s history and settings. Its presence means all files in the folder are under Git management.
  • Working directory
    Contains your project’s source code, documentation, and other files you work with.

3.5 Be Careful with the Current Directory

git clone creates a new folder in the directory where you run it. Be sure to cd into your preferred location first.

4. Cloning a Specific Branch

Git repositories may have multiple branches. By default, git clone checks out the default branch (like main or master), but you may want to clone a specific branch. Here’s how:

4.1 Using the –branch Option

The git clone command provides the --branch (or -b) option to clone a specified branch.

git clone --branch branch-name repository-URL

To clone the “develop” branch, for example:

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

This creates a local repository checked out to the “develop” branch.

4.2 Combining with –single-branch

By default, git clone downloads all branch data. Add --single-branch to fetch only the specified branch, saving disk space and bandwidth.

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

This is handy for large repositories or when you only need one feature branch.

4.3 Fetching Other Branches After Cloning

If you want to use other branches later, run these commands inside your repository:

git fetch --all

Then switch to any branch with:

git checkout branch-name

4.4 Tips & Precautions

  • For private or restricted repositories, you’ll need valid authentication credentials.
  • If you specify a non-existent branch, you’ll get an error—always double-check branch names on the remote repository.

Take advantage of git clone’s flexible options for efficient workflow, even when cloning specific branches.

5. Common Errors and How to Fix Them

When using git clone, you might encounter errors related to authentication, connectivity, or permissions. Here are common Ubuntu-specific issues and solutions:

5.1 HTTPS Authentication Error & Personal Access Token (PAT)

Due to security updates, GitHub and GitLab now require Personal Access Tokens (PAT) instead of passwords. You may see errors like:

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

Generate a PAT in GitHub under Settings > Developer settings > Personal access tokens. Enter your GitHub username and paste the PAT when prompted for a password.

5.2 SSH Authentication Error and Registering Public Keys

If you see “Permission denied (publickey)” while cloning via SSH, your SSH key may not be registered.

Solution:

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

This should resolve SSH authentication errors.

5.3 Repository Access Permission Error

If you try cloning a private or organization repository and see:

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

Solution:

  • Check the repository URL for typos
  • Ensure your account has permission to access the repository
  • Re-enter authentication credentials

5.4 Network-Related Errors

“Connection timed out” or “Could not resolve host” usually point to network issues.

Solution:

  • Check your internet connection
  • If using a VPN or proxy, review your settings
  • If GitHub is temporarily down, wait and try again later

5.5 Other Common Errors

  • Directory already exists
    You can’t clone into a folder with the same name. Rename the target directory or remove the existing folder.
  • Not enough disk space
    Make sure you have enough free space before cloning large repositories.

By troubleshooting each issue step-by-step, you’ll always be able to find a solution.

6. Useful git clone Options

git clone comes with various options to help you fetch only the data you need, handle submodules, and more. Here are some of the most useful options:

6.1 –depth Option (Shallow Clone)

If you want only the latest commit history (not the full history), use the --depth option.
This downloads just the number of commits you specify:

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

This fetches only the latest commit—perfect for CI or when you need speed.

6.2 –single-branch Option

By default, git clone fetches all branch info. Add --single-branch to clone only the branch you specify.
Commonly used together with --branch:

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

Use this when you only need one branch.

6.3 –recursive Option (Clone Submodules)

Some Git repositories use “submodules” (other repositories included inside them). Use --recursive to clone these automatically:

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

If you forget --recursive, run this after cloning:

git submodule update --init --recursive

 

6.4 Other Handy Options

  • –origin Option
    Set a remote name other than the default (origin).
  • Specify Directory Name
    Add a directory name at the end of the command to change where the repository is cloned.
  git clone https://github.com/exampleuser/sample-project.git your-directory-name

Mix and match these options to make git clone even more efficient and flexible.

7. Using GUI Tools for git clone

If you prefer not to use the command line or want a more visual Git experience, try a GUI (Graphical User Interface) tool. Ubuntu supports several tools to make cloning repositories easy.

7.1 Recommended GUI Tools

  • Gittyup
    A lightweight, simple Git client available from Ubuntu’s official repo or via Flatpak.
  • Visual Studio Code (VS Code)
    A popular code editor with powerful Git extensions for intuitive Git operations.
  • GitKraken
    A visually appealing and feature-rich client (free for personal use).

7.2 How to Clone with Gittyup

  1. Install Gittyup (e.g., sudo apt install gittyup or from Flatpak).
  2. Open the app and click the “Clone Repository” button.
  3. Paste the repository URL, select a destination folder, and the repository will be downloaded.

7.3 How to Clone with Visual Studio Code

  1. Open VS Code and click the “Source Control” icon on the left.
  2. Select the “Clone Repository” button at the top.
  3. Paste the repository URL and press Enter. Select a save location to start cloning.
  4. Once cloned, you can edit code, commit, and push changes—all within VS Code.

7.4 Benefits and Caveats of GUI Tools

  • Benefits
    No need to type commands; easy for beginners. You can also visualize file changes and commit history at a glance.
  • Points to note
    Features and interfaces vary by tool, so choose one that fits your needs and preferences. Start with a GUI and gradually learn the command line for more flexible workflows.

Using GUI tools makes git clone and other Git operations much more accessible on Ubuntu. Combining GUI and command line as needed will boost your productivity.

8. Conclusion

This article covered everything you need to know about mastering git clone on Ubuntu—from installation, HTTPS/SSH cloning, branches, handy options, to GUI tools—all in one place.
Once you learn git clone, it’s useful for open-source contributions, team development, and self-study. Mastering it early makes your workflow smoother in many scenarios.

Especially for beginners, remember: “Google the error message or check the official docs if you get stuck.” As you get comfortable, try handy options and GUI tools to level up your Git skills.

If you have further questions or issues not solved here, check the FAQ, GitHub docs, or community forums.
Keep exploring Ubuntu and Git to expand your development and learning opportunities.

9. FAQ (Frequently Asked Questions)

Q1: How do I install Git on Ubuntu?
A1: Open your terminal and run these commands:

sudo apt update
sudo apt install git

After installing, verify with git --version.

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

git clone --branch branch-name repository-URL

Combine with --single-branch if you only want that branch.

Q3: I get an authentication error when cloning over HTTPS. What should I do?
A3: GitHub and GitLab now require a Personal Access Token (PAT) instead of a password. Create a PAT in your GitHub settings and paste it when prompted for a password.

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

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

Copy the public key (~/.ssh/id_ed25519.pub) and add it to your GitHub/GitLab account settings.

Q5: How can I check the remote URL of a cloned repository?
A5: In your cloned repository directory, run:

git remote -v

This displays the remote repository URLs.

Q6: What happens if a directory with the same name already exists?
A6: git clone will error out. Use a different directory name or delete the existing folder before cloning again.

Q7: How do I properly clone a repository with submodules?
A7: Add --recursive when cloning:

git clone --recursive repository-URL

If you’ve already cloned, run:

git submodule update --init --recursive

to fetch submodules.

Q8: What are some recommended Git GUI tools for Ubuntu?
A8: Popular options include Gittyup, Visual Studio Code (with Git extensions), and GitKraken. Choose what fits your workflow.

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

git fetch --all
git checkout branch-name

Use these commands to switch branches as needed.

年収訴求