Lesson 31 +15 XP

Set Remote and Clone

Set Remote and Clone

There are two ways to connect a local project to a remote: add a remote to an existing project, or clone an existing remote project.

Add a remote to a project

If you already have a local repository and a new (empty) GitHub repository, connect them:

git remote add origin https://github.com/user/repo.git

Then push your code:

git push -u origin main

The -u flag sets origin/main as the default, so later pushes are just git push.

Clone an existing project

If the project already exists online, copy it to your computer:

git clone https://github.com/user/repo.git

This downloads the project, including its full history, and sets up origin automatically.

Clone into a folder

git clone https://github.com/user/repo.git my-folder

The last argument names the destination folder.

Verify the connection

git remote -v

TL;DR

  • git remote add origin URL connects an existing repo.
  • git push -u origin main pushes and sets the default.
  • git clone URL copies a remote project locally.
  • Clones set up origin for you automatically.