GitHub Basics
Push your code to GitHub, collaborate with others, and build a public portfolio of your work.
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
- Log in to GitHub and click the + icon → New repository.
- Give it a name (e.g.
my-project). - Do not tick "Add a README" if you already have a local repo — it creates conflicts.
- 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
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.
- Go to your repository on GitHub.
- Click Settings → Pages.
- Under "Source", select Deploy from a branch.
- Select main branch and / (root).
- 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 statuswhich 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 pullfirst. - Pushing API keys or passwords. Once pushed, secrets are visible to everyone. Use
.gitignoreto exclude sensitive files. If you accidentally push a secret, rotate it immediately. - Force pushing to shared branches.
git push --forceoverwrites the remote history. Never do this onmainor any branch others are working on.
Practice Exercise
- Create a free account on GitHub if you don't have one.
- Create a new repository called
git-practiceon GitHub (no README). - Connect your local
git-practicefolder to it withgit remote add origin <url>. - Push with
git push -u origin main. - Refresh the GitHub page — you should see your files.
- Make a change locally, commit it, and push again with just
git push. - Enable GitHub Pages for the repository and visit the live URL.
Assignment
Upload one of your mini project folders to a new GitHub repository:
- Run
git initinside the project folder (if not already a repo). - Add a
README.mdfile with a brief description of the project (one paragraph is fine). - Commit everything:
git add . && git commit -m "Initial commit". - Create a GitHub repo and push.
- 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 fetchandgit pull? —fetchdownloads remote changes without merging;pulldownloads and immediately merges. Fetch lets you inspect before merging. - What does
originrefer to? —originis 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 localmainbranch to origin and sets it as the upstream tracking branch, so futuregit pushcommands 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.