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
- Create an account on github.com.
- Click + → New repository.
- Call it
my-game, leave it Public and don't tick "Add a README". - Click Create repository.
Uploading your code
GitHub shows you the commands. They're these, with your username:
git remote add origin https://github.com/YOUR-USERNAME/my-game.git
git push -u origin main
git remote addtells your repository where its copy on GitHub is. It's calledorigin.git pushuploads your commits.-u origin mainis 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 loginonce.
Afterwards
Whenever you want to upload new commits:
git push
And to download a repository from GitHub, yours or someone else's:
git clone https://github.com/USERNAME/REPOSITORY.git
It creates a folder with the project and all its history.
Try it
Upload my-game to GitHub and check on the website that hello.js, table.js and your commits are there.
Show the solution
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 URLconnects them, once.git pushuploads;git clonedownloads.- In the terminal you sign in through the browser or
gh auth login, not with your password.