0
0
Gitdevops~5 mins

git diff between branches - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to see what changes exist between two versions of your project. Git diff between branches helps you compare the differences in files and code between two branches so you know what changed.
When you want to review code changes before merging one branch into another
When you want to understand what updates a teammate made on their branch
When you want to check if your feature branch has new changes compared to the main branch
When you want to find out what files were added, removed, or modified between branches
When you want to prepare a summary of changes for a code review or deployment
Commands
This command shows the differences between the 'main' branch and the 'feature-branch'. It lists all changes in files line by line so you can see what was added or removed.
Terminal
git diff main feature-branch
Expected OutputExpected
diff --git a/app.js b/app.js index e69de29..d95f3ad 100644 --- a/app.js +++ b/app.js @@ -0,0 +1,5 @@ +console.log('New feature added'); +function greet() { + return 'Hello!'; +} +
This command lists only the names of files that differ between the 'main' and 'feature-branch'. It helps you quickly see which files changed without showing the details.
Terminal
git diff --name-only main feature-branch
Expected OutputExpected
app.js index.html styles.css
--name-only - Show only the names of changed files, not the content differences
This command shows a summary of changes between the branches, including how many lines were added or removed in each file. It gives a quick overview of the size of changes.
Terminal
git diff --stat main feature-branch
Expected OutputExpected
app.js | 5 +++++ index.html | 2 ++ styles.css | 3 +++ 3 files changed, 10 insertions(+)
--stat - Show a summary of changes with line counts per file
Key Concept

If you remember nothing else from this pattern, remember: git diff between branches lets you see exactly what changed in files before merging or deploying.

Common Mistakes
Running 'git diff' without specifying both branch names
This shows changes only between your current branch and the working directory, not between two branches.
Always specify both branches like 'git diff main feature-branch' to compare them directly.
Using branch names that do not exist or are misspelled
Git will show an error because it cannot find the branches to compare.
Check branch names with 'git branch' and type them exactly as shown.
Summary
Use 'git diff branch1 branch2' to see detailed line-by-line changes between two branches.
Use 'git diff --name-only branch1 branch2' to list only the files that changed.
Use 'git diff --stat branch1 branch2' to get a summary of how many lines changed per file.