Working with Remotes
Everything so far has lived only on your machine. A remote is a copy of the repository hosted somewhere else — usually a service like GitHub — that you and your collaborators sync with.
git clone
To get a local copy of a repository that already exists somewhere else (a teammate's project, an open-source library, your own repo on GitHub), use git clone with its URL:
git clone https://github.com/ava-chen/my-project.git
This downloads the entire history — not just the latest files — and sets up a connection back to that URL automatically, so you don't have to configure anything else to start pulling and pushing.
git remote
That connection is called a remote, and by convention the one you cloned from is named origin. See what remotes a repository knows about:
git remote -v
origin https://github.com/ava-chen/my-project.git (fetch) origin https://github.com/ava-chen/my-project.git (push)
If you started with git init locally instead of cloning, you can connect it to a freshly created (empty) GitHub repository yourself:
git remote add origin https://github.com/ava-chen/my-project.git
git push
Push sends your local commits up to the remote, so other people (and other machines) can see them:
git push origin main
This means "push the main branch to the remote named origin." The very first time you push a new branch, add -u so Git remembers this pairing and lets you just type git push from then on:
git push -u origin main
git pull
Pull does the reverse — it fetches any new commits from the remote and merges them into your current branch, which is how you stay up to date with everyone else's work:
git pull origin main
origin is what git clone names your source remote by default, and upstream is a common (also just conventional) name people add when working from a fork of someone else's repository, to distinguish the original project from their own copy.Make it a habit to git pull before you start working and before you push — it avoids a lot of avoidable merge conflicts caused by working from stale history.