Gitastic
Path › Part 3: Working with others › Lesson 8 of 16
08

Remotes & clone

Your repo, their repo, and the sticky notes that remember where they were.

1 Learn the idea

So far everything lived on your laptop. Teams share work through a remote: another full copy of the repository, usually on GitHub or GitLab. By convention the one you cloned from is called origin.

Three different "mains"

  • main on origin: the real branch on the server. Your teammates move it.
  • origin/main on your laptop: your memory of where origin's main was at the last fetch, pull or push.
  • main on your laptop: your own branch. Only you move it.

2 Watch it happen

Clone, then a teammate pushes

Press Play or use → to step through. Watch the graph as each command runs.

No commits yet — the history is empty.
terminal · simulated, no real repository is touched
Read this demo as text
  1. $ git clone git@github.com:you/project.git

    Cloning copies every commit from origin and creates your own main, plus the bookmark origin/main pointing at the same commit.

  2. $ git remote -v

    git remote -v lists the remotes this repo knows about. There is one: origin, used for both fetching and pushing.

  3. $ git branch -a

    -a shows local branches and remote-tracking ones (listed as remotes/origin/main). They are separate things.

  4. $ teammate push -m "Add checkout page"

    Your teammate Sam pushes a commit to origin. Look at the top panel: origin's main moved. Your laptop has no idea yet.

  5. $ git status

    Status says "up to date with origin/main"… because it compares with your last postcard. Git does not check the network by itself.

  6. $ git fetch

    git fetch contacts origin, downloads Sam's commit and moves origin/main forward. Your main and your files stay exactly where they were.

  7. $ git status

    Now status knows: your branch is behind origin/main by 1 commit and can be fast-forwarded.

  8. $ git merge origin/main

    Merging the bookmark into main brings you up to date. Since you had nothing new, it is a simple fast-forward. (Fetch + merge is exactly what git pull does, next lesson.)

  9. $ git branch -vv

    -vv shows each branch with its upstream (the remote branch it tracks) and whether it is ahead or behind.

git fetch
  • Downloads new commits
  • Moves origin/* bookmarks
  • Never changes your branches or files
  • Safe to run anytime
git pull
  • Runs fetch first
  • Then merges (or rebases) into your branch
  • Changes your branch and files
  • Can produce conflicts
git clone <url>Copy a remote repository to your machine
git remote -vList remotes and their URLs
git fetchDownload new commits and update origin/* (safe)
git branch -vvShow branches with upstream and ahead/behind

3 Check your understanding

Question 1

What is origin/main on your laptop?

Question 2

A teammate pushed 3 minutes ago. You run git status and it says "up to date with origin/main". Why?

Question 3

Look at the graph. How many commits is your main ahead of origin/main?

origin (GitHub — shared with your team)
220addd Initial commit220adddInitial commitBranch "main" — a movable pointer (current branch: moves when you commit)main
your laptop ~/project · on main
220addd Initial commit220adddInitial commit172f236 Add search172f236Add searche20c8d2 Style searche20c8d2Style searchRemote-tracking branch: where origin's main was last time you fetchedorigin/mainHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
Question 4

Which command can never change your working files?

4 Practice for real

Your mission

Catch up with the team

Sam pushed two commits to origin while you were away. Bring your local main up to date with origin without creating any new commit of your own.

origin (GitHub — shared with your team)
220addd Initial commit220adddInitial commit0f80ea7 Add product page0f80ea7Add product pa…f8caade Add cartf8caadeAdd cart76046d4 Add checkout76046d4Add checkoutBranch "main" — a movable pointer (current branch: moves when you commit)main
your laptop ~/project · on main
220addd Initial commit click to insert the hash into the terminal220adddInitial commit0f80ea7 Add product page click to insert the hash into the terminal0f80ea7Add product pa…HEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)mainRemote-tracking branch: where origin's main was last time you fetchedorigin/main
terminal · simulated, no real repository is touched
Type commands below. `help` lists everything. Tab completes, ↑ recalls. Click a commit to paste its hash.
~/project (main) $