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
Compare Changes Between Git Branches Using git diff
📖 Scenario: You are working on a software project using Git. You have two branches: main and feature. You want to see what changes have been made in the feature branch compared to main before merging.
🎯 Goal: Learn how to use the git diff command to compare differences between two branches.
📋 What You'll Learn
Have a Git repository with two branches named main and feature
Use git diff to compare branches
Understand how to specify branch names in git diff
💡 Why This Matters
🌍 Real World
Developers often work on separate branches to add features or fix bugs. Comparing branches helps review what changes will be merged.
💼 Career
Knowing how to compare branches with <code>git diff</code> is essential for code reviews and collaboration in software development teams.
Progress0 / 4 steps
1
Create two branches main and feature
Create a Git branch called main and another branch called feature.
Git
Hint
Use git branch branch_name to create a branch.
2
Add a file and commit changes on main branch
Switch to the main branch, create a file named app.txt with the text Main branch content, add it to Git, and commit with the message Initial commit on main.
Git
Hint
Use git checkout main to switch branches. Use echo to create file content.
3
Make a change on the feature branch
Switch to the feature branch, modify app.txt by adding the line Feature branch addition, add and commit the change with the message Update on feature branch.
Git
Hint
Use git checkout feature to switch branches. Use >> to append text to a file.
4
Use git diff to compare feature branch with main
Run the command git diff main..feature to see the changes made in the feature branch compared to main.
Git
Hint
Use git diff main..feature to compare the two branches.
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
Step 1: Understand the purpose of git diff
The git diff command compares changes between two points in Git, such as branches.
Step 2: Identify what comparing two branches means
Comparing branch1 and branch2 shows the code differences between them.
Final Answer:
The differences in code between branch1 and branch2 -> Option C
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
Step 1: Recall git diff syntax for branches
The basic syntax is git diff branch1 branch2 without dots or extra flags.
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.
Final Answer:
git diff main feature -> Option A
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
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 (+).
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.
Final Answer:
A line added: +console.log('Hello'); -> Option D
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?