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)
- Go to https://git-scm.com/download/win
- The download should start automatically
- Run the installer
- 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
- 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
- Go to https://github.com
- Click "Sign up"
- Enter your email address
- Create a strong password
- Choose a username (this will be public, choose wisely!)
- Complete the verification puzzle
- Select the free plan
Step 2: Verify Your Email
Check your inbox and click the verification link from GitHub.
Step 3: Complete Your Profile
- Add a profile picture (optional but recommended)
- Write a short bio
- 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
- Go to github.com and log in
- Click the "+" icon in the top right → "New repository"
- Name it
my-first-repo(same as your local folder) - Leave it as "Public" (or choose Private)
- Don't check "Add a README file" (we already have one)
- 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
- Go to GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic)
- Click "Generate new token (classic)"
- Give it a name, select "repo" scope
- 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:
- Use HTTPS instead of SSH:
git remote set-url origin https://github.com/USERNAME/REPO.git - Set up SSH keys: GitHub SSH Guide
- 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:
- Set your name:
git config --global user.name "Your Name" - Set your email:
git config --global user.email "your@email.com" - Verify settings:
git config --list
Exercise 2: Create Your First Repository
- Create a folder called
git-practice - Initialize it as a Git repository
- Create a file called
notes.md - Add some content to the file
- Stage and commit the file
- Check your commit with
git log --oneline
Exercise 3: Connect to GitHub
- Create a new repository on GitHub called
git-practice - Connect your local repository to GitHub
- Push your code to GitHub
- Verify by visiting your GitHub repository in the browser
Exercise 4: Practice the Workflow
Make three separate commits in your repository:
- Add a new file called
todo.txtwith a list of things to learn - Modify
notes.mdto add more content - Add another file called
links.mdwith 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: edit → stage (
git add) → commit (git commit) - Use
git statusfrequently 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 |