Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 78 ยท The Basic Workflow

Status, Diff and Seeing Changes

Master git status (long, short, branch info) and git diff (working tree, staged, HEAD, stat, word diff, whitespace options) and learn to read unified diffs fluently.

Two commands account for most of the time you spend looking at Git: git status tells you which files are in what state, and git diff tells you exactly what changed inside them. Before every commit, every pull and every moment of doubt, you run these two. Reading their output quickly and correctly is the single most useful everyday Git skill.

In How Git Works you met the three areas (working tree, index, repository). status summarizes how those three compare, and diff shows the content difference between any two of them. This lesson covers every form you need, then teaches you to read a unified diff line by line. Next, Staging Changes uses this knowledge to build clean commits.

git status: the long form

The default output is wordy on purpose; it is written for humans and tells you the next command to try. Here is a repository in a "busy" state: one staged change, one unstaged change, one new file.

Bash The long form of git status

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Git prints the sections in a fixed order, and each tells you what to do:

  1. Changes to be committed are staged. They go into the next commit. git restore --staged <file> unstages.
  2. Changes not staged for commit are modified in the working tree only. git add <file> stages them; git restore <file> throws the edits away.
  3. Untracked files are unknown to Git. git add <file> starts tracking them.

When all three areas agree, status ends with nothing to commit, working tree clean. That sentence is your goal state before pulling, switching branches or deploying.

git status -s: the short form

Once you know the sections, the verbose text gets in the way. -s (or --short) prints one line per file with a two-letter code. -b adds a header line with the branch name.

Bash Short status with the branch line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Each line has the form XY path:

  • X (left column) compares the index with the last commit.
  • Y (right column) compares the working tree with the index.
Code Meaning
?? Untracked
A New file, staged
M Modified, staged
M Modified, not staged
MM Staged changes plus further unstaged changes
D Deleted, staged
D Deleted in the working tree, not staged
R Renamed, staged
UU Both sides modified during a merge (a conflict)

## main on the first line is the branch. Once a branch tracks a remote, -b also prints ...origin/main [ahead 2] or [behind 1]. Remember "left column = ready to commit, right column = still to do".

For scripts, use git status --porcelain instead. It prints the same format as -s but is guaranteed stable across Git versions and independent of your configuration, so it is safe to parse. A classic check is "is the working tree dirty?":

Terminal
if [[ -n "$(git status --porcelain)" ]]; then echo "dirty"; fi

git diff: working tree against index

git diff with no arguments answers: "What have I changed but not yet staged?" It compares the working tree with the index. Two details that confuse beginners:

  • Untracked files never appear in git diff, because Git has nothing to compare them with.
  • Staged changes do not appear either; they are no longer "unstaged".

Here is a diff of one edited file:

Bash Your first unified diff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Reading a unified diff

A diff looks cryptic until you know its anatomy. Take the output above piece by piece:

TEXT
diff --git a/poem.txt b/poem.txt        1. which file; a/ is the old side, b/ the new side
index 19239e6..ab2c474 100644           2. old and new blob IDs, and the file mode
--- a/poem.txt                          3. the old file
+++ b/poem.txt                          4. the new file
@@ -1,4 +1,5 @@                         5. hunk header
 Roses are red                          6. context line (unchanged, starts with a space)
-Violets are blue                       7. removed line
+Violets are purple                     8. added line
 Sugar is sweet
 And so are you
+The end

The hunk header @@ -1,4 +1,5 @@ says: in the old file this hunk covers 4 lines starting at line 1; in the new file it covers 5 lines starting at line 1. Git shows three lines of context around every change so you can find the spot. When changes are far apart, they appear as several hunks, each with its own header. After the closing @@, Git may print the nearest preceding "function" line (for source code) to tell you where you are.

A changed line is shown as a removal plus an addition. There is no separate "modified" marker; if you see - immediately followed by +, that is an edit.

Staged changes and HEAD

After you stage a change, git diff shows nothing (the working tree equals the index). To see what you are about to commit, compare index with the last commit:

TEXT
git diff --staged     (same as --cached)   index    vs  last commit
git diff              (no arguments)       working  vs  index
git diff HEAD                              working  vs  last commit  (staged + unstaged together)

Study all three on one file. We stage one edit and then make a second one:

Bash diff, diff --staged and diff HEAD

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

(We filtered the output with grep to hide the header lines and keep the example short.) Notice that git diff HEAD combines both edits: it is the answer to "what does my project look like now compared with the last commit, whatever the staging state?"

Summaries: --stat, --name-only and friends

