Cloning a Repository
git clone downloads a complete copy of an existing repository — every file, every commit, every branch — and sets up the remote connection for you. It's how you start working on any project that already exists, whether your team's or an open-source one.
Cloning
Copy the URL from the repository's page on GitHub (the green Code button) and pass it to git clone:
git clone https://github.com/sam-rivera/recipes.git
Cloning into 'recipes'... remote: Enumerating objects: 9, done. remote: Counting objects: 100% (9/9), done. remote: Compressing objects: 100% (5/5), done. remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (9/9), done.
Git creates a folder named after the repository, downloads everything into it, and checks out the default branch. Move into it and you're ready to work:
cd recipes git log --oneline
13992c7 (HEAD -> main, origin/main, origin/HEAD) Add omelette recipe 6b13d64 Add pancake recipe
What clone sets up
- A remote called
originpointing at the URL you cloned from. - A local
mainbranch withorigin/mainas its upstream, sogit pullandgit pushwork straight away. - Remote-tracking branches for every branch on the server —
origin/main,origin/desserts— which record what the server had when you last talked to it.
git remote -v git branch -a
origin https://github.com/sam-rivera/recipes.git (fetch) origin https://github.com/sam-rivera/recipes.git (push) * main remotes/origin/HEAD -> origin/main remotes/origin/desserts remotes/origin/main
Only main is a local branch. To work on desserts, just switch to it — Git sees there's a remote branch with that name and creates a local one that tracks it:
git switch desserts
branch 'desserts' set up to track 'origin/desserts'. Switched to a new branch 'desserts'
Choosing the folder name
Add a folder name after the URL to clone somewhere else:
cd ~ git clone --quiet https://github.com/sam-rivera/recipes.git family-cookbook ls
family-cookbook recipes
Use . as the name to clone into the current folder (it must be empty).
Useful clone options
| Option | Effect |
|---|---|
-b desserts | Check out a different branch than the default |
--depth 1 | A shallow clone: only the latest commit, without history — much faster for huge repositories when you just need the code (e.g. in automated builds) |
--filter=blob:none | A partial clone: full history, but old file contents are downloaded only when needed |
--recurse-submodules | Also clone any submodules (lesson 46) |
--bare | Just the history, no working tree (used for servers and backups) |
git clone --depth 1 https://github.com/sam-rivera/recipes.git quick-look cd quick-look git log --oneline
Cloning into 'quick-look'... remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (4/4), done. 13992c7 (grafted, HEAD -> main, origin/main, origin/HEAD) Add omelette recipe
Only one commit came down. The grafted label marks where the cut-off history begins. A shallow clone can be deepened later with git fetch --unshallow if you need the history after all.
Clone vs. download ZIP
GitHub's "Download ZIP" gives you the files only — no .git folder, no history, no connection to the remote. That's fine for just using the code, but to contribute or keep up to date, clone.
Cloning by SSH
If you set up SSH keys (lesson 31), use the SSH URL from the Code button instead: git clone git@github.com:sam-rivera/recipes.git. Everything else is the same.
Try it yourself
Clone a small open-source project you like. Run git log --oneline -5, git branch -a and git remote -v, and find out how many commits it has with git rev-list --count HEAD.