git diff

Term side of the Card.
Topic: Git Commands - Main
Unit title: git diff
Categories: Command
Explanation side of the Card

Explanation: The command that shows line-by-line differences between the working directory and the staging area. With `--staged` it compares staged changes to the last commit, and it can also compare branches or commits.

Example

Wrongbash
git add .
git diff
Rightbash
git add .
git diff --staged

Plain git diff compares against the staging area, so staged changes only show with --staged.

Common mistakes

  • Running git diff after git add and seeing nothing, because staged changes need --staged.

Interview questions

How do I compare two branches with git diff?

Run git diff main..feature. To see only what the feature branch added since it split off, use git diff main...feature.

How do I list only changed file names?

Run git diff --name-only. Add --staged or two refs to choose what to compare.

What is the difference between git diff --staged and --cached?

None. They are two names for the same option.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy