🔍 Daily Git Workflow: Inspecting History
The Time Machine Analogy
Imagine you have a magical diary that remembers everything you ever wrote. Every word, every change, every little doodle. And the best part? You can go back and read any page from any day!
Git is like that magical diary for your code. It remembers every change you’ve ever made. Today, we’ll learn how to read this diary and discover who wrote what, when, and why.
🎯 What You’ll Learn
Think of yourself as a detective. Your job is to investigate the history of your code. You have special tools:
- git log - Your magnifying glass to see all past entries
- git log with filters - Your search flashlight to find specific things
- git diff - Your comparison glasses to spot differences
- git show - Your x-ray vision to see inside any change
- git blame - Your truth detector to find who wrote each line
Let’s explore each one!
đź“– Viewing Commit History with git log
The Story
Remember your magical diary? The git log command is like flipping through its pages from newest to oldest.
What It Shows You
Every time someone saves their work (makes a “commit”), Git writes down:
- Who made the change
- When they made it
- What they said about it (the message)
- A special code (hash) to identify it
Try It Yourself
git log
You’ll see something like:
commit a1b2c3d (HEAD -> main)
Author: Alex <alex@email.com>
Date: Mon Nov 25 10:30:00 2024
Add login button to homepage
commit e4f5g6h
Author: Sam <sam@email.com>
Date: Sun Nov 24 15:45:00 2024
Fix typo in welcome message
Making It Easier to Read
Sometimes the diary is too long! Use this shortcut:
git log --oneline
Now each entry takes just ONE line:
a1b2c3d Add login button to homepage
e4f5g6h Fix typo in welcome message
d7e8f9g Create homepage layout
Seeing the Story Flow
graph TD A[a1b2c3d - Add login button] --> B[e4f5g6h - Fix typo] B --> C[d7e8f9g - Create homepage] C --> D[older commits...]
🔦 Git Log Filtering
The Story
What if your diary has 1000 pages? You need a flashlight to find exactly what you’re looking for!
Find Changes by a Person
Want to see only what Alex wrote?
git log --author="Alex"
Find Changes from a Time Period
What happened last week?
git log --since="7 days ago"
What happened in November?
git log --since="2024-11-01" --until="2024-11-30"
Find Changes with Specific Words
Looking for anything about “login”?
git log --grep="login"
See Only Files That Changed
Want to know which files were touched?
git log --name-only
Combine Filters Like a Pro
git log --author="Alex" --since="1 week ago" --oneline
This shows: Alex’s changes from the past week, one per line.
Quick Reference
| I want to find… | Command |
|---|---|
| Alex’s commits | git log --author="Alex" |
| Last 5 commits | git log -5 |
| This week’s work | git log --since="7 days ago" |
| Mentions of “bug” | git log --grep="bug" |
| Changes to one file | git log -- filename.js |
🔬 Comparing Changes with git diff
The Story
Imagine you have two photos of your room. One from yesterday, one from today. git diff is like a special app that shows you exactly what changed between them!
See What You Changed (But Haven’t Saved Yet)
git diff
This shows changes you made but haven’t committed yet.
What the Output Means
- old line that was removed
+ new line that was added
Red with minus (-) = This line was deleted Green with plus (+) = This line was added
Compare Two Specific Commits
git diff a1b2c3d e4f5g6h
This compares commit a1b2c3d with commit e4f5g6h.
Compare a File Between Commits
git diff a1b2c3d e4f5g6h -- style.css
Just shows changes to style.css between those commits.
See What’s Ready to Save
git diff --staged
Shows changes you’ve added (staged) but not committed yet.
Visual Understanding
graph TD A[Your Working Files] -->|git diff| B[Last Saved State] C[Commit A] -->|git diff A B| D[Commit B] E[Staged Changes] -->|git diff --staged| F[Last Commit]
🔎 Showing Commit Details with git show
The Story
You found an interesting page in your diary. Now you want to read EVERYTHING on that page - not just the title, but every word!
How to Use It
git show a1b2c3d
Replace a1b2c3d with any commit hash (that special code).
What You’ll See
- The commit info (who, when, message)
- The actual changes (what lines were added/removed)
See a Specific File in a Commit
git show a1b2c3d:index.html
This shows what index.html looked like at that exact moment in time!
See Just the Files That Changed
git show --stat a1b2c3d
Quick summary of which files changed and how much.
Example Output
commit a1b2c3d
Author: Alex <alex@email.com>
Date: Mon Nov 25 10:30:00 2024
Add login button to homepage
index.html | 10 ++++++++--
style.css | 5 +++++
2 files changed, 13 insertions(+), 2 deletions(-)
🕵️ Using git blame
The Story
Someone wrote a strange line of code. You’re curious: “Who wrote this? When? And why?”
git blame is like a truth detector. It tells you exactly who wrote each line!
How to Use It
git blame filename.js
What You’ll See
Every line of the file with:
- Who wrote it
- When they wrote it
- Which commit it came from
Example Output
a1b2c3d (Alex 2024-11-25) function login() {
e4f5g6h (Sam 2024-11-24) console.log("Hello");
d7e8f9g (Alex 2024-11-23) return true;
a1b2c3d (Alex 2024-11-25) }
Now you know:
- Alex wrote the function and the return
- Sam added the console.log
Blame a Specific Section
Only want lines 10-20?
git blame -L 10,20 filename.js
Why “Blame”?
Don’t worry! It’s not about blaming anyone. It’s about understanding who to ask when you have questions about that code.
Think of it as “git credit” - finding who deserves credit for writing each line!
🎮 Real-World Detective Story
Let’s put it all together with a story:
The Mystery: The homepage is broken. Something changed yesterday.
Your Investigation:
-
Check recent history:
git log --since="yesterday" --onelineFound 3 commits!
-
Who made them?
git log --since="yesterday" --format="%an - %s"Shows author names and messages.
-
What changed in index.html?
git log -- index.html --onelineFound the suspicious commit:
x1y2z3 -
See the full changes:
git show x1y2z3Aha! Someone removed an important line!
-
Who wrote the original line?
git blame index.html | grep "button"Mystery solved!
📝 Quick Summary
| Tool | What It Does | Simple Example |
|---|---|---|
git log |
Shows all commits | git log --oneline |
git log --author |
Filter by person | git log --author="Alex" |
git log --since |
Filter by time | git log --since="yesterday" |
git log --grep |
Filter by message | git log --grep="fix" |
git diff |
Compare changes | git diff |
git show |
See commit details | git show a1b2c3d |
git blame |
Who wrote each line | git blame file.js |
🌟 You’re Now a Git Detective!
Remember:
- git log = Your diary of all changes
- Filters = Your flashlight to find specific things
- git diff = Your comparison tool
- git show = Your deep-dive inspector
- git blame = Your truth detector
You have all the tools to investigate any mystery in your code history. Go explore!