NeuroAgent

View Staged Changes in Git: Complete Guide

Learn how to view all staged changes in Git using git diff --staged or git diff --cached. Complete guide with examples and advanced options for better version control workflow.

Question

How do I view the diffs of all staged changes in Git?

I’ve staged several changes for commit and want to see the actual differences between my staged files and their previous versions. While git status only displays the names of staged files, I need to see the content changes. Is there a simple one-liner command to show all staged diffs?

The Git documentation mentions that git diff can view changes relative to the index (staging area), but I’m not sure how to apply this specifically to see only the staged changes.

NeuroAgent

To view all staged changes in Git, use git diff --staged or git diff --cached. This command shows the differences between what’s in your staging area and your last commit, displaying exactly what will be included in your next commit. Both commands are equivalent and provide a comprehensive view of all your staged changes.

Contents

The Basic Command

The most straightforward way to view all staged changes is using either git diff --staged or git diff --cached. These commands are syntactically identical and do exactly the same thing:

bash
git diff --staged
git diff --cached

Both commands show the differences between your staging area (index) and the last commit. This is perfect for reviewing what you’re about to commit before you actually commit it.

Key Point: The --staged flag was introduced in Git 1.6.1, while --cached has been available since earlier versions. Both are interchangeable and you can use whichever you prefer.

Understanding the Staging Area

Before diving deeper, it’s important to understand how Git’s staging area works:

  1. Working Directory: Your actual files on disk
  2. Staging Area (Index): What you’ve prepared for the next commit
  3. HEAD: The last commit you’ve made

When you run git add, you’re moving changes from the working directory to the staging area. When you run git commit, you’re moving changes from the staging area to the repository.

Git documentation explains that git diff without arguments compares the working directory with the staging area, while git diff --staged compares the staging area with the last commit.

Comparing with Unstaged Changes

You can easily compare the different types of changes Git tracks:

Command Compares Shows
git diff Working directory vs Staging area Unstaged changes
git diff --staged Staging area vs Last commit Staged changes
git diff HEAD Working directory vs Last commit All changes (unstaged + staged)

This gives you a complete picture of your changes:

bash
# See what you're about to commit
git diff --staged

# See what you've changed but not staged
git diff

# See all changes relative to last commit
git diff HEAD

Advanced Options for Staged Diffs

You can enhance your staged diff view with several useful options:

Color Output

bash
git diff --staged --color=always

Show Context Lines

bash
git diff --staged --unified=5  # Show 5 lines of context

Word-Level Diff

bash
git diff --staged --word-diff

Only Show Changed Files

bash
git diff --staged --name-only

Combine with Other Options

bash
git diff --staged --stat  # Show statistics instead of full diff
git diff --staged --ignore-space-change  # Ignore whitespace changes

Practical Examples

Example 1: Review Before Committing

bash
# Stage some changes
git add file1.txt file2.py

# Review staged changes
git diff --staged

# If satisfied, commit
git commit -m "Update files"

Example 2: Interactive Staging

bash
# See what you've changed
git diff

# Stage specific parts of changes
git add -p

# Review what's staged
git diff --staged

# Continue staging more if needed
git add another_file.txt

# Final review
git diff --staged

Example 3: Multiple File Diff

bash
# Stage changes across multiple files
git add *.js

# See all staged JavaScript changes
git diff --staged -- '*.js'

Common Use Cases

Pre-Commit Review

Always run git diff --staged before committing to ensure you’re committing exactly what you intend:

bash
git status
git diff --staged
git commit -m "Message"

Partial Commits

When working on multiple features in the same branch, use staged diffs to create focused commits:

bash
# Stage feature 1 changes
git add feature1/*

# Review and commit feature 1
git diff --staged
git commit -m "Implement feature 1"

# Stage feature 2 changes
git add feature2/*

# Review and commit feature 2
git diff --staged
git commit -m "Implement feature 2"

Collaborative Development

When working with others, use staged diffs to verify your changes before pushing:

bash
git add .
git diff --staged
git commit
git push

Conclusion

  • Use git diff --staged or git diff --cached to view all your staged changes before committing
  • This command shows exactly what will be included in your next commit
  • Combine with other options like --stat, --color, or --word-diff for different viewing preferences
  • Make it a habit to review staged diffs before committing to ensure clean, focused commits
  • The staging area workflow allows for precise control over what goes into each commit

By mastering the git diff --staged command, you gain better control over your version control workflow and can create more intentional, well-organized commits.

Sources

  1. Official Git Documentation - git-diff
  2. Pro Git Book - Viewing the Staged and Unstaged Changes
  3. Atlassian Git Tutorial - The Staging Area