Chapter 1.4โ˜• 14 min read

git status, log & diff

Inke bina tum andhe ho โ€” Git mein kuch bhi karo, pehle yeh teen commands chalao.

01git status โ€” Your Daily Compass

git status is the MOST used Git command. Period. Run it before and after every operation โ€” it tells you exactly where you stand.

git status shows: which branch you are on, whether it is up to date with the remote, and the state of every file in your working directory.

The long form gives descriptive text with categories. The short form (git status -s) gives a compact two-column format that experienced developers prefer.

Short format columns: LEFT = staging area status, RIGHT = working directory status.

Key codes to memorize:

  • ?? = untracked (Git does not know about this file yet)
  • A = added to staging (new file, staged)
  • M = modified (content changed)
  • D = deleted
  • R = renamed
  • MM = staged AND modified โ€” the dual state from Chapter 1.3

Always run git status BEFORE committing to verify you are committing what you think you are committing.

# Full status output
git status
# On branch main
# Changes to be committed:
#   modified:   app.js        (staged, green)
# Changes not staged for commit:
#   modified:   style.css     (modified, red)
# Untracked files:
#   new-feature.js            (untracked, red)

# Short status โ€” the pro way
git status -s
# M  app.js         (left M = staged)
#  M style.css      (right M = modified, not staged)
# ?? new-feature.js  (untracked)
# MM config.js      (staged AND modified โ€” dual state!)
# A  utils.js       (newly added to staging)
# D  old-file.js    (deleted from working dir)
02git log โ€” The History Book

git log shows the commit history โ€” who changed what, when, and why. It is your project's history book.

Basic git log is overwhelming โ€” too much information per commit. Use flags to customize the output.

git log --oneline โ€” one line per commit: short hash + message. The most useful format for daily work.

git log --oneline -10 โ€” last 10 commits only. Perfect for quick checks.

git log --oneline --graph --all โ€” visual branch graph. THE GOD COMMAND for understanding your repository structure.

Useful filters:

  • git log --author="Sai" โ€” filter by author
  • git log --since="2 weeks ago" โ€” filter by date
  • git log --grep="feat" โ€” filter by commit message pattern
  • git log -p โ€” show the actual diff with each commit (detailed but long)
  • git log --stat โ€” show which files changed and how many lines (summary view)
# Basic log โ€” too verbose
git log
# commit a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
# Author: Sai Kumar <sai@devinhyderabad.com>
# Date:   Mon Jan 6 10:00:00 2025 +0530
#
#     feat: add login page

# Oneline โ€” the daily driver
git log --oneline
# a94a8fe feat: add login page
# b3c4d56 fix: handle null response
# e7f8g90 chore: update dependencies

# THE GOD COMMAND โ€” visualize branches
git log --oneline --graph --all
# * a94a8fe (HEAD -> main) feat: add login page
# * b3c4d56 fix: handle null response
# | * e7f8g90 (feature/auth) feat: auth module
# |/
# * 1a2b3c4 initial commit

# Useful filters
git log --oneline -5              # last 5 commits
git log --author="Sai"            # Sai's commits
git log --since="yesterday"       # recent commits
git log --grep="fix"              # commits with "fix"
git log -- app.js                 # commits touching app.js
๐Ÿ’ก Pro Tip: Set up this alias and use it daily: git config --global alias.lg 'log --oneline --graph --all'. Then just type git lg for a beautiful visual history. This single alias will save you thousands of keystrokes over your career.
03git diff โ€” The Surgical Instrument

git diff shows the EXACT changes โ€” which lines were added, removed, or modified. It is the surgical instrument of Git.

Without flags: compares working directory vs staging area (unstaged changes only).

git diff --staged (or --cached): compares staging area vs last commit (staged changes โ€” what will be in your next commit).

git diff HEAD: compares working directory vs last commit (all changes, staged or not).

git diff branch1..branch2: compare two branches.

git diff commit1..commit2: compare two specific commits.

Output format: - lines (red) = removed, + lines (green) = added. Lines starting with @@ show the location (hunk header).

git diff --stat: summary of changes without the full diff โ€” shows files changed, insertions, deletions.

# What have I changed but not staged?
git diff
# Shows: working directory vs staging area

# What have I staged for the next commit?
git diff --staged
# Shows: staging area vs repository

# All changes from last commit (staged + unstaged)
git diff HEAD

# Compare two branches
git diff main..feature
# Shows: what's different between main and feature

# Compare two specific commits
git diff HEAD~3..HEAD
# Shows: changes in the last 3 commits

# Summary view โ€” just file names and stats
git diff --stat
# app.js      | 5 +++--
# style.css   | 2 +-
# 2 files changed, 3 insertions(+), 4 deletions(-)

