Level 5

Clone & remote

Make a copy of an existing repository, and link your original to it so the two can share pages.

In Level 5 you will learn the ancient art of cloning: a command that makes a duplicate copy of an entire scrapbook.

Think back to Level 2. When you ran git init, it initialized a totally new, empty binder. What I did not tell you back then is that there is another way to get a scrapbook: cloning an existing one.

Step 1 of 8
0 xp

Two ways to get a scrapbook

A clone in Git is a duplicate copy of an already existing repository. Kind of like an evil twin, but friendlier.

git init is like starting fresh with an empty binder. git clone is like copying a scrapbook that you or someone else already started. Unless you start a brand new project yourself, you will most likely join a team that already uses Git on an established project, and cloning downloads an exact copy of their scrapbook so you can jump in and start working on it too.

The shape of the command

git clone <url> <dest>

The <url> tells Git where to find the scrapbook you want to clone. It is most often a web address, like one from GitHub, but it can also be a path to a scrapbook on your own computer. The second part, <dest>, tells Git where on your computer to put the copy.

What you are about to do is not the most common use case, but it is great practice: you will clone your current scrapbook, containing everything you have done so far, into a folder called scrapbook-copy right next to it. A dot means "this folder", and .. means "the folder above this one".

Make the copy

Off you go, then:

Type this into the terminal below
git clone . ../scrapbook-copy

You just cloned the scrapbook you have been working in. The copy has the same three pages, with the same commit IDs and everything.

Does that make sense, or are you stumped? Do not worry, it will all be clear soon. For now, just know that you have a duplicate of the repository, which we will use for some fun stuff going forward.

safe 'clone' creates a new clone; nothing is discarded.

Local and remote

Now that you have two repositories, you need a way to tell them apart. Call the original your local repository: the one you code in and make commits in. Call the copy the remote repository, even though in this exercise it is just another folder on the same computer.

Think of the remote as the official family scrapbook that everyone can see, and your local copy as the one on your desk.

Linking the two

Even though you created the copy, your original does not know it exists. You need to link the two, so you can send updates back and forth between your local scrapbook and the shared one. That is the job of git remote:

git remote add <name> <url_or_path>

git remote add tells Git you want to link your local repository to a remote one. <name> is a nickname for that remote, and <url_or_path> is where it lives. The nickname almost everyone uses is origin, but you could pick anything. The same command has siblings: git remote rename <old> <new> changes the nickname, and git remote remove <name> drops the link.

Link them

Link the two under the usual nickname:

Type this into the terminal below
git remote add origin ../scrapbook-copy

Your local repository is now linked to its remote doppelganger under the name origin. Nothing moved: this is a bookmark saying "the shared copy lives over there". Level 6 uses it.

safe 'remote add' edits .git/config; no history or files change.

Linked

Here is the situation. Two identical scrapbooks, one on your desk and one that stands in for the family copy, and a link between them called origin. This link is the foundation of how collaboration works in Git.

Your turn: a better nickname

You would rather call the remote family than origin. Which command changes the nickname without losing the link?

git-sim
Initial CommitDevlands

The graph appears here when the first command runs. The simulations are drawn by git-sim from a real repository.

The scrapbook · one picture for every Git idea

The scrapbook picture appears as the story is told.