What Does git clone Do?

Introduction

When you see a GitHub, GitLab, or Bitbucket project and you want the full code on your laptop so you can run it, study it, or contribute—your first step is usually git clone.

So, what does git clone do?

git clone creates a local copy of an existing Git repository (usually from a remote URL) on your machine. It downloads the project’s history (commits, branches, tags), sets up the repo locally, and connects it back to the remote so you can fetch updates and push changes.

This article explains git clone from zero to professional-level usage, with practical commands, visuals you can add as screenshots, and common pitfalls.

What does git clone do?

git clone does three big things in one command:

  1. Downloads a repository from a remote source (or copies from a local path).
  2. Creates a new folder containing a complete Git repository.
  3. Configures a remote connection (typically named origin) and sets up local tracking for the default branch.

Think of it like this:

  • Download + Initialize Git + Connect to remote = git clone

It’s not just “copying files.” It also copies the entire Git history, so you can:

  • view past commits
  • compare changes
  • switch branches
  • create new branches
  • contribute through pull requests

The simplest git clone command

Here’s the most common form:

git clone <repository-url>
Code language: HTML, XML (xml)

Example:

git clone https://github.com/user/project.git
Code language: PHP (php)

Where to add a screenshot:

  • Screenshot: the green “Code” button on GitHub showing the clone URL
  • Screenshot: terminal showing the git clone ... command running

What exactly gets created on your computer?

After cloning, you’ll see a folder like this:

  • A project directory (usually the repo name)
  • Inside it, your normal project files
  • And a hidden folder called .git/ (this stores the full Git database)

Example flow:

git clone https://github.com/user/project.git
cd project
Code language: PHP (php)

Now you are inside a local Git repo.

What Git sets up automatically

After cloning, Git automatically configures:

  • Remote name: origin
  • Remote URL: the one you cloned
  • Default branch: checked out locally (like main or master)
  • Tracking: your local branch tracks the remote branch

You can verify this:

git remote -v

Expected output (example):

origin  https://github.com/user/project.git (fetch)
origin  https://github.com/user/project.git (push)
Code language: JavaScript (javascript)

Clone via HTTPS vs SSH

Most platforms provide two common clone URLs.

Option A: HTTPS

Example:

git clone https://github.com/user/project.git
Code language: PHP (php)

Pros:

  • simplest to start
  • works everywhere

Cons:

  • pushing may require authentication (token/password depending on platform)

Option B: SSH

Example:

git clone git@github.com:user/project.git
Code language: PHP (php)

Pros:

  • smooth authentication once SSH keys are configured
  • preferred for frequent contributors

Cons:

  • requires SSH key setup

Where to add a screenshot:

  • Screenshot: GitHub “Code” dropdown showing HTTPS and SSH options

Cloning a specific branch

Sometimes you don’t want the default branch. Use -b (or --branch).

git clone -b <branch-name> <repository-url>
Code language: HTML, XML (xml)

Example:

git clone -b develop https://github.com/user/project.git
Code language: PHP (php)

This checks out the develop branch immediately.

Shallow clones (faster downloads)

For huge repositories, downloading all history can be slow. Use a shallow clone.

git clone --depth 1 <repository-url>
Code language: HTML, XML (xml)

Example:

git clone --depth 1 https://github.com/user/project.git
Code language: PHP (php)

What it means

  • Downloads only the latest commit(s) instead of full history
  • Faster and smaller

When to use

  • CI/CD builds
  • quick review
  • learning a repo quickly

Limitation

  • You can’t access old commit history beyond the depth

Cloning only what you need

A) Clone only one branch

If the repo has many branches and you want only one:

git clone --single-branch -b main <repository-url>
Code language: HTML, XML (xml)

B) Sparse checkout (download fewer files)

If you only need a specific folder (advanced but very useful in monorepos):

  1. Clone without checkout:
git clone --no-checkout <repository-url>
cd <repo>
Code language: HTML, XML (xml)
  1. Enable sparse checkout:
git sparse-checkout init --cone
  1. Set folders you want:
git sparse-checkout set path/to/folder another/path
Code language: JavaScript (javascript)
  1. Checkout:
git checkout

Where to add a screenshot:

  • Screenshot: repo tree showing a large monorepo and the folder you actually need

Cloning into a custom folder name

By default, Git creates a folder with the repository name. You can override it:

git clone <repository-url> <folder-name>
Code language: HTML, XML (xml)

Example:

git clone https://github.com/user/project.git my-local-project
Code language: PHP (php)

This is useful when:

  • you want multiple copies for experiments
  • the repo name is too long

Re-cloning vs pulling: when to use which

A very common confusion is:

  • “Should I clone again?”
  • “Or should I pull?”

Use git clone when

  • you do not have the repository on your machine yet
  • you want a fresh copy
  • your existing copy is broken and you want to start clean

Use git pull when

  • you already cloned the repo
  • you want to update your local branch with remote changes

A simple rule:

  • First time: clone
  • Later updates: pull

Submodules and Git LFS notes

A) Submodules

Some repositories depend on other repos using submodules. If you clone normally, submodule folders may be empty until you initialize them.

Clone and get submodules in one step:

git clone --recurse-submodules <repository-url>
Code language: HTML, XML (xml)

Or after cloning:

git submodule update --init --recursive

B) Git LFS

If a project stores large files using Git LFS (Large File Storage), you may need Git LFS installed to pull actual binaries (models, videos, datasets).

Common errors and fixes

Error 1: “Repository not found”

Cause:

  • wrong URL
  • private repo without access

Fix:

  • confirm the URL
  • ensure you have permissions
  • try SSH if HTTPS access is limited

Error 2: “Permission denied (publickey)” (SSH)

Cause:

  • SSH key not set up or not added to Git hosting account

Fix:

  • generate SSH key
  • add public key to GitHub/GitLab
  • test connection

Error 3: “fatal: could not read Username/Password” (HTTPS)

Cause:

  • platform requires token-based authentication

Fix:

  • use personal access token (PAT)
  • configure credential manager

Error 4: Very slow clone

Fix options:

  • use shallow clone: --depth 1
  • clone only one branch: --single-branch
  • check network / proxy

Best practices for teams

  • Clone once, pull often. Don’t re-clone every day.
  • Prefer SSH for professional workflows (after setup).
  • Use shallow clones in CI/CD for speed.
  • Keep your local folder clean; avoid editing generated files without .gitignore.
  • After cloning, always check your branch:
git branch

And check remote tracking:

git branch -vv

Summary

So, what does git clone do? It creates a fully working local copy of a repository—files plus Git history—then connects it to the original remote so you can fetch updates and contribute changes.

If you learn only one Git command to start collaborating, make it git clone—because everything else (branches, commits, pull requests) becomes possible after you have the repo locally.

Leave a Reply

Your email address will not be published. Required fields are marked *