Git by Example: Cloning Repositories
Learn how to copy an existing Git repository to your local machine using git clone. This example demonstrates how to download a project, including its entire history and branches, to start collaborating.
Code
# Clone a repository from a remote URL (HTTPS)
git clone https://github.com/example/project.git
# Output:
# Cloning into 'project'...
# remote: Enumerating objects: 100, done.
# remote: Counting objects: 100% (100/100), done.
# remote: Compressing objects: 100% (80/80), done.
# remote: Total 100 (delta 20), reused 100 (delta 20), pack-reused 0
# Receiving objects: 100% (100/100), 1.20 MiB | 2.40 MiB/s, done.
# Resolving deltas: 100% (20/20), done.
# Navigate into the cloned directory
cd project
# Check the remote configuration
git remote -v
# Output:
# origin https://github.com/example/project.git (fetch)
# origin https://github.com/example/project.git (push)Explanation
Cloning is the most common way to start working on an existing project. The git clone command creates a complete local copy of a remote repository, including all files, branches, and the entire commit history. This allows you to work offline and have the full context of the project on your machine.
When you clone a repository, Git automatically sets up a remote connection named "origin" pointing back to the source URL. This simplifies future interactions, allowing you to pull updates and push changes without repeatedly specifying the URL. You can clone using various protocols, with HTTPS and SSH being the most popular.
Unlike simply downloading a ZIP file of the code, cloning establishes a relationship with the upstream repository. This connection is vital for collaboration, enabling you to synchronize your work with other developers and contribute back to the project.
- Downloads the full project history and files
- Creates a directory named after the repository
- Sets up the "origin" remote automatically
- Checks out the default branch (usually main)

