Bird
Raised Fist0
Gitdevops~20 mins

git diff for working directory 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 Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of git diff after modifying a tracked file?
You have a file app.txt tracked by Git. You change a line in this file but do not stage it. What does git diff show?
Git
git diff
AShows the line differences between the last committed version and the current modified file.
BShows the differences between the staged changes and the last commit.
CShows no output because changes are not staged.
DShows the commit history of the file.
Attempts:
2 left
💡 Hint
Think about what git diff compares by default when no options are given.
🧠 Conceptual
intermediate
2:00remaining
Which command shows differences between staged changes and the last commit?
You have modified and staged some files. You want to see what changes are staged compared to the last commit. Which command do you use?
Agit diff --cached
Bgit diff --staged
CBoth B and C
Dgit diff
Attempts:
2 left
💡 Hint
There are two equivalent options to show staged changes.
Troubleshoot
advanced
2:00remaining
Why does git diff show no output after modifying a file?
You edited a tracked file but running git diff shows no output. What could be the reason?
AYou have no changes in the working directory.
BThe file was staged already, so changes are not in working directory.
CThe file is ignored by Git.
DYou ran <code>git diff</code> in a different directory.
Attempts:
2 left
💡 Hint
Think about what git diff compares by default.
🔀 Workflow
advanced
2:00remaining
What is the correct sequence to view unstaged changes, stage them, and then view staged changes?
Arrange these commands in the correct order to see unstaged changes, stage them, and then see staged changes.
A2, 1, 3
B1, 2, 3
C3, 2, 1
D2, 3, 1
Attempts:
2 left
💡 Hint
First see unstaged changes, then stage, then see staged changes.
Best Practice
expert
2:00remaining
Which command safely shows all changes including untracked files before committing?
You want to review all changes including unstaged, staged, and untracked files before committing. Which command shows this best?
Agit diff
Bgit diff --name-only
Cgit diff HEAD
Dgit status
Attempts:
2 left
💡 Hint
Think about which command summarizes all changes and untracked files.

Practice

(1/5)
1. What does the command git diff show you?
easy
A. The current branch name
B. The changes in your working directory that are not yet staged
C. The list of all commits in the repository
D. The status of remote branches

Solution

  1. Step 1: Understand the purpose of git diff

    git diff compares your working directory files with the last saved snapshot (commit or staged changes).
  2. Step 2: Identify what git diff outputs

    It shows the differences that are not yet staged for commit, meaning changes you made but haven't told git to save yet.
  3. Final Answer:

    The changes in your working directory that are not yet staged -> Option B
  4. Quick Check:

    git diff = unstaged changes [OK]
Hint: git diff shows unstaged file changes only [OK]
Common Mistakes:
  • Confusing git diff with git status
  • Thinking git diff shows committed changes
  • Assuming git diff shows staged changes
2. Which of the following is the correct syntax to see changes only in a single file named app.js?
easy
A. git diff --single app.js
B. git diff --file app.js
C. git diff -f app.js
D. git diff app.js

Solution

  1. Step 1: Recall the basic git diff syntax

    The command to check changes in a specific file is git diff <filename>.
  2. Step 2: Match the correct option

    Only git diff app.js uses the correct syntax: git diff app.js. Other options use invalid flags.
  3. Final Answer:

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

    git diff + filename = correct syntax [OK]
Hint: Use git diff followed by filename to check one file [OK]
Common Mistakes:
  • Adding unsupported flags like --file or --single
  • Using -f which is not for git diff
  • Confusing git diff syntax with other git commands
3. Given the following scenario: You modified a file index.html by adding a new line. What will git diff index.html show?
medium
A. The difference showing the added line in index.html
B. An error saying file not found
C. No output because changes are staged
D. The full content of index.html

Solution

  1. Step 1: Understand what git diff shows for a modified file

    When a file is changed but not staged, git diff filename shows the exact changes line by line.
  2. Step 2: Apply this to index.html

    Since you added a line and did not stage it, the command will show the added line as a difference.
  3. Final Answer:

    The difference showing the added line in index.html -> Option A
  4. Quick Check:

    git diff filename = shows unstaged changes [OK]
Hint: git diff filename shows unstaged changes in that file [OK]
Common Mistakes:
  • Expecting full file content instead of diff
  • Thinking git diff shows staged changes
  • Assuming error if file exists
4. You ran git diff but saw no output, even though you edited files. What could be the reason?
medium
A. You have untracked files only
B. You are on a detached HEAD state
C. You already staged the changes with git add
D. You have no git repository initialized

Solution

  1. Step 1: Understand what git diff shows

    git diff shows changes in the working directory that are not staged.
  2. Step 2: Analyze why no output appears despite edits

    If changes are already staged using git add, git diff will show nothing because working directory matches the staging area.
  3. Final Answer:

    You already staged the changes with git add -> Option C
  4. Quick Check:

    Staged changes hide from git diff output [OK]
Hint: No git diff output? Check if changes are staged [OK]
Common Mistakes:
  • Thinking git diff shows staged changes
  • Assuming untracked files appear in git diff
  • Confusing detached HEAD with diff output
5. You want to review all your unstaged changes in the project but exclude changes in the docs/ folder. Which command will help you achieve this?
hard
A. git diff -- . ':!docs/'
B. git diff --exclude=docs/
C. git diff --ignore docs/
D. git diff --skip docs/

Solution

  1. Step 1: Understand how to exclude paths in git diff

    Git supports pathspecs with negation using :!path syntax to exclude files or folders.
  2. Step 2: Apply exclusion to docs/ folder

    The correct command uses git diff -- . ':!docs/' to show all changes except those in docs/.
  3. Final Answer:

    git diff -- . ':!docs/' -> Option A
  4. Quick Check:

    Use pathspec negation ':!folder/' to exclude [OK]
Hint: Use git diff with ':!folder/' to exclude paths [OK]
Common Mistakes:
  • Using unsupported flags like --exclude or --ignore
  • Trying --skip which is invalid
  • Not using pathspec syntax for exclusion