Cloning a Repository
Once you have a repository on GitHub (either your own or a Fork you made), the next step is to bring that code onto your physical computer. This process is called Cloning.
When you clone a repository, you aren't just downloading the files; you are downloading the entire project history and setting up a connection between your computer and the cloud.
What happens during a Clone?
Unlike a simple "Download ZIP," git clone does three things automatically:
- Creates a folder with the project name.
- Downloads every version of every file in the project's history.
- Sets up a Remote called
origin, which points back to GitHub so you can push and pull changes later.
How to Clone a Project
To clone a repository, you need its URL. You can find this on the main page of any GitHub repository.
Step 1: Get the URL
- Go to the repository on GitHub.
- Click the green
<>Code button. - Copy the HTTPS URL (it looks like
https://github.com/user/repo.git).
Step 2: Run the Command
Open your terminal (or Git Bash) and type git clone followed by the URL you copied:
git clone https://github.com/codeharborhub/tutorial.git
Step 3: Enter the Project
After the download finishes, you must move into the new folder before you can start working:
cd tutorial
Comparison: Clone vs. Download ZIP
| Feature | Git Clone | Download ZIP |
|---|---|---|
| Files | Yes | Yes |
| History | Yes (Every commit ever made) | No (Only current files) |
| Updates | Easy (git pull) | Must download whole ZIP again |
| Contribution | Ready to Push/PR | Not connected to Git |
Common Use Cases
| Scenario | What to do? |
|---|---|
| Joining a Team | Get the project URL from your lead and git clone it. |
| Switching PCs | git clone your own repo to your new laptop. |
| Learning Open Source | Find a cool repo, fork it, then clone your fork. |
Cloning with VS Code
If you prefer using a visual tool instead of the terminal, you can clone directly inside Visual Studio Code:
- Open VS Code.
- Press
Ctrl + Shift + P(orCmd + Shift + Pon Mac). - Type Git: Clone and press Enter.
- Paste the GitHub URL and choose a folder on your computer. VS Code will handle the cloning process for you!
Important Note for Beginners
Only clone once. A common mistake is running git clone every time you want to work on a project.
- Use
git clonethe first time you get the project. - Use
git pullevery time after that to get the latest updates.
By default, Git clones the project into a folder named after the repository. If you want to name the folder something else, just add the name at the end:
git clone https://github.com/user/repo.git my-custom-folder-name