Cloning and fetching
Overview
There are two ways commits come into your repository: git clone, once, to get a copy of a project, and git fetch, regularly, to download what others have pushed since. Both are completely safe — they never change your work.
Cloning
git clone https://github.com/asha/shop.git
git clone git@github.com:asha/shop.git my-shop # into a folder with a different namegit clone:
- creates a folder and an empty repository in it,
- adds the remote
originpointing at the URL, - downloads every commit, branch and tag — the full history,
- creates remote-tracking branches (
origin/main,origin/feature/…), - creates a local
maintrackingorigin/main, and checks it out.
You get a complete, independent repository. You can commit, branch and view history offline from now on.
Cloning big repositories
For huge projects you can download less:
git clone --depth 1 <url> # shallow: only the latest commit (fast, but no history)
git clone --filter=blob:none <url> # partial: all commits, but file contents downloaded on demandShallow clones are common in CI systems. For everyday development, prefer a full or partial clone — some commands (log, blame, bisect) need the history.
Fetching
git fetch # from origin
git fetch --all # from every remoteremote: Enumerating objects: 7, done.
…
From github.com:asha/shop
a1b2c3d..e4f5a6b main -> origin/main
* [new branch] feature/dark-mode -> origin/feature/dark-modegit fetch downloads new commits and updates remote-tracking branches — and that’s all. Your local branches, staging area and files are untouched. You can fetch at any time, even in the middle of work, without risk.
Look before you integrate
After fetching, you can see exactly what arrived before deciding what to do:
git status # Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.
git log --oneline main..origin/main # commits on origin/main that you don't have
git log --oneline origin/main..main # commits you have that origin/main doesn't
git diff main...origin/main # what changed on origin since you divergedThen integrate:
git merge --ff-only origin/main # if you have no local commits: just move forward
git rebase origin/main # if you do: replay yours on top
git merge origin/main # …or mergeThat fetch-inspect-integrate sequence is what git pull does in one step — the next lesson.
Working on someone else’s branch
To get a local copy of a branch that exists on the remote:
git switch feature/dark-modebranch 'feature/dark-mode' set up to track 'origin/feature/dark-mode'.
Switched to a new branch 'feature/dark-mode'If there’s no local branch with that name but exactly one remote has it, git switch creates a local branch tracking it. (If several remotes have it, use git switch -c feature/dark-mode --track origin/feature/dark-mode.)
To just look without creating a branch: git switch --detach origin/feature/dark-mode.
Pruning deleted branches
When a branch is deleted on GitHub (for example after its pull request is merged), your origin/feature/… record stays until you prune it:
git fetch --prune - [deleted] (none) -> origin/feature/dark-modeMake it automatic: git config --global fetch.prune true. Pruning only removes remote-tracking branches; your own local branches stay until you delete them with git branch -d.
Cloning vs downloading a ZIP
GitHub’s “Download ZIP” gives you the files at one commit — no history, no .git, no connection to the repository. You can’t commit or pull. Use it only when you need the files, never when you want to contribute.
Habit: start the day with
git fetch(orgit fetch --all --prune) andgit status. You’ll always know where you stand before you write a line of code.
Two developers, one repository
Simulate two people sharing a repository. Asha creates the project and pushes it to server.git. Ben clones it. Asha then pushes two more commits and a new branch feature/dark-mode. As Ben: fetch, check how far behind main is before changing anything, look at the incoming commits, then fast-forward. Check out Asha’s branch locally with git switch feature/dark-mode. Finally Asha deletes the feature branch on the server — prune it from Ben’s remote-tracking branches.
git init -q --bare -b main server.git
# Asha creates and publishes the project
git clone -q server.git asha 2>/dev/null
cd asha
echo "app" > app.txt && git add . && git commit -qm "Start app" && git push -qu origin main
cd ..
# 1. Ben clones
# Asha keeps working
cd asha
echo "login" > login.txt && git add . && git commit -qm "Add login"
echo "logout" > logout.txt && git add . && git commit -qm "Add logout"
git push -q
git switch -qc feature/dark-mode && echo dark > theme.txt && git add . && git commit -qm "Dark theme" && git push -qu origin feature/dark-mode
cd ..
# 2. Ben: fetch, inspect, fast-forward, try the feature branch
# 3. Asha deletes the branch on the server; Ben prunesgit init -q --bare -b main server.git
# Asha creates and publishes the project
git clone -q server.git asha 2>/dev/null
cd asha
echo "app" > app.txt && git add . && git commit -qm "Start app" && git push -qu origin main
cd ..
# 1. Ben clones
git clone server.git ben
# Asha keeps working
cd asha
echo "login" > login.txt && git add . && git commit -qm "Add login"
echo "logout" > logout.txt && git add . && git commit -qm "Add logout"
git push -q
git switch -qc feature/dark-mode && echo dark > theme.txt && git add . && git commit -qm "Dark theme" && git push -qu origin feature/dark-mode
cd ..
# 2. Ben: fetch, inspect, fast-forward, try the feature branch
cd ben
git fetch # downloads; Ben's files don't change
git status # Your branch is behind 'origin/main' by 2 commits…
git log --oneline main..origin/main # Add logout / Add login
git merge --ff-only origin/main # now up to date
git switch feature/dark-mode # creates a local branch tracking origin/feature/dark-mode
cd ..
# 3. Asha deletes the branch on the server; Ben prunes
(cd asha && git push -q origin --delete feature/dark-mode)
cd ben
git fetch --prune # - [deleted] (none) -> origin/feature/dark-mode
git branch -r # only origin/HEAD and origin/main leftSummary
git clone <url>copies the whole repository and history, sets uporiginand checks out the default branch.git fetchdownloads new commits and updates remote-tracking branches, but never touches your files or branches.- Inspect before integrating:
git log main..origin/mainandgit diff main...origin/main. git switch <branch>creates a local branch from a matching remote-tracking branch automatically.git fetch --pruneremoves remote-tracking branches that were deleted on the server.