Skip to content
Programming contents

Lesson 2.6: GitHub: your code online

GitHub keeps a copy of your repositories online: so you don't lose them, can share them and can work with others.

The idea

Git is on your computer. GitHub is a website that keeps a copy of your repositories. They're not the same: Git works without GitHub.

What the copy is for:

  • If your computer breaks, you lose nothing.
  • You can show your code and work with others.
  • You can publish a website for free (2.7).

Creating the repository on GitHub

  1. Create an account on github.com.
  2. Click + → New repository.
  3. Call it my-game, leave it Public and don't tick "Add a README".
  4. Click Create repository.

Uploading your code

GitHub shows you the commands. They're these, with your username:

Terminal
git remote add origin https://github.com/YOUR-USERNAME/my-game.git
git push -u origin main
  • git remote add tells your repository where its copy on GitHub is. It's called origin.
  • git push uploads your commits. -u origin main is only needed the first time.

Signing in

The first time, Git will ask you to sign in to GitHub. Your GitHub password doesn't work in the terminal:

  • Windows: the browser opens so you can sign in.
  • macOS and Linux: install GitHub CLI (cli.github.com) and run gh auth login once.

Afterwards

Whenever you want to upload new commits:

Terminal
git push

And to download a repository from GitHub, yours or someone else's:

Terminal
git clone https://github.com/USERNAME/REPOSITORY.git

It creates a folder with the project and all its history.

Try it

Exercise

Upload my-game to GitHub and check on the website that hello.js, table.js and your commits are there.

Show the solution
Terminal
git remote add origin https://github.com/YOUR-USERNAME/my-game.git
git push -u origin main

At github.com/YOUR-USERNAME/my-game you'll see the files, and the commits link shows the history. If git remote add says origin already exists, you'd already connected it: go on to git push.

Summary

  • Git is local; GitHub keeps a copy online.
  • git remote add origin URL connects them, once.
  • git push uploads; git clone downloads.
  • In the terminal you sign in through the browser or gh auth login, not with your password.