For a big change you first want a bird's-eye view. These options replace the line-by-line patch with a summary:

Option Shows
--stat Files with a bar graph of added/removed lines
--numstat The same numbers in tab-separated form (for scripts)
--shortstat A single summary line
--name-only Just the changed file names
--name-status File names with a status letter (A, M, D, R)
--check Warns about whitespace errors (trailing spaces, conflict markers)
Bash Summaries of the same change

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

--stat is perfect before a commit: it confirms that you touched the files you meant to and no others. --name-status uses A for added, M for modified and D for deleted. Scripts prefer --name-only (one path per line) or --numstat (added<TAB>removed<TAB>path).

Comparing only some files

Add a path after -- to limit any diff. The double dash tells Git "everything after this is a path, not a revision", which avoids ambiguity if a file shares a name with a branch.

Terminal
git diff -- src/app.js
git diff --staged -- docs/

Whitespace and word-level changes

Sometimes a line changes only by indentation or by a single word. A normal diff then shows an entire line as removed and added, hiding the real change. Two tools help.

Ignoring whitespace. -w (--ignore-all-space) ignores all whitespace differences, -b (--ignore-space-change) ignores changes in the amount of whitespace, and --ignore-blank-lines skips blank-line changes. They are invaluable after reformatting a file.

Word diff. --word-diff marks changed words inline instead of whole lines. --word-diff=porcelain and --color-words are variants; the plain mode wraps removals in [-...-] and additions in ....

Bash Whitespace options and word diff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The plain diff reports both the indented line and the changed sentence. With -w the re-indented line is no longer reported as a change (it shows as plain context), leaving the real edit. With --word-diff, the sentence becomes The quick [-brown-]red fox jumps: now you see the one word that changed. Word diff is especially good for prose, Markdown and long lines.

git diff --check is a handy pre-commit habit: it flags trailing whitespace and leftover conflict markers.

Bash git diff --check catches whitespace errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

--check prints a.txt:2: trailing whitespace. and exits with code 2, so you can use it in a script or a Git hook to refuse sloppy commits.

Other things worth knowing

  • Comparing any two points. git diff A B compares two commits, branches or tags; this is the subject of Comparing Commits, Branches and Files.
  • Binary files. For images and archives diff cannot show lines; it prints Binary files a/x.png and b/x.png differ.
  • Renames. When you move a file, status reports R because Git detects the rename by similarity. See Moving, Renaming and Removing Files.
  • Color. In a terminal, removals are red and additions green. Colour is only decoration; the + and - characters carry the meaning.
  • Exit status. git diff --exit-code exits with 1 if there are differences, 0 if none; --quiet is the same without output. Useful in scripts.
  • Seeing a commit's patch. git show <commit> prints a past commit with its diff (Viewing History).
  • Pager. On your computer long output goes through less; press q to leave. Our sandbox prints it all at once.

A healthy habit

Before every commit run this pair:

TEXT
git status -s            # which files? any surprises (untracked, unintended)?
git diff --staged        # exactly what is about to be recorded?

Many "oops" moments (a debug line, a password, a stray file) are caught by that 10-second review.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A file shows M (a space, then M) in git status -s. What does it mean?

    The left column describes the index, the right column the working tree. A blank left and an M on the right means the change is not staged.
  2. 2. You staged all your edits. What does plain git diff print?

    git diff compares the working tree with the index. Use git diff --staged to see what you staged.
  3. 3. In the hunk header @@ -10,6 +10,8 @@, what does -10,6 describe?

    The part with - is the old side (start line, number of lines) and the part with + is the new side.
  4. 4. Which command shows only the names of changed files together with a letter such as A, M or D?

    --name-status adds the status letter; --name-only lists only paths; --stat shows a bar graph of line counts.
  5. 5. Why use git status --porcelain in a script instead of git status -s?

    The short format can be influenced by settings, while the porcelain format is a promised, stable interface for scripts.

Summary

  • git status shows what is staged, unstaged and untracked; -s gives a two-column short form (left = index vs HEAD, right = working tree vs index) and -b adds the branch.
  • git diff compares the working tree with the index; --staged compares index with HEAD; HEAD compares working tree with the last commit.
  • Untracked files never appear in git diff.
  • A unified diff has context lines, - removals, + additions and @@ hunk headers with line ranges.
  • --stat, --name-only, --name-status, --numstat summarize; -w ignores whitespace; --word-diff shows word-level changes; --check finds whitespace problems.
  • Review git status -s and git diff --staged before each commit.

Next: Staging Changes: add, patch Mode and Partial Commits