Bird
Raised Fist0
Gitdevops~20 mins

git diff between branches - 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
2:00remaining
Output of git diff between two branches
What will be the output of the following command if branch feature has 3 new lines added to app.py compared to main?
Git
git diff main..feature -- app.py
AShows the commit history of <code>feature</code> branch
BShows all files changed between <code>main</code> and <code>feature</code> including <code>app.py</code>
CShows no output because <code>git diff</code> does not compare branches
DShows the 3 new lines added in <code>app.py</code> compared to <code>main</code>
Attempts:
2 left
💡 Hint
Use git diff branch1..branch2 to see differences between branches.
🧠 Conceptual
intermediate
1:30remaining
Understanding git diff branch range syntax
Which of the following correctly describes what git diff branchA..branchB shows?
AAll commits that exist in both branches
BChanges needed to convert <code>branchB</code> into <code>branchA</code>
CChanges needed to convert <code>branchA</code> into <code>branchB</code>
DThe list of files changed in <code>branchA</code> only
Attempts:
2 left
💡 Hint
Think about which branch is the base and which is the target.
Troubleshoot
advanced
2:00remaining
Why does git diff show no output between branches?
You run git diff main..feature but see no output, even though you expect differences. What could be a reason?
ABoth branches point to the same commit, so no differences exist
BYou forgot to add files to the staging area
CThe command only shows differences in the working directory, not between branches
DYou need to run <code>git fetch</code> before <code>git diff</code>
Attempts:
2 left
💡 Hint
Check if the branches have diverged commits.
🔀 Workflow
advanced
1:30remaining
Using git diff to review changes before merging
You want to review all changes in branch feature compared to main before merging. Which command is best?
Agit diff main..feature
Bgit diff feature..main
Cgit merge feature
Dgit log main..feature
Attempts:
2 left
💡 Hint
Think about which branch is the base and which is the feature.
Best Practice
expert
2:30remaining
Best practice for comparing branches with uncommitted changes
You have uncommitted changes in your working directory. You want to compare feature branch with main. What is the best practice?
AStash or commit your changes before running <code>git diff main..feature</code>
BRun <code>git diff main..feature</code> directly; uncommitted changes do not affect it
CUse <code>git diff --cached main..feature</code> to ignore uncommitted changes
DReset your working directory to discard uncommitted changes before diff
Attempts:
2 left
💡 Hint
git diff between branches compares branch tips and ignores working directory.

Practice

(1/5)
1. What does the command git diff branch1 branch2 show you?
easy
A. The commit history of branch1
B. The list of branches in the repository
C. The differences in code between branch1 and branch2
D. The status of files in the current branch

Solution

  1. Step 1: Understand the purpose of git diff

    The git diff command compares changes between two points in Git, such as branches.
  2. Step 2: Identify what comparing two branches means

    Comparing branch1 and branch2 shows the code differences between them.
  3. Final Answer:

    The differences in code between branch1 and branch2 -> Option C
  4. Quick Check:

    git diff branch1 branch2 = code differences [OK]
Hint: git diff between branches shows code changes [OK]
Common Mistakes:
  • Thinking it shows commit history
  • Confusing with git branch command
  • Expecting file status instead of differences
2. Which of the following is the correct syntax to see differences between two branches named main and feature?
easy
A. git diff main feature
B. git diff --branches main feature
C. git diff feature..main
D. git diff main..feature

Solution

  1. Step 1: Recall git diff syntax for branches

    The basic syntax is git diff branch1 branch2 without dots or extra flags.
  2. Step 2: Evaluate each option

    git diff main feature uses git diff main feature, which is correct. Options A and C use double dots which is incorrect for git diff. git diff --branches main feature uses a non-existent flag.
  3. Final Answer:

    git diff main feature -> Option A
  4. Quick Check:

    Correct syntax = git diff main feature [OK]
Hint: Use 'git diff branch1 branch2' without dots [OK]
Common Mistakes:
  • Using double dots '..' with git diff
  • Adding unsupported flags like --branches
  • Swapping branch order without reason
3. Given two branches, main and dev, where dev has added a new line console.log('Hello'); in app.js, what will git diff main dev show?
medium
A. An error message about branch names
B. A line removed: -console.log('Hello');
C. No output because branches are the same
D. A line added: +console.log('Hello');

Solution

  1. Step 1: Understand what git diff shows for added lines

    When a line is added in the second branch, git diff shows it with a plus sign (+).
  2. Step 2: Apply to the example

    The new line console.log('Hello'); added in dev will appear as a line starting with + in the diff output.
  3. Final Answer:

    A line added: +console.log('Hello'); -> Option D
  4. Quick Check:

    Added lines show with + in git diff [OK]
Hint: Added lines show with + in git diff output [OK]
Common Mistakes:
  • Thinking added lines show with -
  • Expecting no output for changes
  • Confusing branch order in diff
4. You run git diff main feature but get no output even though you know feature has changes. What is a likely reason?
medium
A. You forgot to commit changes in feature branch
B. The branches have no differences
C. You are currently on the feature branch
D. You used the wrong command syntax

Solution

  1. Step 1: Understand git diff compares committed changes

    Git diff between branches compares committed differences, not uncommitted changes.
  2. Step 2: Identify why no output appears

    If changes are not committed in feature, git diff won't show them when comparing branches.
  3. Final Answer:

    You forgot to commit changes in feature branch -> Option A
  4. Quick Check:

    Uncommitted changes not shown in branch diff [OK]
Hint: Only committed changes appear in git diff between branches [OK]
Common Mistakes:
  • Expecting uncommitted changes to show
  • Confusing current branch with diff branches
  • Assuming syntax error without checking commits
5. You want to review all changes between release and hotfix branches but only for files in the src/ folder. Which command correctly shows this?
hard
A. git diff release..hotfix src/
B. git diff release hotfix -- src/
C. git diff --src release hotfix
D. git diff release hotfix --path src/

Solution

  1. Step 1: Understand how to limit git diff to a folder

    Git diff allows specifying paths after a double dash -- to limit output to those files or folders.
  2. Step 2: Check each option for correct syntax

    git diff release hotfix -- src/ uses git diff release hotfix -- src/, which correctly limits diff to src/. git diff release..hotfix src/ uses double dots incorrectly. Options C and D use invalid flags.
  3. Final Answer:

    git diff release hotfix -- src/ -> Option B
  4. Quick Check:

    Use -- then folder to limit git diff [OK]
Hint: Use -- then folder path to filter git diff output [OK]
Common Mistakes:
  • Using double dots '..' with git diff
  • Adding unsupported flags like --src or --path
  • Placing folder path before branch names