Beginner 2 min read

Push Code to GitHub

You wrote code on your computer. Now you want it on GitHub — backed up, shareable, and version tracked.

This tutorial shows you exactly how to do that from the terminal.


Before You Start

Make sure you have: - Git installed → git --version (should show a version number) - A GitHub account - A project folder on your computer


Step 1: Open Your Project Folder

cd your_project_folder

Step 2: Initialize Git

This tells Git to start tracking your project.

git init

You'll see: Initialized empty Git repository


Step 3: Add Your Files

Stage all files so Git knows what to include:

git add .

The . means "everything in this folder".

To check what's staged:

git status

Step 4: Commit Your Changes

A commit is like saving a snapshot of your code.

git commit -m "first commit"

Write a short message that describes what you did. This message shows in your GitHub history.


Step 5: Create a Repository on GitHub

  1. Go to github.com
  2. Click +New repository
  3. Give it a name (e.g. my-project)
  4. Keep it Public or Private
  5. Do NOT check "Add README" — your local project already has files
  6. Click Create repository

GitHub shows you a URL like:

https://github.com/yourusername/my-project.git

Copy it.


Step 6: Connect Local Repo to GitHub

git remote add origin https://github.com/yourusername/my-project.git

Step 7: Push Your Code

git branch -M main
git push -u origin main

Go to your GitHub repo — your files are now there. ✅


Quick Reference

git init                          # Start tracking
git add .                         # Stage all files
git commit -m "message"           # Save snapshot
git remote add origin <url>       # Connect to GitHub
git push -u origin main           # Upload code

Common Errors

error: src refspec main does not match any → You haven't committed yet. Run git add . and git commit first.

remote: Repository not found → Wrong URL. Check the GitHub repo URL and run git remote set-url origin <correct-url>

Username/password prompt → GitHub no longer accepts passwords. Use a Personal Access Token or switch to SSH.


Next Step

👉 Set up SSH so you never type your password again