Git and GitHub

GitHub is a website where you can store your Git repositories online, share them and work with other people. GitLab and Bitbucket are similar services. They all use Git underneath, so the commands you learned in this tutorial work the same.

Create an account and a repository

  • Create a free account on github.com.
  • Click the + button and choose New repository.
  • Write a repository name, like my-first-project, and choose Public or Private.
  • If you already have the project on your computer, do not add a README here. Click Create repository.

Push an existing project to GitHub

GitHub shows you these commands after you create the empty repository. Run them inside your project folder:

git init
git add .
git commit -m "First commit"
git branch -M main
git remote add origin https://github.com/username/my-first-project.git
git push -u origin main

The first time you push over HTTPS, GitHub asks for a username and a personal access token (the account password does not work). With SSH, you do not need it, see Git SSH Key.

Download a project from GitHub

git clone https://github.com/username/project.git

Fork and pull request

You can not push directly to the repository of another person. To suggest a change to an open source project:

  • Fork the repository (the Fork button). GitHub makes your own copy of it.
  • Clone your fork to your computer.
  • Create a branch for your change, commit and push it to your fork.
  • On GitHub, click Compare and pull request, write what you changed and why, and submit.
  • The owner reviews it. They may ask for changes, and then merge it.
git clone https://github.com/yourname/project.git
cd project
git remote add upstream https://github.com/original-owner/project.git
git switch -c fix-typo
git add .
git commit -m "Fix typo in readme"
git push -u origin fix-typo

Keep your fork updated

git fetch upstream
git switch main
git merge upstream/main
git push origin main

Other GitHub features

FeatureWhat it is for
IssuesTrack bugs and tasks
Pull requestsReview and discuss changes before the merge
ActionsRun tests and deploy automatically
PagesHost a static website for free
ReleasesShare a version of the project from a tag
ProjectsPlan and track work in boards
Note: Do not push passwords, API keys or .env files to a public repository. Use .gitignore to keep them out. Next: Git Commands Cheat Sheet.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.