Skip to main content

What is git diff used for?

git diff is used to view differences (changes) between file versions: before a commit, after a commit, or between commits/branches.

Simply put: the command shows exactly what changed, not just that something changed.

What git diff shows by default

bash
git diff

Shows:

  • differences between the working directory and the staging area
  • changes that have not yet been added via git add

Main use cases

Difference between the staging area and the last commit

bash
git diff --staged # or git diff --cached

All changes (both staged and unstaged)

bash
git diff HEAD

Difference between two commits

bash
git diff commit1 commit2

Difference between branches

bash
git diff main feature/login

Changes in a specific file

bash
git diff src/app.js

How to read git diff output

diff
- const a = 1; + const a = 2;
  • - - a removed line
  • + - an added line
  • no sign - context

Why git diff is used in practice

  • Check code before git add
  • Deliberately shape commits
  • Find the source of a bug
  • Do a code review locally
  • Compare implementations

Frequent mistakes

  • Confusing git diff and git status
  • Not checking changes before a commit
  • Committing "blindly"

Short version for an interview

git diff is used to view differences between file versions - in the working directory, staging area, commits, or branches.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.