Git Fundamentals

Master local Git workflow with text files: track changes, write clear commit messages, and never lose work again.

Introduction

Why Git Changes Everything

Real-world scenario: After finishing my studies at SWIC, I started an internship with the City of Columbia. I had no idea what I was doing. For every new feature, I created a new folder: project-v1, project-v2, project-final, project-ACTUALLY-final. I ended up with 40-50 copies of the same project. When something broke, I had to manually compare files to figure out what changed.

Then I learned about Git. It solved everything. One folder. Complete history. No more chaos.

This is why I teach Git from day one. Entry-level jobs require it, and it saves you from the nightmare I experienced.

The Cost of Not Using Git

Without Git:

  • Lost work: One wrong save destroys hours of effort
  • File chaos: final_v2_REALLY_FINAL_fixed.html
  • No history: Who changed what? When? Why?
  • Fear of trying new things: Scared to experiment because you might break working code

With Git:

  • Confidence: Every change is tracked and reversible
  • Professional credibility: Required for any programming job
  • Clear history: See exactly what changed and why
  • Safe experimentation: Try new ideas without breaking what works

What You'll Learn Today

  • Configure Git with your identity (do this once, done). Note: You probably already did this.
  • Create a Git repository and track changes
  • Essential Git commands: status, add, commit, log
  • Write commit messages that make sense
  • Use diff to see changes and restore to undo mistakes

What Success Looks Like

By the end of this lesson, you'll have a Git repository tracking a text file with clear commit messages. Your git log will tell the story of your work. You'll understand how to save progress and undo mistakes.

Core Concept Overview

What is Git?

Git is version control for any file. Think of it like Track Changes in Word, but designed for developers.

Think of Git like:

  • Photo album: Each commit is a snapshot of your project
  • Save game: Go back to any previous save point
  • Time machine: See your project from days or weeks ago

Key Git Concepts

Repository (Repo): A folder that Git is tracking. Contains your files plus Git's history.

Staging Area: A "waiting room" for changes before they become commits. You choose what to save together.

Commit: A snapshot of your project at a specific moment with a message describing what changed.

Think of the staging area like packing a moving box. You don't throw everything in at once—you carefully select what belongs together. All kitchen items in one box (one commit), bedroom items in another (separate commit). The staging area lets you organize related changes before saving them permanently.

The Basic Git Workflow

Working Directory → Staging Area → Repository
     (edit)          (git add)     (git commit)
  1. Edit files in your working directory
  2. Select changes with git add → moves to staging
  3. Save permanently with git commit → stored in repository

Example with a text file:

Working Directory: notes.txt has new text added
                   (modified but Git doesn't know yet)

Staging Area:      git add notes.txt
                   (change is ready to commit)

Repository:        git commit -m "Add notes about Git workflow"
                   (change is saved in history forever)

Hands-On Application

Step 1: Configure Git (Do This Once)

Run git config --global --list to see if you've already set your name and email. If you see them listed, skip to Step 2.

Before using Git, tell it who you are. This information appears in every commit.

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Why? Every commit records who made it. This is essential for team projects and your own history.

Use your school email to get the GitHub Student Developer Pack with free tools worth hundreds of dollars, including GitHub Copilot. You can always add personal emails later or update your config after graduation.

Verify your settings:

git config --global --list

You should see your name and email. Done. You never have to do this again on this computer.

Step 2: Terminal Review (Quick Refresher)

Before we start Git, let's review the commands you learned in Terminal and TXT Files:

pwd                 # Where am I?
ls                  # What's in here?
cd foldername       # Go into that folder
mkdir foldername    # Create a folder
touch filename.txt  # Create a file
code .              # Open current folder in VS Code

Remember: . means "current folder" and .. means "parent folder one level up."

Step 3: Create Your Git Project

Navigate to your Dev folder:

cd ~/Dev

Create a new project folder:

mkdir git-practice
cd git-practice

Verify you're in the right place:

pwd

Should show something like /Users/yourname/Dev/git-practice.

Step 4: Initialize Git Repository

Turn this folder into a Git repository:

git init

You should see: Initialized empty Git repository in /Users/yourname/Code/git-practice/.git/

What just happened? Git created a hidden .git folder that stores all your history.

Check with ls:

ls

You won't see anything because the folder is empty. But try this:

ls -a

Remember flags 🏴 from the terminal lesson? The -a flag means "all"—it shows hidden files (files starting with a dot). You should see .git in the list.

Your terminal prompt changed too! It now shows your branch name (usually main or master). This tells you you're inside a Git repository.

Step 5: Check Git Status

This is the command you'll use constantly:

git status

You should see:

On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)

This is Git saying: "I'm ready, but you haven't given me anything to track yet."

Step 6: Create and Track a Text File

Create a file:

touch learning.txt

Open VS Code:

code .

