git commands


git diff

Show what changed: in the working tree, in what is staged, or between two commits or branches.

git diff
Save

Angle brackets are yours to fill in. Windows, Linux, macOS.

Examples for git diff

  • See what is staged for the next commit

    git diff --staged
    Save

    A plain git diff shows only unstaged changes, so once you git add a file it seems to disappear. --staged shows what the commit will contain.

    gittooldiffsubcommand--stagedflag
  • Compare your branch with main

    git diff main...HEAD
    Save

    Three dots compare against the point where your branch left main, so you see only your branch's changes, as a pull request shows them. Two dots compare the two tips directly, which also shows main's new commits in reverse.

    gittooldiffsubcommandmain...HEADsince your branch left main
  • List only the files that changed

    git diff --name-only <commit>
    Save

    Prints one path per line, which is easy to pass to another command. Use --stat to also see how many lines changed in each.

    gittooldiffsubcommand--name-onlyflag<commit>argument
  • See the changes to one file

    git diff -- <path>
    Save

    Limits the diff to <path>. The -- separates paths from commits and branches, so a file named like a branch is not misread.

    gittooldiffsubcommand--flag<path>path
  • Ignore whitespace changes

    git diff -w
    Save

    Hides lines whose only change is spaces or tabs, so reindented code does not hide the real change.

    gittooldiffsubcommand-wflag

Flags

--staged --cached
Show what is staged, which is what the next commit will contain.
--stat
Show only the changed files and how many lines each gained and lost.
--name-only
Show only the names of the changed files.
--word-diff
Mark changed words inside a line, instead of whole lines.
--ignore-all-space -w
Ignore whitespace changes, such as reindentation.