Course contents

Clone a repository

Sign in to run this lab and save your progress

Learn

Every Git module so far has assumed the repo already exists on your machine. In real life, most projects start the other way around: someone else has a repo on a server (GitHub, GitLab, your company's Git host), and you want a local copy to work on. That's what git clone is for.

What clone actually does

git clone /home/student/origin.git .

Git connects to the URL you give it, downloads the entire repository history (every commit, every branch, every tag), and sets up:

  • A working directory with the files from the default branch checked out, ready to edit.
  • A .git/ folder containing the whole history you just downloaded.
  • A remote called origin pointing back at where you cloned from. This is how Git knows where to push and pull from later.

Everything Git needs to work locally is in place. You can commit, branch, merge, do anything you've learned so far, all without touching the source repo. Sharing your work back to origin is a separate step (that's git push, next module).

About that trailing dot

Normally git clone <url> creates a new subdirectory named after the repo and puts everything inside it. Sometimes you're already sitting in the folder you want the repo to go into, and you want Git to clone INTO the current directory rather than create a new one below it. That's what the dot at the end means:

git clone /home/student/origin.git .

Reads as: "clone this repo into the current directory (.)". Git will happily do so as long as the current directory is empty.

URL vs filesystem path

git clone accepts anywhere Git can talk to a repo: https:// URLs, git@ SSH URLs, and plain filesystem paths. All four of these work if the source exists:

git clone https://github.com/user/project.git
git clone git@github.com:user/project.git
git clone /home/student/origin.git
git clone ../other-folder/some-repo

Filesystem clones are useful for lessons like this one, for local backups, and for setting up temporary sandbox repos.

The remote-tracking branches

After clone, run git branch -a. You'll see something like:

* main
  remotes/origin/main

main is your local branch. remotes/origin/main (usually displayed as origin/main) is a remote-tracking branch. It's your local memory of where main was on the remote when you last synced. Git updates this pointer when you fetch or pull, so you always know the difference between "what I have" and "what the remote had last time I checked."

If the source repo had other branches (say develop), they show up as origin/develop. To start working on one locally, switch to it and Git will create a local branch that tracks it:

git switch develop

Cloning a big repo

For huge projects (some are gigabytes with millions of commits), you can skip most of the history and just get the latest snapshot:

git clone --depth 1 <url>

This is called a shallow clone. Fast to download, tiny on disk. The trade-off: you don't have the older commits, so operations like git log further back or blaming a line to an old commit won't work. For read-only use of a large repo, shallow is often ideal.

Your task

A bare repository exists at /home/student/origin.git with one commit on main. Your working directory (/work) is empty. Clone the bare repo into your current directory so the files appear here and a remote called origin is configured automatically. After this, git status should show you on main with a clean working tree.