Home Module 16 GitHub Basics

Introduction

GitHub is a cloud platform where developers store, share, and collaborate on Git repositories. It is the industry standard for open-source projects and the primary way employers evaluate your code. A strong GitHub profile is one of the best things you can have as a junior developer.

In this lesson you will learn how to: connect a local repository to GitHub, push code, pull updates, and understand the relationship between local and remote.

Key Concepts

Remote

A remote is a version of your repository hosted on a server (usually GitHub). By convention the main remote is named origin.

Push and Pull

  • Push — send your local commits to the remote repository.
  • Pull — download and merge commits from the remote into your local branch.
  • Fetch — download commits from the remote but do NOT merge yet (so you can inspect before merging).

Tracking branches

When you push a branch, Git links your local branch to the remote branch. Your local main tracks origin/main. git status will tell you if you are ahead or behind the remote.

Connecting a Local Repo to GitHub

Step 1 — Create a repository on GitHub

  1. Log in to GitHub and click the + icon → New repository.
  2. Give it a name (e.g. my-project).
  3. Do not tick "Add a README" if you already have a local repo — it creates conflicts.
  4. Click Create repository.

Step 2 — Link your local repo and push

# Add the remote (paste your GitHub URL)
git remote add origin git@github.com:username/my-project.git

# Verify the remote was added
git remote -v

# Push and set upstream tracking
git push -u origin main
# -u sets the upstream so future pushes just need: git push

Subsequent pushes

# After the first push, just run:
git push

Cloning a Repository

Cloning downloads a full copy of a remote repository including its entire history:

# Clone via SSH (recommended if you have SSH keys set up)
git clone git@github.com:username/repo-name.git

# Clone via HTTPS
git clone https://github.com/username/repo-name.git

# The remote 'origin' is automatically configured
cd repo-name
git remote -v
# origin  git@github.com:username/repo-name.git (fetch)
# origin  git@github.com:username/repo-name.git (push)

Pulling Updates

# Download and merge the latest changes from origin/main
git pull

# Same as:
git fetch origin
git merge origin/main

# Pull a specific remote branch
git pull origin feature/new-header
Always pull before you start working for the day when collaborating with others. This ensures you are working with the latest code and reduces merge conflicts.

Remote Commands Reference

# List remotes
git remote -v

# Add a remote
git remote add origin <url>

# Change the remote URL
git remote set-url origin <new-url>

# Remove a remote
git remote remove origin

# Push a branch for the first time (sets upstream)
git push -u origin branch-name

# Push subsequent commits
git push

# Pull latest changes
git pull

# Fetch without merging
git fetch origin

# See remote branches
git branch -r

# See all branches (local + remote)
git branch -a

GitHub Pages — Free Hosting

GitHub Pages lets you host a static website directly from a GitHub repository for free. This is perfect for your portfolio and mini projects.

  1. Go to your repository on GitHub.
  2. Click Settings → Pages.
  3. Under "Source", select Deploy from a branch.
  4. Select main branch and / (root).
  5. Click Save. Your site will be live at https://username.github.io/repo-name/ within a minute.

Every time you push to main, GitHub Pages automatically rebuilds your site.

Common Mistakes

  • Pushing to the wrong branch. Double-check with git status which branch you are on before pushing.
  • Forgetting to pull before pushing. If the remote has changes you don't have locally, your push will be rejected. Run git pull first.
  • Pushing API keys or passwords. Once pushed, secrets are visible to everyone. Use .gitignore to exclude sensitive files. If you accidentally push a secret, rotate it immediately.
  • Force pushing to shared branches. git push --force overwrites the remote history. Never do this on main or any branch others are working on.

Practice Exercise

  1. Create a free account on GitHub if you don't have one.
  2. Create a new repository called git-practice on GitHub (no README).
  3. Connect your local git-practice folder to it with git remote add origin <url>.
  4. Push with git push -u origin main.
  5. Refresh the GitHub page — you should see your files.
  6. Make a change locally, commit it, and push again with just git push.
  7. Enable GitHub Pages for the repository and visit the live URL.

Assignment

Upload one of your mini project folders to a new GitHub repository:

  1. Run git init inside the project folder (if not already a repo).
  2. Add a README.md file with a brief description of the project (one paragraph is fine).
  3. Commit everything: git add . && git commit -m "Initial commit".
  4. Create a GitHub repo and push.
  5. Enable GitHub Pages. Paste the live URL somewhere you can find it — this is the start of your portfolio.

Interview Questions

  • What is the difference between git fetch and git pull?fetch downloads remote changes without merging; pull downloads and immediately merges. Fetch lets you inspect before merging.
  • What does origin refer to?origin is the default name Git gives to the remote repository you cloned from or explicitly added.
  • What is git push -u origin main? — It pushes the local main branch to origin and sets it as the upstream tracking branch, so future git push commands know where to push.
  • What is GitHub Pages? — A free static site hosting service provided by GitHub that serves your repository's HTML files directly at a public URL.