← Back to Git Course | Chapter 2: Basic Commands | Lesson 4 of 18

Git clone Command

git clone is like photocopying an entire book, including all its old edits, so you have your own full copy to read and change at home.
Syntax
bash
git clone repository_url [directory_name]

What is git clone?

git clone <url> downloads a complete copy of a remote repository -- every commit, every branch, the full history -- onto your machine in one step, giving you a fully independent local repository ready to work in immediately.

Example: What is git clone?

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

Cloning from a Remote URL

Cloning works over HTTPS or SSH URLs, and either way Git automatically creates a remote connection named origin pointing back to that source, so future push and pull commands know exactly where to sync.

Example: Cloning from a Remote URL

bash
git clone https://github.com/user/repo.git
git remote -v

Cloning into a Specific Folder

By default the cloned folder is named after the repository itself, but git clone <url> <folder-name> lets you clone into a custom directory name -- useful when you already have a folder with that name, or want something more descriptive.

Example: Cloning into a Specific Folder

bash
git clone https://github.com/user/repo.git my-folder

Cloning a Specific Branch

Cloning normally checks out only the default branch, but git clone --branch <name> <url> lets you clone and check out a specific branch directly, skipping the extra step of switching branches after the fact.

Example: Cloning a Specific Branch

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

Checking the Cloned Remote

Running git remote -v right after a clone shows you the origin URL Git configured automatically -- a quick sanity check that you're pointed at the repository you actually intended to clone.

Example: Checking the Cloned Remote

bash
git remote -v
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Running git clone and then git init inside the new folder, when the clone is already a repository.
  2. Forgetting to cd into the cloned folder before running Git commands, so Git says not a git repository.
  3. Expecting git clone to keep the copy up to date, when you need git pull to fetch later changes.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.