
Module 2 Lesson 2: Cloning existing repositories
Don't reinvent the wheel. Learn how to download a full copy of a project—including its entire history—from platforms like GitHub using 'git clone'.
Module 2 Lesson 2: Cloning existing repositories
While git init is for your new projects, git clone is for existing projects. Whether you are joining a new team or downloading a library from GitHub, you will use git clone more often than almost any other command.
1. What is git clone?
Cloning is the process of making a complete copy of a remote repository on your local machine.
It is NOT a "Download"
When you download a .zip from a website, you only get the current version of the files. When you clone a repository, you get:
- Every file in the project.
- Every branch ever created.
- Every commit ever made.
- Every tag ever used.
Because you get the full history, you can immediately start browsing the project's evolution without needing an internet connection.
2. How to Clone
To clone a repository, you need its URL. You can find this on GitHub by clicking the green "Code" button.
# Syntax: git clone <url>
git clone https://github.com/torvalds/linux.git
Specifying a folder name
By default, Git will create a folder named after the project (e.g., linux). If you want to name the folder something else, add it to the end:
git clone https://github.com/torvalds/linux.git my-linux-copy
graph LR
Remote["Remote Server (GitHub)"] -- "git clone" --> Local["Local Machine"]
subgraph "On Your Disk"
Files["Project Files"]
History[".git folder (Full History)"]
end
Local --- Files
Local --- History
3. HTTPS vs SSH
You will often see two URLs for a repository:
- HTTPS: Starts with
https://. This is the easiest to use but requires you to enter your username and a Personal Access Token (PAT) every time you push changes. - SSH: Starts with
git@. This is slightly more complex to set up (you need to generate SSH keys), but once configured, it allows you to interact with the repository securely without typing a password.
4. Useful Cloning Flags
git clone --depth 1 <url>: This is called a Shallow Clone. It only downloads the latest version of the files, skipping the history. This is great for large projects when you only want to see the code, not the history of the last 10 years.git clone --recursive <url>: This downloads the main repo and any submodules it depends on (we will cover submodules in Module 7).
Lesson Exercise
Goal: Clone your first open-source project.
- Go to GitHub.com and find a small repository (like a "Bootstrap Template" or a simple Python script).
- Copy the HTTPS URL.
- Open your terminal and run
git clone <URL>. - Navigate into the new folder and type
ls -a. - Verify that the
.gitfolder exists.
Observation: You didn't just get the code; you got the work history of the person who wrote it. You are now part of the distributed network.
Summary
In this lesson, we established:
git clonecreates a full mirror of a remote repository.- It is superior to a
.zipdownload because it includes the entire version history. - You can clone via HTTPS or SSH protocols.
- Shallow clones (
--depth 1) can save time and disk space for large projects.
Next Lesson: We’ll open up that mysterious .git folder and see what’s actually inside.