Fatskills
Practice. Master. Repeat.
Study Guide: TECH **Git Basic Snapshotting: `git add`, `git commit`, `git status`, `git diff`**
Source: https://www.fatskills.com/web-development/chapter/tech-git-basic-snapshotting-git-add-git-commit-git-status-git-diff

TECH **Git Basic Snapshotting: `git add`, `git commit`, `git status`, `git diff`**

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~8 min read

Git Basic Snapshotting: git add, git commit, git status, git diff

A Hyper-Practical, Zero-Fluff Study Guide


1. What This Is & Why It Matters

Git’s snapshotting commands (git add, git commit, git status, git diff) are the atomic unit of version control. They let you: - Capture changes to your codebase at a point in time.
- Inspect what’s changed, staged, or untracked.
- Debug regressions by comparing versions.
- Collaborate safely by sharing only intentional changes.

Why this matters in production:
- You’re a DevOps engineer and a deployment fails. You need to roll back to the last known good commit—but only if you’ve been committing properly.
- You inherit a legacy codebase with no commit history. You must reconstruct what changed between versions to debug a critical bug.
- You’re working in a team and someone breaks the build. git diff and git status help you identify exactly what changed before merging.
- You’re preparing for a certification (e.g., GitHub Actions, AWS DevOps Pro) and need to explain how Git tracks changes in CI/CD pipelines.

If you ignore snapshotting:
- You lose the ability to undo mistakes (no git reset, no git revert).
- You pollute the commit history with half-baked changes, making debugging a nightmare.
- You accidentally commit secrets (API keys, passwords) because you didn’t check git status before committing.


2. Core Concepts & Components

Term Definition Production Insight
Working Directory The files on your local machine (not yet tracked by Git). ⚠️ Changes here aren’t backed up—if your laptop dies, they’re gone.
Staging Area (Index) A "preview" of what will be committed (git add moves changes here). Use this to split logical changes (e.g., "fix bug" vs. "update docs") into separate commits.
Repository (Repo) The .git folder where Git stores all commits, branches, and history. Never manually edit .git/—you’ll corrupt the repo.
git status Shows the state of your working directory and staging area. Always run this before committing—it prevents accidental commits of secrets or debug code.
git diff Shows unstaged changes (working dir vs. staging area). Use git diff --staged to see what’s about to be committed.
git add Moves changes from the working directory to the staging area. ⚠️ git add . stages everything, including untracked files (use git add -p to review changes interactively).
git commit Permanently records staged changes in the repo. Bad commits = bad history. Write clear, atomic messages (e.g., "Fix login API 500 error" vs. "WIP").
Commit Hash (SHA-1) A unique 40-character ID for each commit (e.g., a1b2c3d). The first 7 chars (a1b2c3d) are enough for most commands (e.g., git show a1b2c3d).
HEAD A pointer to the latest commit in the current branch. git reset HEAD~1 undoes the last commit (but keeps changes in working dir).


3. Step-by-Step Hands-On: Your First Snapshots


Prerequisites

  • Git installed (git --version should work).
  • A terminal (Bash, Zsh, or Git Bash on Windows).
  • A project to track (or create one with mkdir my-project && cd my-project).


Task: Track a Python Project with Git

Goal: Initialize a repo, make changes, stage them, commit, and inspect differences.


Step 1: Initialize a Git Repo

mkdir git-snapshotting-demo && cd git-snapshotting-demo
git init

Expected Output:


Initialized empty Git repository in /path/to/git-snapshotting-demo/.git/

Step 2: Create a File and Check git status

echo 'print("Hello, Git!")' > app.py
git status

Expected Output:


On branch main
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
app.py nothing added to commit but untracked files present (use "git add" to track)

Key Takeaway:
- app.py is untracked (Git sees it but isn’t tracking changes yet).


Step 3: Stage the File (git add)

git add app.py
git status

Expected Output:


On branch main
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
new file: app.py

Key Takeaway:
- app.py is now staged (ready to commit).


Step 4: Commit the Change (git commit)

git commit -m "Add initial Python script"

Expected Output:


[main (root-commit) a1b2c3d] Add initial Python script
 1 file changed, 1 insertion(+)
 create mode 100644 app.py

Key Takeaway:
- The commit is now permanently stored in Git’s history.
- The hash (a1b2c3d) uniquely identifies this commit.


Step 5: Modify the File and Check git diff

echo 'print("Goodbye, Git!")' >> app.py
git diff

Expected Output:


diff --git a/app.py b/app.py
index 8a0f0f4..e69de29 100644
--- a/app.py
+++ b/app.py
@@ -1 +1,2 @@
 print("Hello, Git!")
+print("Goodbye, Git!")

Key Takeaway:
- git diff shows unstaged changes (the new line isn’t staged yet).


Step 6: Stage and Commit the Change

git add app.py
git commit -m "Add farewell message"

Expected Output:


[main 4e5f6g7] Add farewell message
 1 file changed, 1 insertion(+)

Step 7: View Commit History (git log)

git log --oneline

Expected Output:


4e5f6g7 (HEAD -> main) Add farewell message
a1b2c3d Add initial Python script

Key Takeaway:
- HEAD points to the latest commit (4e5f6g7).
- --oneline shows a compact history.


Step 8: Compare Commits (git diff Between Commits)

git diff a1b2c3d 4e5f6g7

