Bird
Raised Fist0
Gitdevops~20 mins

git diff --staged for staged changes - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Git Diff Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What does git diff --staged show?
You have modified a file and then added it to the staging area using git add. What will git diff --staged display?
Git
echo 'Hello' > file.txt
git add file.txt
# Now run: git diff --staged
AShows the differences between the working directory and the last commit.
BShows the differences between the last commit and the staged changes.
CShows the differences between the working directory and the staged changes.
DShows the differences between the staging area and the remote repository.
Attempts:
2 left
💡 Hint
Think about what 'staged' means in Git and what you want to compare.
🧠 Conceptual
intermediate
1:30remaining
Why use git diff --staged before committing?
Which reason best explains why a developer should run git diff --staged before making a commit?
ATo reset the staging area to the last commit.
BTo see all untracked files in the project directory.
CTo compare the current branch with the remote branch.
DTo verify exactly what changes are staged and will be included in the commit.
Attempts:
2 left
💡 Hint
Think about what you want to check before saving your changes permanently.
Troubleshoot
advanced
2:00remaining
Why does git diff --staged show no output after staging changes?
You modified a file and ran git add to stage it. But when you run git diff --staged, nothing is shown. What is the most likely reason?
AYou staged the file but then modified it again without adding the new changes.
BThe file was never added to the staging area.
CYou are running the command in a different repository.
DThe file is ignored by .gitignore.
Attempts:
2 left
💡 Hint
Consider whether git add actually staged any changes (e.g., no differences from HEAD or ignored file).
🔀 Workflow
advanced
2:00remaining
Order the steps to review and commit staged changes using git diff --staged
Put these steps in the correct order to safely commit changes after staging them.
A4,1,2,3
B1,4,2,3
C4,2,1,3
D1,2,4,3
Attempts:
2 left
💡 Hint
Think about the natural flow from editing to committing.
Best Practice
expert
2:00remaining
Which command combination ensures you only commit staged changes and not unstaged modifications?
You have some changes staged and some unstaged. Which command sequence guarantees that only staged changes are committed?
Agit commit -m 'message' && git diff --staged
Bgit diff --staged && git commit
Cgit commit -m 'message'
Dgit commit --only -m 'message'
Attempts:
2 left
💡 Hint
By default, what does git commit include?

Practice

(1/5)
1. What does the command git diff --staged show?
easy
A. Changes that are staged and ready to be committed
B. All changes in the working directory, staged or not
C. The commit history of the repository
D. Untracked files in the repository

Solution

  1. Step 1: Understand what staging means in Git

    Staging means preparing changes to be saved in the next commit.
  2. Step 2: Identify what git diff --staged compares

    This command compares the staged changes against the last commit, showing what will be committed.
  3. Final Answer:

    Changes that are staged and ready to be committed -> Option A
  4. Quick Check:

    Staged changes = git diff --staged output [OK]
Hint: Remember: --staged shows only prepared changes [OK]
Common Mistakes:
  • Confusing staged changes with all changes
  • Thinking it shows commit history
  • Assuming it lists untracked files
2. Which of the following is the correct syntax to view staged changes using git?
easy
A. git diff staged
B. git diff --cached
C. git diff --stage
D. git diff --status

Solution

  1. Step 1: Recall git diff options for staged changes

    Git uses --cached as the official option to show staged changes.
  2. Step 2: Understand that --staged is an alias

    --staged is a common alias but --cached is the correct and original syntax.
  3. Final Answer:

    git diff --cached -> Option B
  4. Quick Check:

    Correct syntax for staged diff = git diff --cached [OK]
Hint: Use --cached to view staged changes reliably [OK]
Common Mistakes:
  • Using incorrect flags like --stage
  • Omitting the double dash before options
  • Confusing staged with unstaged flags
3. Given the following commands executed in order:
echo 'Hello' > file.txt
git add file.txt
git diff --staged
What will git diff --staged display?
medium
A. No output, because the file is new and staged
B. An error because the file is not committed yet
C. The difference showing removal of 'Hello' in file.txt
D. The difference showing the addition of 'Hello' in file.txt

Solution

  1. Step 1: Understand the state of file.txt after commands

    The file is new with content 'Hello' and has been staged with git add.
  2. Step 2: What does git diff --staged show here?

    It shows the difference between the staged version and the last commit (which has no file.txt), so it shows the addition of 'Hello'.
  3. Final Answer:

    The difference showing the addition of 'Hello' in file.txt -> Option D
  4. Quick Check:

    New staged file diff shows added content [OK]
Hint: New staged files show additions in git diff --staged [OK]
Common Mistakes:
  • Expecting no output for new files
  • Thinking git diff --staged errors on new files
  • Confusing removal with addition
4. You modified files in your editor, staged them with git add, ran git diff --staged but saw no output. What could be the problem?
medium
A. You staged the files but forgot to save changes in the editor
B. You used git diff instead of git diff --staged
C. You committed the changes already, so no staged changes remain
D. The repository has no commits yet

Solution

  1. Step 1: Check if file changes are saved before staging

    If changes are not saved in the editor, staging old content means no visible diff.
  2. Step 2: Understand why no output appears

    Since staged content matches last commit (or is empty), git diff --staged shows nothing.
  3. Final Answer:

    You staged the files but forgot to save changes in the editor -> Option A
  4. Quick Check:

    Unsaved edits cause empty staged diff [OK]
Hint: Always save files before staging to see diffs [OK]
Common Mistakes:
  • Assuming git diff --staged shows unstaged changes
  • Not realizing files were not saved
  • Thinking commit status affects staged diff output
5. You have staged changes in two files: app.js and index.html. You want to see only the staged changes in app.js. Which command should you use?
hard
A. git diff --cached index.html
B. git diff app.js staged
C. git diff --staged app.js
D. git diff --staged --name-only app.js

Solution

  1. Step 1: Understand how to limit git diff to a specific file

    You can specify the file path after the options to filter the diff output.
  2. Step 2: Choose the correct syntax for staged changes and file filter

    git diff --staged app.js correctly shows staged changes only for app.js.
  3. Final Answer:

    git diff --staged app.js -> Option C
  4. Quick Check:

    File filter after --staged shows staged diff for that file [OK]
Hint: Put filename after --staged to filter staged diff [OK]
Common Mistakes:
  • Placing filename before options
  • Using --name-only which lists files, not diffs
  • Mixing staged and unstaged file filters