Click learning.txt and write a few sentences about what you're learning. Save it (Cmd + S or Ctrl + S).

Go back to your terminal and check status:

git status

You should see:

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

Git sees the file but isn't tracking it yet.

Step 7: Stage the File

Add the file to the staging area:

git add learning.txt

Important: We're adding the file by name. No shortcuts. You need to understand what you're adding.

Check status again:

git status

Now you should see:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   learning.txt

The file is in the staging area, ready to commit.

Step 8: Make Your First Commit

Save the file permanently to Git's history:

git commit -m "Add initial learning notes"

Commit message rules:

  • Use imperative tense (like giving a command). Some teams do use present tense, but at least be consistent!
  • Start with a verb: Add, Update
  • Be specific but concise

Good examples:

  • "Add initial learning notes"
  • "Update progress on Git commands"
  • "Add summary of key concepts"

Bad examples:

  • "changes" (what changes?)
  • "update" (update what?)
  • "asdfgh" (come on...)

You should see output like:

[main (root-commit) a1b2c3d] Add initial learning notes
 1 file changed, 3 insertions(+)
 create mode 100644 learning.txt

Congratulations! You made your first commit.

Step 9: View Your Commit History

See your commit:

git log

You'll see detailed output with:

  • Commit hash (long string of letters/numbers)
  • Author (you!)
  • Date
  • Commit message

The commit hash is like a unique ID for this snapshot. Git uses it to track every change.

Want a cleaner view? Try this:

git log --oneline

Remember flags 🏴? --oneline is a flag that tells log to show compact output. One commit per line with just the short hash and message.

Much easier to read when you have many commits!

Step 10: Make a Change and See the Difference

Open learning.txt in VS Code and add more text:

I am learning Git fundamentals.
Git tracks every change I make.
I can commit my work to save progress.

Save the file. Now check what changed:

git diff

You'll see output showing what you added (lines starting with + in green) and what you removed (lines starting with - in red).

This is powerful—you can see exactly what changed before committing.

Step 11: Stage and Commit the Change

Add the change:

git add learning.txt

Check what's staged:

git status

Commit it:

git commit -m "Add more details about Git workflow"

View your history:

git log --oneline

You should see 2 commits now!

Step 12: The Safety Net (Undo Mistakes)

Make a deliberate mistake. Open learning.txt and delete everything except one word:

oops

Save the file. Don't commit it.

Check what you broke:

git diff

You'll see all the red lines showing what you deleted. Panic? No need. Git has your back.

Restore the file to the last committed version:

git restore learning.txt

Open learning.txt in VS Code. Everything is back!

This is the magic of Git. As long as you committed something, you can always get it back.

Step 13: Exploring Your History

Want to see an old version of your project? Git has a command for that: git checkout <commit-hash>.

Get your commit hash from the log:

git log --oneline

You'll see something like:

b2c3d4e Add more details about Git workflow
a1b2c3d Add initial learning notes

To explore the first commit:

git checkout a1b2c3d

Git will show you a message about "detached HEAD state"—don't worry about this for now. Just know that your files are now showing the first version.

Open learning.txt in VS Code. It shows the original content!

To get back to the latest version:

git checkout main

Everything is back to the current state.

For now, just know this exists. We won't be doing much of this 'fancy time traveling' in this course, unless it becomes necessary.

Key Takeaways

Essential Git Commands

git config --global user.name "Name"    # Set your name (once)
git config --global user.email "email"  # Set your email (once)
git init                                 # Turn folder into Git repo
git status                               # Check current state (use constantly!)
git add filename                         # Stage specific file
git commit -m "message"                  # Save staged changes
git log                                  # View full commit history
git log --oneline                        # View compact history
git diff                                 # See unstaged changes
git restore filename                     # Undo uncommitted changes
git checkout <hash>                      # Explore old commit
git checkout main                        # Return to latest version

Commit Message Formula

Format: [Verb] [what you did]

Use imperative tense:

  • "Add contact form"
  • "Update README with setup instructions"
  • "added contact form" (past tense)
  • "changes" (meaningless)

The Git Workflow Loop

  1. Make changes to files
  2. git status to see what changed
  3. git diff to review changes
  4. git add filename to stage
  5. git commit -m "message" to save
  6. Repeat

Use git restore filename anytime you want to undo uncommitted changes.

What's Next

You've mastered local Git—tracking changes on your computer. Next up: GitHub—sharing your work with the world.

In the GitHub lesson, you'll learn:

  • Creating repositories on GitHub
  • Connecting local repos to GitHub
  • Pushing your commits to the cloud
  • Cloning existing projects
  • Collaborating with others

For now, keep practicing the local workflow. The better you understand add, commit, status, and log, the easier GitHub will be.

Git is not optional in professional development. You've just learned the foundation of how every developer works. Keep practicing, and this will become second nature.