Expected Output:


diff --git a/app.py b/app.py
index 8a0f0f4..e69de29 100644
--- a/app.py
+++ b/app.py
@@ -1 +1,2 @@
 print("Hello, Git!")
+print("Goodbye, Git!")

Key Takeaway:
- This shows what changed between two commits.


4. ? Production-Ready Best Practices


Security

  • Never commit secrets (API keys, passwords, .env files).
  • Use .gitignore to exclude sensitive files:
    bash
    echo ".env" >> .gitignore
    echo "*.pem" >> .gitignore
  • Pre-commit hook: Use git-secrets (AWS) or pre-commit to scan for secrets before committing.

Cost Optimization (Yes, Git Affects Costs!)

  • Small, frequent commits reduce merge conflicts (saving dev time).
  • Atomic commits (one logical change per commit) make git bisect (debugging) faster.
  • Avoid binary files (e.g., node_modules/, .zip)—they bloat the repo. Use .gitignore.

Reliability & Maintainability

  • Write clear commit messages:
  • Bad: fix bug
  • Good: Fix 500 error in /login endpoint (missing user validation)
  • Template:
    <type>(<scope>): <subject>
    <BLANK LINE>
    <body>

    Example:
    ```
    feat(auth): add OAuth2 login
    • Integrate Google OAuth2 provider
    • Add /auth/google endpoint ```
  • Use git add -p to stage changes interactively (avoids accidental commits).
  • Tag important commits (e.g., git tag v1.0.0).

Observability

  • Monitor repo size: git count-objects -vH (large repos slow down operations).
  • Check for large files: git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | awk '/^blob/ {print substr($0,6)}' | sort --numeric-sort --key=2 | tail -n 10
  • Use git blame to find who last modified a line (e.g., git blame app.py).


5. ⚠️ Common Mistakes & Traps

Mistake Symptom Fix/Prevention
Committing everything with git add . Accidentally staging node_modules/, .env, or debug code. Use git add -p to review changes before staging.
Writing vague commit messages Can’t find which commit introduced a bug. Use the <type>(<scope>): <subject> format.
Not checking git status before committing Committing secrets or temporary files. Always run git status first.
Committing large binary files Repo becomes slow and bloated. Use .gitignore and Git LFS for large files.
Using git commit -a (stages all tracked files) Stages changes you didn’t intend to commit. Avoid -a—stage files manually.


6. ? Exam/Certification Focus


Typical Question Patterns

  1. "What does git add do?"
  2. ❌ "It commits changes."
  3. "It stages changes for the next commit."

  4. "How do you see unstaged changes?"

  5. git status
  6. git diff

  7. "What’s the difference between git diff and git diff --staged?"

  8. git diff = unstaged changes (working dir vs. staging).
  9. git diff --staged = staged changes (staging vs. last commit).

  10. "How do you undo a git add?"

  11. git reset <file> (unstages the file).
  12. git commit --amend (that’s for commits).

  13. "What’s the default editor for git commit if no -m is provided?"

  14. Vim (or whatever core.editor is set to).
  15. ⚠️ Trap: If you don’t know Vim, you’ll get stuck in the editor.

Key Trap Distinctions

Command What It Does Trap
git add . Stages all changes (including untracked files). ⚠️ Can accidentally stage secrets.
git add -u Stages modified/deleted files (ignores untracked). ⚠️ Won’t stage new files.
git commit -a Stages all tracked files and commits. ⚠️ Skips the staging area entirely.


7. ? Hands-On Challenge


Challenge: Fix a Botched Commit

Scenario:
You ran git add . and committed, but you accidentally included a debug.log file with sensitive data. Undo the commit, remove the file, and recommit properly.

Solution:


# Undo the last commit (keeps changes in working dir)
git reset HEAD~1

# Remove debug.log from staging (if it was staged)
git reset debug.log

# Add debug.log to .gitignore
echo "debug.log" >> .gitignore

# Stage and commit the correct files
git add .gitignore app.py
git commit -m "Add .gitignore and update app"

Why It Works:
- git reset HEAD~1 undoes the commit but keeps changes in the working directory.
- git reset debug.log unstages the file.
- .gitignore ensures debug.log is never tracked again.


8. ? Rapid-Reference Crib Sheet

Command Purpose Key Flags
git status Show staged/unstaged changes. -s (short format)
git diff Show unstaged changes. --staged (staged changes)
git add <file> Stage a file. -p (interactive staging)
git commit Commit staged changes. -m "message" (inline message)
git log Show commit history. --oneline (compact)
git reset <file> Unstage a file. --hard (⚠️ DANGER: deletes changes)
git rm --cached <file> Stop tracking a file (keeps it on disk).
git show <commit> Show changes in a commit.

⚠️ Exam Traps:
- git commit -a skips the staging area (commits all tracked changes).
- git reset --hard deletes uncommitted changes permanently.
- git diff only shows unstaged changes (use --staged for staged changes).


9. ? Where to Go Next

  1. Git Official Docs - Basic Snapshotting
  2. GitHub’s Git Handbook
  3. Atomic Commits (ThoughtBot)
  4. Git Flight Rules (Common Problems & Fixes)

Final Pro Tip:


"A good commit is like a good tweet: short, clear, and useful. A bad commit is like a rambling email—no one reads it, and it clutters history."


Now go commit something useful! ?



ADVERTISEMENT