Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Git Remotes  ›  Lesson

Cloning a Repository

Cloning Repositories 12 mingit clone copies a repository's full history into a new folder, records the source as the remote origin and checks out the default branch.

Cloning a Repository

Asha has put recipe-box on GitHub, and her brother Ravi wants to add his dal recipe. He does not need a zip file or a shared drive. One command gives him the whole project, with its complete history, ready to work in.

The rule

git clone copies the whole history and remembers where it came from as origin.

Every repository on a hosting service has a URL. Ravi passes it to git clone:

git clone https://github.com/asha-dev/recipe-box.git
cd recipe-box

That one command:

  1. Creates a folder named after the repository, recipe-box.
  2. Downloads every commit and every branch, not just the latest files.
  3. Records the source URL as a remote named origin.
  4. Checks out the default branch, usually main, and links it to the remote's copy, origin/main, so later pushes and pulls know where to go.

Want a different folder name? Add it at the end:

git clone https://github.com/asha-dev/recipe-box.git family-recipes

URLs come in two main forms:

https://github.com/asha-dev/recipe-box.git    # HTTPS
git@github.com:asha-dev/recipe-box.git        # SSH

HTTPS works anywhere and asks you to sign in when you push (GitHub expects a token or a credential helper, not your account password). SSH uses a key pair you set up once, after which pushes need no typing. Either gives you the same repository.

Cloning and git init are the two ways a repository begins on your machine. git init starts a new, empty history; git clone starts from an existing one. Never run git init inside a folder you just cloned: it already is a repository.

The shortcut

Ask "Does this project already exist somewhere?" If yes, clone it. If no, git init it. Then, after cloning, check two things: git remote -v shows origin, and git log --oneline shows history you did not write.

Ravi joins the recipe boxStep 1 of 4
  1. Copy the URL

    On the repository's GitHub page, Ravi opens the green Code button and copies the HTTPS URL.

The trap is downloading the ZIP file from the website instead. It contains the files but no .git folder: no history, no remote, and no way to send changes back.

Drills

Answer fast — trust the rule.

quick drill

What does git clone download?

quick drill

After cloning, what is the remote called by default?

quick drill

Which command clones recipe-box into a folder called family-recipes?

quick drill

Ravi downloads the repository as a ZIP instead of cloning. What is missing?

Remember

  • git clone copies the whole history and remembers where it came from as origin.
  • Existing project: clone it. New project: git init it.
  • A ZIP download is files without history; clone instead.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainClone, init or ZIP?

Ravi wants to contribute to Asha's recipe-box on GitHub. Explain why he should use git clone rather than git init or downloading a ZIP, and describe what he has on his laptop after cloning.