Sub topic 1
Repositories, remotes, and why you clone
Prerequisites
This chapter assumes you have the following:
- Git installed on your computer.
- A GitHub account.
- Comfort running commands in a terminal.
- A basic idea of what a Git commit and a branch are.
What a repository is
A repository is a project's files plus its complete history, stored by Git. The history is a chain of commits. Each commit is a snapshot of the project at one moment, with an author, a message, and a pointer to the commit before it. Branches are named pointers into that chain.
A repository on your machine keeps this data in a hidden .git directory at the top of the project. The files you edit sit next to it and are called the working tree.
What a remote is
A remote is a named reference to another copy of the same repository, usually hosted on a server. GitHub hosts remote copies. Your local repository stores the remote's name and URL, and you use that name to exchange commits.
The default remote name is origin. Because Git is distributed, your local copy is a full repository too. You can browse history, commit, and create branches without a network connection. You only need the network when you exchange commits with a remote.
Why you clone
Cloning copies a remote repository to your computer and links the two. The command is git clone, and you will run it in a later chapter. You clone when you want to do any of the following:
- Work on a project that already exists on GitHub.
- Read the full history offline, with tools such as
git log. - Build, run, or test the code on your own machine.
- Contribute changes back through a branch and a pull request.
Clone versus downloading a ZIP
GitHub also offers a ZIP download of a branch. The two differ in what you get.
| Row | `git clone` | ZIP download |
|---|---|---|
| Files | Yes, from the checked-out branch | Yes, from one branch |
| Commit history | Yes, all of it by default | No |
| Other branches | Available as remote-tracking branches | No |
| Link to GitHub | Yes, the `origin` remote | No |
| Get later updates | `git fetch` and `git pull` | Download again |
A ZIP is a one-time snapshot. It is fine if you only need to read or run the files once. If you plan to track changes or contribute, clone instead.
What you get from a clone
A clone leaves three things on your machine:
- A full local copy of the history, stored in
.git. - A remote named
originthat points to the URL you cloned from. - A checked-out default branch in your working tree, so the files are ready to edit.
The next chapters cover where the URL comes from, how to authenticate, and the exact command.
Common misunderstandings
- A clone is not a live mirror. Your copy does not change when the remote changes. You must run
git fetchorgit pullto bring in new commits. - Committing locally does not change GitHub. Your commits stay local until you push them.
originis only a default name. Git does not treat it specially, and you can rename it or add more remotes.
Exercise
You download a repository as a ZIP from GitHub and make changes. Your teammate pushes new commits the next day. Why can't you run git pull to get them, and what would you do differently?
Show answer
A ZIP contains only the files from one snapshot. It has no .git directory, no history, and no origin remote, so Git has nothing to pull from. Clone the repository with git clone instead. The clone has a full history and an origin remote, so git fetch and git pull can bring in your teammate's commits.