Git and GitHubgit 2.54 · GitHub · macOS, Linux or Windows
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
26 small wins to finish your pathNext lesson →

Cloning a repository

git clone copies a whole repository, its files and its entire history, into a new folder on your machine. It is how you start on a project that already exists.

Last updated: 12 Oct, 2026 · git 2.54

Pushing and pulling moved commits between two copies that already existed. Cloning is where the second copy comes from.

Cloning a repository you already have on GitHub · from the Git and GitHub series · 16:57 to 18:16

The video makes an empty folder, runs git clone with the repository's URL and finds the project inside. The clone arrives with the files and with every commit that was ever made to the branch.

Syntax: the forms of git clone worth knowing.

bash
git clone <URL>                  # into a new folder named after the repository
git clone <URL> my-folder        # into a folder you name
git clone --branch dev <URL>     # start on the dev branch instead of the default one
git clone --depth 1 <URL>        # the latest commit only, not the whole history

The repository you clone has to exist first, so the block below builds one: a bare repository in a folder on this machine, standing in for the GitHub URL so the commands are real runs, with two commits pushed into it by somebody else. On GitHub the only difference is the URL.

ExampleThe repository that already exists, with two commits in it
export GIT_AUTHOR_NAME=Reader GIT_AUTHOR_EMAIL=reader@example.com GIT_AUTHOR_DATE=2026-10-12T10:00:00
export GIT_COMMITTER_NAME=Reader GIT_COMMITTER_EMAIL=reader@example.com GIT_COMMITTER_DATE=2026-10-12T10:00:00
git init --bare -q "$HOME/ask-docs.git"               # the stand-in for the GitHub repository
mkdir seed && cd seed && git init -q -b main          # someone fills it in
printf 'Answer only from the notes.\n' > prompt.txt
git add . && git commit -q -m "Add the prompt"
printf 'chunk_size = 400\n' > settings.py
git add . && git commit -q -m "Add a settings file"
git remote add origin "$HOME/ask-docs.git" && git push origin main

The push is the last line, and its output is the stand-in gaining a branch called main. Everything from here on is a reader arriving at that repository with nothing on their own machine.

What a clone arrives with

One command does four things: it makes the folder, copies the history into .git, writes the files of the default branch into the folder, and adds a remote called origin pointing at where it came from.

ExampleOne command, and the project is on your machine
cd ~ && git clone "$HOME/ask-docs.git" ask-docs
cd ask-docs && ls
git log --oneline
  • The files are there. git clone made the folder and wrote out the files of the default branch, so ls shows the two the commits added.
  • Both commits came down. The clone has the history, not a snapshot. git log works offline from the first second.

What the clone set up for you

ExampleThe remote and the pairing a clone writes
git remote -v
git status -sb
  • origin was set for you. Nobody ran git remote add. That is why origin is the name everyone uses.
  • The branch is already paired. ## main...origin/main means git push and git pull work with no arguments.

Choosing the folder name

Without a second argument the folder is named after the repository. Pass a name and git uses that instead. The name of the folder has no effect on anything inside it.

ExampleThe folder name is yours; the repository inside is the same
cd ~ && git clone -q "$HOME/ask-docs.git"          # no folder name given
git clone -q "$HOME/ask-docs.git" notes-bot        # a folder name of your choosing
ls -d ask-docs notes-bot
echo "--- the same history in both"
git -C ask-docs log --oneline
git -C notes-bot log --oneline

Starting on another branch

A clone writes out the default branch. --branch checks out another one instead, which needs a second branch to exist on the remote, so the block below pushes one.

ExampleA second branch, pushed to the stand-in
git switch -q -c experiment
printf 'chunk_size = 200\n' > settings.py
git add . && git commit -q -m "Try smaller chunks"
git push -q origin experiment
git branch -r

The stand-in now has two branches, which git branch -r lists as origin/experiment and origin/main.

ExampleA clone that starts on the branch you name
cd ~ && git clone -q --branch experiment "$HOME/ask-docs.git" try-it
git -C try-it status -sb

The clone starts on experiment. No git switch was needed, and status shows the branch and its pairing straight away.

Taking less history

--depth 1 asks for the latest commit only, which is useful for a huge repository you want to read or build once. A shallow clone cannot show you old history, and some operations refuse to work in one.

ExampleA clone that takes one commit
cd ~ && git clone -q --depth 1 "file://$HOME/ask-docs.git" latest-only
git -C latest-only log --oneline
  • The shallow clone has one commit. The older one was never downloaded. For a repository with years of history that is the difference between seconds and minutes.
  • The file:// prefix is there for a reason. Copying from a folder on the same machine ignores --depth; the prefix makes git use the same path it uses for a network URL.

Getting at a branch that only exists on the remote

A clone writes out one branch, but it downloaded all of them. git branch -a lists what came down, and git switch with the branch name creates a local branch paired with the remote one.

ExampleThe branches a fresh clone gives you locally
cd ~ && git clone -q "$HOME/ask-docs.git" ask-docs && cd ask-docs
git branch

One branch, although the stand-in has two. The other one came down with the clone and has no local name yet.

Listing the branches that came down

ExampleLocal branches and remote ones together
git branch -a

git branch showed one branch and git branch -a shows three more lines, each starting with remotes/origin/. Those are not branches you are working on; they are what the remote had when you cloned.

Switching to one of them

ExampleA branch that was downloaded but not checked out
git switch experiment
ls

When you ask to switch to a name that exists on exactly one remote, git creates the local branch and pairs it with that remote branch, which is what the two lines after the switch say. The files from that branch are then in the folder.

Cloning against init plus remote

git clonegit init then git remote add
Starting pointa repository that already existsan empty folder of yours
Historycopied in fullstarts empty
originset for youyou add it
Branch pairingset for youset by your first push -u
Use it whenjoining a projectputting a project of yours on GitHub

Where cloning is the first thing you do

  • Joining a team. Clone, install what requirements.txt lists, and you are at the same commit as everyone else.
  • Reading someone's code properly. A clone lets you search the whole project and read its history, which a web page does not.
  • Moving to a new machine. Clone your own repository and the project is back, with every commit.
Watch out. Do not clone inside a folder that is already a repository. Git will not stop you, and you end up with a repository inside a repository, where the outer git status sees a folder it cannot explain. Run git status before cloning: if it answers instead of saying it is not a repository, move somewhere else first.
Try it yourself
  • Clone a public repository you like, then run git log --oneline | wc -l and see how many commits came with it.
  • Clone the same repository again with --depth 1 and compare the two counts, and the size of the two .git folders with du -sh.
  • In a clone, run git branch -a and switch to a branch you have never checked out.

Little by little, you're building something great.