Lesson 1 of 5 in Remotes & Collaboration

Cloning

Story: HQ keeps the master copy of every timeline in its archive. Take out a copy you can work on.

Until now, each repository lived only on your computer. A team shares its work through a remote: another copy of the repository, usually on a server everyone can reach. In this course, that server is git.example.

git clone makes your own copy of a remote repository:

git clone https://git.example/team/recipes.git

It creates a folder named after the repository, recipes, and copies everything into it: every commit, every branch, the whole history. Then it checks out main, so the files are there to work on. Move into the folder with cd recipes before you run git commands there.

A clone is a complete repository. You can read its history, commit and make branches without the server. You only talk to the server again when you choose to: to send your commits (git push), or to get other people's (git fetch and git pull). The next two Lessons cover those.

Choosing the folder

Give git clone a second name to clone into a different folder:

git clone https://git.example/team/recipes.git cookbook

The name is a path, so ../cookbook works too: .. is the folder above the one you're in.

git won't clone into a folder that already has files in it. It stops with fatal: destination path 'recipes' already exists and is not an empty directory, and changes nothing. Pick another name. Or make an empty folder, cd into it, and run git clone <url> . to clone right there: . means "this folder".

origin

A clone remembers where it came from, under the name origin. git remote -v lists your remotes:

origin  https://git.example/team/recipes.git (fetch)
origin  https://git.example/team/recipes.git (push)

Each remote is listed twice, with the address git fetches from and the one it pushes to. They're almost always the same. When you need a repository's address, to clone it again or to tell a teammate, this is where to look.

Remote-tracking branches

git log --oneline --all in a fresh clone shows more names than main:

68ff54e (HEAD -> main, origin/main, origin/HEAD) Add a drinks list
4677213 Add the bread recipe
ad1deb4 Add the soup recipe
  • main is your own branch. It moves when you commit.
  • origin/main is a remote-tracking branch: your repository's note of where main was on origin the last time you talked to it. You don't commit on it. git moves it when you fetch, pull or push.
  • origin/HEAD names the branch origin treats as its main one.

The Live Graph draws origin/main as a dashed label. When origin itself is shown, it gets a band of its own, with its own branches.

Your branch's upstream

A clone also makes origin/main the upstream of your main: the remote branch it's compared with, and the one it pushes to and pulls from by default. That's what git status reports:

On branch main
Your branch is up to date with 'origin/main'.

"Up to date" means up to date with what your repository last saw. git status doesn't ask the server. If a teammate pushes a minute after you clone, git status won't know until you fetch.

New commands

  • git clone
  • git remote -v

Explainer: A clone is a full copy

recipes: your repo, you are hereorigin: remote host (bare), https://git.example/team/recipes.gitad1deb4 Add the soup recipead1deb44677213 Add the bread recipe4677213HEAD → mainorigin/HEADorigin/mainad1deb4 Add the soup recipead1deb44677213 Add the bread recipe4677213main
The repository at the end of the Explainer.
Graph as text

recipes: your repo, you are here

  • HEAD → main at 4677213 "Add the bread recipe"

Branches and tags

  • main at 4677213 "Add the bread recipe", tracks origin/main
  • origin/HEAD at 4677213 "Add the bread recipe"
  • origin/main at 4677213 "Add the bread recipe"

Commits, newest first

  1. 4677213 "Add the bread recipe"; parent ad1deb4; refs: HEAD → main, origin/HEAD, origin/main
  2. ad1deb4 "Add the soup recipe"; no parent

origin: remote host (bare), https://git.example/team/recipes.git

Branches and tags

  • main at 4677213 "Add the bread recipe"

Commits, newest first

  1. 4677213 "Add the bread recipe"; parent ad1deb4; refs: main
  2. ad1deb4 "Add the soup recipe"; no parent

Not shown: teammate.

Challenge 1 of 3

Story: The recipes timeline is in HQ's archive. Check out a copy for yourself.

The team's recipes are in a repository at https://git.example/team/recipes.git. Make your own copy of it, history included, in ~/recipes, and move into it.

Unlocks after Branching & Merging.

Terminal

Unlocks after Branching & Merging.

This Lesson can be played once Branching & Merging is complete. Its text and Explainer are open to everyone.