# Cloning a Repository

> How to download a complete copy of an existing remote repository to your local machine.

Subject: Version Control | Lesson 14 of 29 | 4 min read
Interactive lesson: https://codewithanimation.in/git/git-clone
Previous: [What is a Remote?](https://codewithanimation.in/git/what-is-a-remote.md) | Next: [Pushing Changes](https://codewithanimation.in/git/git-push.md)

## Cloning a Repository

`git clone` is a command used to copy an existing Git repository into a newly created directory.

It downloads the entire history of the project, creates remote-tracking branches for all branches, and creates and checks out an initial branch (usually `main` or `master`) based on the currently active branch of the cloned repository.

## Why it matters

For a web developer, `git clone` is the fundamental starting point when joining an existing project.

Whether you are contributing to an open-source library, starting a new job, or working on a team project, cloning gives you a complete local copy of the codebase and its history so you can run the code locally.

## How it works

First, it pulls down all commits, branches, and tags from the remote repository.

Second, it sets up references to the remote branches (e.g., `origin/main`), allowing you to fetch updates or push changes later. Finally, it extracts the files from the default branch into your working directory so you can immediately see and edit the code.

## Common mistake: Nested Repositories

The #1 common mistake is cloning a repository INTO an already initialized repository.

If you run `git clone` while inside a folder that already has a `.git` directory, you create a nested repository. The outer repository will see the new folder as a sub-project (similar to a submodule), and its contents will be ignored. This causes immense confusion for beginners when they try to track and commit their work.

## Tip: Automatic origin setup

`git clone` automatically sets up a remote connection for you and names it `origin`.

You do not need to manually configure the remote URL to start pushing or pulling changes. Git has already securely tethered your local copy to the remote cloud.

## Try it: Clone a repository locally

Copy a sample repository and inspect the local history and remote connection.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-clone

## Check yourself

### Question 1

What does `git clone` do by default?

- A. It only downloads the latest commit of the default branch.
- B. It initializes a new empty repository without downloading any files.
- C. It copies the files but does not track the history or branches.
- D. It downloads the entire repository history, sets up remote-tracking branches, and checks out the default branch.

Correct answer: D

`git clone` gets the complete repository data, sets up connections (remotes) to the original repo, and unpacks the current default branch for you to work on.

- Why not A: Git is distributed, so it downloads the entire history.
- Why not B: That is `git init`.
- Why not C: It copies both files and the `.git` folder.

### Question 2

What is the default name given to the remote repository created by `git clone`?

- A. `upstream`
- B. `main`
- C. `origin`
- D. `remote`

Correct answer: C

By default, `git clone` creates a remote connection named `origin` pointing to the URL you cloned from.

- Why not A: Upstream is often used manually for forks, but not the default clone name.
- Why not B: That is a branch name, not a remote name.
- Why not D: Git uses `origin` by default.

### Question 3

What is a common mistake related to cloning repositories?

- A. Cloning a repository using an SSH URL instead of HTTPS.
- B. Cloning without specifying a branch, which downloads nothing.
- C. Cloning a repository into a directory that is already an initialized Git repository.
- D. Cloning a repository and forgetting to type `git init` afterwards.

Correct answer: C

Cloning into an existing Git repository creates a nested Git repo. The outer repository will ignore the files in the inner repository by default, which can lead to lost work or confusion when trying to commit.

- Why not A: Both are perfectly valid ways to clone.
- Why not B: Git defaults to checking out the active remote branch.
- Why not D: `git clone` automatically initializes the repo for you.

## Remember this

- `git clone` downloads the entire repository history to your computer.
- It automatically creates a remote named `origin`.
- Never run `git clone` inside a folder that is already a Git repository.
