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.
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.
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 historyThe 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.
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 mainTo ~/ask-docs.git * [new branch] main -> 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.
cd ~ && git clone "$HOME/ask-docs.git" ask-docs
cd ask-docs && ls
git log --onelineCloning into 'ask-docs'... done. prompt.txt settings.py 21e04fb Add a settings file 0bc2f92 Add the prompt
- The files are there.
git clonemade the folder and wrote out the files of the default branch, solsshows the two the commits added. - Both commits came down. The clone has the history, not a snapshot.
git logworks offline from the first second.
What the clone set up for you
git remote -v
git status -sborigin ~/ask-docs.git (fetch) origin ~/ask-docs.git (push) ## main...origin/main
originwas set for you. Nobody rangit remote add. That is whyoriginis the name everyone uses.- The branch is already paired.
## main...origin/mainmeansgit pushandgit pullwork 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.
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 --onelineask-docs notes-bot --- the same history in both 21e04fb Add a settings file 0bc2f92 Add the prompt 21e04fb Add a settings file 0bc2f92 Add the prompt
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.
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 -rorigin/experiment origin/main
The stand-in now has two branches, which git branch -r lists as origin/experiment and origin/main.
cd ~ && git clone -q --branch experiment "$HOME/ask-docs.git" try-it
git -C try-it status -sb## experiment...origin/experiment
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.
cd ~ && git clone -q --depth 1 "file://$HOME/ask-docs.git" latest-only
git -C latest-only log --oneline21e04fb Add a settings file
- 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.
cd ~ && git clone -q "$HOME/ask-docs.git" ask-docs && cd ask-docs
git branch* main
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
git branch -a* main remotes/origin/HEAD -> origin/main remotes/origin/experiment remotes/origin/main
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
git switch experiment
lsSwitched to a new branch 'experiment' branch 'experiment' set up to track 'origin/experiment'. prompt.txt settings.py
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 clone | git init then git remote add | |
|---|---|---|
| Starting point | a repository that already exists | an empty folder of yours |
| History | copied in full | starts empty |
origin | set for you | you add it |
| Branch pairing | set for you | set by your first push -u |
| Use it when | joining a project | putting a project of yours on GitHub |
Where cloning is the first thing you do
- Joining a team. Clone, install what
requirements.txtlists, 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.
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.Related
- Previous: Pushing and pulling
- Next: Pull requests
- Reference: git clone in the Git documentation
- Clone a public repository you like, then run
git log --oneline | wc -land see how many commits came with it. - Clone the same repository again with
--depth 1and compare the two counts, and the size of the two.gitfolders withdu -sh. - In a clone, run
git branch -aand switch to a branch you have never checked out.
Little by little, you're building something great.