Cloning and fetching

Topic 2 of 4 in Working with remotes6 min readBeginnerFree course

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

Terminal
git clone https://github.com/asha/shop.git
git clone git@github.com:asha/shop.git my-shop     # into a folder with a different name

git clone:

  1. creates a folder and an empty repository in it,
  2. adds the remote origin pointing at the URL,
  3. downloads every commit, branch and tag — the full history,
  4. creates remote-tracking branches (origin/main, origin/feature/…),
  5. creates a local main tracking origin/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:

Terminal
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 demand

Shallow clones are common in CI systems. For everyday development, prefer a full or partial clone — some commands (log, blame, bisect) need the history.

Fetching

Terminal
git fetch            # from origin
git fetch --all      # from every remote
Text
remote: Enumerating objects: 7, done.
…
From github.com:asha/shop
   a1b2c3d..e4f5a6b  main              -> origin/main
 * [new branch]      feature/dark-mode -> origin/feature/dark-mode

git 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.

Moving commits between repositories. Four columns: working directory, local branch, origin/main and GitHub (origin). git fetch downloads commits from GitHub into origin/main; your files do not change. git pull = fetch + merge brings the changes all the way into your branch and files. git push uploads your new commits from your local branch to GitHub. Fetch often — it is always safe. Pull when you are ready to combine. Push when your commits are ready to share.

Look before you integrate

After fetching, you can see exactly what arrived before deciding what to do:

Terminal
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 diverged

Then integrate:

Terminal
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 merge

That 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:

Terminal
git switch feature/dark-mode
Text
branch '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:

Terminal
git fetch --prune
Text
 - [deleted]         (none)     -> origin/feature/dark-mode

Make 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 (or git fetch --all --prune) and git status. You’ll always know where you stand before you write a line of code.

Exercise

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.

Terminal
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 prunes

Summary

  • git clone <url> copies the whole repository and history, sets up origin and checks out the default branch.
  • git fetch downloads new commits and updates remote-tracking branches, but never touches your files or branches.
  • Inspect before integrating: git log main..origin/main and git diff main...origin/main.
  • git switch <branch> creates a local branch from a matching remote-tracking branch automatically.
  • git fetch --prune removes remote-tracking branches that were deleted on the server.