Lesson 3: Basic Git Commands
What You'll Learn
In this lesson, you'll learn the three fundamental Git commands: git add, git commit, and how to check your repository's status. These are the commands you'll use most often.
The Three-Step Workflow
When working with Git, you typically follow this pattern:
- Edit your files (work on your project)
- Stage changes you want to save (
git add) - Commit those changes (
git commit)
Part 1: Staging Files with git add
The git add command moves files from your working directory to the staging area. Think of it as selecting which changes you want to include in your next snapshot.
Add a Single File
git add index.html
Add Multiple Files
git add index.html style.css
Add All Changed Files
git add .
The dot (.) means "everything in the current directory".
Check What You've Staged
git status
Files in the staging area will show as "Changes to be committed" in green.
Part 2: Committing Changes
Once you've staged your changes, you save them with git commit. Every commit needs a message describing what you changed.
Make a Commit
git commit -m "Add initial HTML structure"
The -m flag lets you add a message inline. Without it, Git opens a text editor.
Good Commit Messages
| Good Example | Bad Example | Why |
|---|---|---|
| "Add contact form validation" | "updated stuff" | Be specific about what changed |
| "Fix nav bar alignment on mobile" | "fix bug" | Describe what was fixed |
| "Remove unused CSS classes" | "changes" | Explain the action taken |
Part 3: Checking Status
The git status command is your best friend. Use it frequently to see:
- Which files have been modified
- Which files are staged
- Which files are untracked
- Which branch you're on
git status
Practice Exercise
Let's practice the complete workflow:
- Make sure you're in your
my-first-repofolder - Edit
index.html- add a paragraph - Check status:
git status - Stage the file:
git add index.html - Check status again:
git status - Commit:
git commit -m "Add welcome paragraph" - Check status one more time:
git status
Summary
Essential Commands
| Command | What It Does |
|---|---|
git add <file> |
Stage a specific file |
git add . |
Stage all changes |
git commit -m "message" |
Save staged changes with a description |
git status |
Check what's changed |
What's Next?
In the next lesson, you'll learn more about commits - how to view your commit history, write better commit messages, and understand what makes a good commit.