# Diff a single file
git diff app.js
git diff --staged app.js
04Real-World Workflow โ€” All Three Together

Real-world workflow: these three commands work TOGETHER to answer questions about your repository. This is the Investigator Workflow.

Every question about your code has an answer in one of these three commands. The trick is knowing which one to use:

  • "What did I change today?" โ†’ git diff HEAD or git log --since="today" -p
  • "What will be in my next commit?" โ†’ git diff --staged
  • "Who broke the build?" โ†’ git log --oneline -10 then git show <hash>
  • "What changed in this file?" โ†’ git log -- app.js then git diff commit1..commit2 -- app.js
  • "Am I on the right branch?" โ†’ git status (first line)
  • "Did I forget to stage anything?" โ†’ git status -s

Pro tip: always run git status + git diff --staged BEFORE every commit. This catches mistakes before they enter history.

# The Investigator Workflow โ€” real scenarios

# Scenario 1: "What will I commit?"
git status -s          # see which files changed
git diff --staged      # review staged changes
# THEN commit if everything looks right

# Scenario 2: "What did I change today?"
git log --since="8am" --oneline --author="$(git config user.name)"
git diff HEAD@{morning}..HEAD  # if you have a ref

# Scenario 3: "Who broke the login page?"
git log --oneline -10 -- login.js   # who touched login.js?
git show <suspect-hash>              # inspect that commit
git blame login.js                    # line-by-line ownership

# Scenario 4: "What's different between my branch and main?"
git log --oneline main..feature  # commits on feature not on main
git diff main..feature --stat    # files changed summary
git diff main..feature           # full diff

# Pre-commit checklist (DO THIS EVERY TIME)
git status -s       # 1. What changed?
git diff --staged   # 2. Review staged changes
git log --oneline -3 # 3. Where am I in history?
05Power Tricks โ€” Custom Format & Pickaxe

Once you are comfortable with the basics, these power tricks make you a Git detective โ€” able to find any change in any codebase.

Custom log format: git log --pretty=format:"%h - %an, %ar : %s" โ€” complete control over output.

Format placeholders:

  • %h โ€” short hash
  • %H โ€” full hash
  • %an โ€” author name
  • %ar โ€” relative date ("2 hours ago")
  • %s โ€” subject/commit message

git log -S "functionName" โ€” pickaxe search: find commits that added or removed a specific string. Incredibly powerful for tracking down when a bug was introduced.

git log -G "regex" โ€” pickaxe with regex support.

git diff --word-diff โ€” show word-level changes instead of line-level (useful for documentation and prose).

git diff --check โ€” warn about whitespace errors (trailing spaces, missing newline at EOF).

git diff --color-words โ€” color changed words within lines for easier reading.

# Custom log format
git log --pretty=format:"%h | %an | %s" -5
# a94a8fe | Sai Kumar | feat: add login page
# b3c4d56 | Sai Kumar | fix: handle null response

# Find when a function was introduced
git log -S "authenticate" --oneline
# e7f8g90 feat: add auth module

# Find when a function was removed
git log -S "oldFunction" --diff-filter=D --oneline
# b3c4d56 refactored: removed old authentication

# Word-level diff for documentation
git diff --word-diff README.md
# [-old instructions-]{+new instructions+}

# Check for whitespace errors
git diff --check
# README.md:5: trailing whitespace.
# app.js:12: new blank line at EOF.

# Color words โ€” changes within lines
git diff --color-words app.js
# old code new code (red/green inline)

# The ULTIMATE log alias
git config --global alias.lg "log --oneline --graph --all --decorate"
git lg  # beautiful, colorful, informative history
The pickaxe (-S flag) is your most powerful debugging tool. When a bug appears, use git log -S "buggyFunction" to find the EXACT commit that introduced or removed it. This is far more precise than reading commit messages. Combined with git show <hash>, you can trace any change to its source in seconds.

Lo kar liya โ€” Key Points:

  • โœ… git status is your compass โ€” run it before and after every Git operation to know the state of your files
  • โœ… git status -s gives compact two-column output: left = staging, right = working directory
  • โœ… git log --oneline shows one-line-per-commit history โ€” add --graph --all for visual branch structure
  • โœ… git diff shows unstaged changes (working vs staging), git diff --staged shows staged changes (staging vs repo)
  • โœ… The investigator workflow: git status โ†’ git diff --staged โ†’ git log โ€” always review before committing
  • โœ… Custom log format with --pretty=format gives complete control over output appearance
  • โœ… Pickaxe search git log -S "string" finds when specific text was added or removed in the codebase
Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase