From Zero to AI

Lesson 5.4: Git and GitHub - Version Control

Duration: 50 minutes

Learning Objectives

After completing this lesson, you will be able to:

  • Understand what version control is and why it's essential
  • Install and configure Git on your computer
  • Create a GitHub account and understand its purpose
  • Initialize a Git repository and make your first commit
  • Use basic Git commands confidently
  • Connect a local repository to GitHub

Introduction

Imagine you're writing a long document. You make changes, then realize you preferred the earlier version. But you've already saved over it. Frustrating, right?

Now imagine working on code with other people. Everyone is editing the same files. How do you keep track of who changed what? How do you merge everyone's work together?

This is where version control comes in, and Git is the tool that makes it possible. Git is used by virtually every software development team in the world, and learning it is essential for any developer.


Main Content

What is Version Control?

A system that records changes to files over time, so you can recall specific versions later.

With Git, all history is saved and you can go back to any version.

Git vs GitHub

These are often confused, but they're different things:

Git GitHub
Software on your computer Website/cloud service
Tracks changes locally Stores your code online
Works offline Requires internet
Free and open source Free and paid tiers
Command-line tool Web interface + features

Think of it this way:

  • Git is like a detailed journal of all changes you make to your code
  • GitHub is like a cloud backup service where you store that journal online and share it with others

Installing Git

For Windows

Option A: Git for Windows (Recommended)

  1. Go to https://git-scm.com/download/win
  2. The download should start automatically
  3. Run the installer
  4. Important settings during installation:
    • "Adjusting your PATH environment" → Select "Git from the command line and also from 3rd-party software"
    • "Choosing the default editor" → Select "Use Visual Studio Code as Git's default editor"
    • "Adjusting the name of the initial branch" → Select "Override the default branch name" and type main
    • Keep other settings as default
  5. Complete the installation

Verify installation:

git --version

You should see something like: git version 2.43.0.windows.1


For macOS

Option A: Xcode Command Line Tools (Easiest)

Open Terminal and type:

git --version

If Git isn't installed, macOS will prompt you to install the Xcode Command Line Tools. Click "Install".

Option B: Using Homebrew

brew install git

Verify installation:

git --version

For Linux (Ubuntu/Debian)

sudo apt update
sudo apt install git

Verify installation:

git --version

Configuring Git

Before you can use Git, you need to tell it who you are. This information is attached to every change you make.

Set your name:

git config --global user.name "Your Name"

Set your email (use the same email you'll use for GitHub):

git config --global user.email "your.email@example.com"

Set the default branch name:

git config --global init.defaultBranch main

Set VS Code as your default editor:

git config --global core.editor "code --wait"

Verify your configuration:

git config --list

Creating a GitHub Account

GitHub is where you'll store your code online, collaborate with others, and build your portfolio.

Step 1: Sign Up

  1. Go to https://github.com
  2. Click "Sign up"
  3. Enter your email address
  4. Create a strong password
  5. Choose a username (this will be public, choose wisely!)
  6. Complete the verification puzzle
  7. Select the free plan

Step 2: Verify Your Email

Check your inbox and click the verification link from GitHub.

Step 3: Complete Your Profile

  1. Add a profile picture (optional but recommended)
  2. Write a short bio
  3. This is your developer portfolio - make it presentable!

Understanding Git Concepts

Before using Git commands, let's understand the key concepts:

Concept Description
Repository (repo) A folder tracked by Git, containing all files and history
Working Directory The files you see and edit
Staging Area Files marked to be included in the next commit
Commit A snapshot of your staged files at a point in time
Branch A parallel version of your code
Remote A version of your repository hosted elsewhere (like GitHub)

Basic Git Commands

Let's learn the essential Git commands through a practical example.

Creating a New Repository

Step 1: Create a project folder

# Navigate to your projects folder
cd ~/Documents/coding-projects

# Create a new folder
mkdir my-first-repo

# Enter the folder
cd my-first-repo

Step 2: Initialize Git

git init

You'll see: Initialized empty Git repository in .../my-first-repo/.git/

A hidden .git folder has been created. This is where Git stores all its tracking information.

Making Your First Commit

Step 3: Create a file

# Create a README file
echo "# My First Repository" > README.md

Step 4: Check the status

git status

You'll see something like:

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md

nothing added to commit but untracked files present

Step 5: Stage the file

git add README.md

Or to stage all changes:

git add .

Step 6: Check status again

git status

Now you'll see:

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   README.md

Step 7: Commit the changes

git commit -m "Initial commit: add README"

The -m flag lets you add a commit message. Always write meaningful messages!

Viewing History

git log

You'll see:

commit abc123... (HEAD -> main)
Author: Your Name <your.email@example.com>
Date:   Mon Jan 8 10:00:00 2024 +0000

    Initial commit: add README

For a compact view:

git log --oneline

Connecting to GitHub

Now let's push your local repository to GitHub.

Step 1: Create a new repository on GitHub

  1. Go to github.com and log in
  2. Click the "+" icon in the top right → "New repository"
  3. Name it my-first-repo (same as your local folder)
  4. Leave it as "Public" (or choose Private)
  5. Don't check "Add a README file" (we already have one)
  6. Click "Create repository"

Step 2: Connect your local repo to GitHub

GitHub will show you commands. Use the "push an existing repository" option:

git remote add origin https://github.com/YOUR-USERNAME/my-first-repo.git
git branch -M main
git push -u origin main

Replace YOUR-USERNAME with your actual GitHub username.

Step 3: Authenticate

When you push for the first time, Git will ask for your GitHub credentials.

Important: GitHub no longer accepts passwords for Git operations. You'll need to either:

Option A: Use a Personal Access Token

  1. Go to GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic)
  2. Click "Generate new token (classic)"
  3. Give it a name, select "repo" scope
  4. Copy the token and use it as your password

Option B: Use GitHub CLI (Recommended)

# Install GitHub CLI
# Windows: winget install --id GitHub.cli
# macOS: brew install gh
# Linux: sudo apt install gh

# Login
gh auth login
# Follow the prompts, choose HTTPS

Step 4: Push your code

git push -u origin main

Refresh your GitHub repository page - your files are now online!


Git Commands Cheat Sheet


Troubleshooting

"fatal: not a git repository"

You're not inside a Git repository. Either:

  • Navigate to the correct folder with cd
  • Initialize Git with git init

"Permission denied (publickey)"

GitHub can't authenticate you. Solutions:

  1. Use HTTPS instead of SSH: git remote set-url origin https://github.com/USERNAME/REPO.git
  2. Set up SSH keys: GitHub SSH Guide
  3. Use GitHub CLI: gh auth login

"Updates were rejected because the remote contains work..."

The GitHub repository has changes you don't have locally:

git pull --rebase origin main
git push

"Your branch is ahead of 'origin/main'"

You have commits locally that aren't on GitHub yet:

git push

Undo Mistakes

Unstage a file:

git restore --staged <file>

Discard changes to a file:

git restore <file>

Undo the last commit (keep changes):

git reset --soft HEAD~1

Practice Exercise

Exercise 1: Configure Git

Complete these configuration steps:

  1. Set your name: git config --global user.name "Your Name"
  2. Set your email: git config --global user.email "your@email.com"
  3. Verify settings: git config --list

Exercise 2: Create Your First Repository

  1. Create a folder called git-practice
  2. Initialize it as a Git repository
  3. Create a file called notes.md
  4. Add some content to the file
  5. Stage and commit the file
  6. Check your commit with git log --oneline

Exercise 3: Connect to GitHub

  1. Create a new repository on GitHub called git-practice
  2. Connect your local repository to GitHub
  3. Push your code to GitHub
  4. Verify by visiting your GitHub repository in the browser

Exercise 4: Practice the Workflow

Make three separate commits in your repository:

  1. Add a new file called todo.txt with a list of things to learn
  2. Modify notes.md to add more content
  3. Add another file called links.md with useful resources

After each change, stage and commit with a descriptive message. Push all changes to GitHub.


Key Takeaways

  • Version control tracks changes to your files over time
  • Git is the version control software installed on your computer
  • GitHub is a cloud service for hosting Git repositories
  • Always configure Git with your name and email before using it
  • The basic workflow: editstage (git add) → commit (git commit)
  • Use git status frequently to understand what's happening
  • Push to GitHub to backup your code and share it with others
  • Write meaningful commit messages that describe your changes
  • Git has a learning curve, but it becomes second nature with practice

Resources

Resource Type Difficulty
Git Official Documentation Documentation Beginner
GitHub Docs - Get Started Documentation Beginner
Git Handbook Tutorial Beginner
Learn Git Branching Interactive Beginner
Pro Git Book Book Intermediate
GitHub Skills Interactive Beginner