Git/
Lesson

Cloning is like borrowing a book from a library, except in addition to the book, you are taking the entire history of its writing, all previous versions, and the author's notes. When you clone a Git repo, you download much more than files.

The basic clone

The simplest command to copy an existing repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory.:

# Clone a repository from GitHub
git clone https://github.com/user/my-project.git

# This creates a 'my-project' folder with all content
cd my-project

You now have a complete copy of the project on your computer, ready to be modified.

02

Clone into a custom folder

By default, Git creates a folder with the repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory. name. But you can choose a different name:

# Clone into a folder named 'client-work'
git clone https://github.com/user/my-project.git client-work

# Now it is in 'client-work/', not 'my-project/'
cd client-work

This is useful when working on multiple versions of the same project or when you want a more descriptive name.

03

The different URL protocols

Git supports several protocols to connect to remotes. Each has its advantages.

ProtocolURL formatBest for
HTTPShttps://github.com/user/repo.gitQuick setup, works behind firewalls
SSHgit@github.com:user/repo.gitNo password after setup, more secure
GitHub CLIgh repo clone user/repoModern alternative with extra features

HTTPSWhat is https?HTTP with encryption added, so data traveling between your browser and a server can't be read or tampered with by anyone in between. (the simplest)

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

Works everywhere, no configuration necessary. Since 2021, GitHub requires Personal Access Tokens instead of passwords, create one in your GitHub settings.

SSH (the most secure)

git clone git@github.com:user/repo.git

Requires a one-time setup:

# Generate an SSH key pair
ssh-keygen -t ed25519 -C "your-email@example.com"

# Copy the public key and add it in GitHub Settings > SSH Keys
cat ~/.ssh/id_ed25519.pub
AI pitfall
When you ask AI "how do I clone a repo?", it almost always gives you the HTTPS URL. That works fine for reading, but if you plan to push frequently, SSH saves you from entering credentials every time. Ask specifically for "SSH clone" if that is what you need.
04

What happens when you clone

When you run git clone, Git performs several steps:

  1. Creates a new folder with the repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory. name
  2. Initializes a Git repository (git init implicitly)
  3. Adds the remote origin pointing to the source URL
  4. Downloads all Git objects (commits, trees, blobs)
  5. Creates remote-tracking branches (origin/main, origin/develop, etc.)
  6. Checks out the default branch (usually main)
05

After the clone: first steps

# Enter the project folder
cd my-project

# Verify the configured remote
git remote -v
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

# See all branches (local and remote)
git branch -a
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
#   remotes/origin/feature-login

When you see remotes/origin/main, it is a local reference that represents the current state of main on the server. It updates when you fetch, but never automatically changes your local branch.

06

Shallow and partial clones

For huge repositories with years of history, you can clone selectively:

# Clone only the last 10 commits
git clone --depth 10 https://github.com/user/repo.git

# Clone only a specific branch
git clone --branch feature-login --single-branch https://github.com/user/repo.git
Warning
A shallow clone limits certain Git operations. You cannot see history beyond the depth you specified, and git blame on old code will not work.
07

Clone a forkWhat is fork?A personal copy of someone else's repository on a platform like GitHub, letting you make changes freely and propose them back via pull request. vs the original

When contributing to open source, you generally clone your fork, not the original:

# Wrong: clone the original project (you cannot push here)
git clone https://github.com/facebook/react.git

# Right: clone YOUR fork (you can push freely)
git clone https://github.com/YOUR-USERNAME/react.git

Your fork is under your control. You push your branches to it, then create pull requests to the original project.

08

Quick reference

CommandDescription
git clone urlStandard clone into a folder with the project name
git clone url folderClone into a specific folder
git clone --branch name --single-branch urlClone only one branch
git clone --depth N urlShallow clone (last N commits)
git clone --recursive urlClone with submodules