Foundations · lesson 3 of 18

Git Fundamentals

init, add, commit, status, log

Why it matters

These five commands cover 80% of daily Git usage. Master them and you’ll handle most version control tasks with confidence.

Key concepts

The idea

The Checkpoint Analogy

Think of Git like a video game’s save system:

Unlike video games, Git keeps every save point forever. You can always go back.

The Workflow

1. Edit files (work in your editor)
       ↓
2. git status (see what changed)
       ↓
3. git add <files> (stage changes)
       ↓
4. git commit -m "message" (create checkpoint)
       ↓
5. Repeat

Walkthrough

Setup (One-Time)

# Install Git
sudo apt install git          # Ubuntu/Debian
brew install git              # macOS

# Configure identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Optional: set default branch name
git config --global init.defaultBranch main

Starting a Repository

Every repository — new or cloned — is a full Git database living in a .git directory.
# Create new repository
git init

# Or clone an existing one
git clone https://github.com/user/repo.git

The Daily Workflow

# Check current status
git status

# Stage specific files
git add file.txt
git add src/

# Stage all changes
git add .

# Commit with message
git commit -m "Add login feature"

# View commit history
git log
git log --oneline        # Compact view
git log --graph          # Show branch structure

Understanding Status Output

On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   staged_file.txt     ← Ready to commit

Changes not staged for commit:
        modified:   unstaged_file.txt   ← Changed but not staged

Untracked files:
        new_file.txt                    ← Git doesn't know about this

Key takeaways

Dos & don’ts

✅ DO

❌ DON’T

Going deeper

The .git folder: Everything Git knows lives in .git/. The objects/ folder stores all file content (as compressed blobs). The refs/ folder stores branch pointers. HEAD is a file pointing to your current branch.

Staging area internals: The staging area (index) is a binary file at .git/index. It’s a snapshot of what your next commit will look like. This two-step process (stage then commit) gives you control over what goes into each commit.

Common mistakes

Committing without staging: git commit -m "message" only commits staged changes. If you didn’t git add, nothing is committed. Use git commit -am "message" to add+commit tracked files (but not new files).

Forgetting to init: “fatal: not a git repository” means you’re not in a Git repo. Run git init or navigate to the right folder.