Git & GitHub · 10 min · 110 XP

Reading history: status, diff and log

Read what changed with status, diff and log, and throw away edits you don't want.

Three commands answer nearly every what's going on? question. git status tells you which files changed and which are staged. git diff shows the changed lines themselves. git log shows the commits so far, newest first.

bash
git diff               # changes you haven't staged yet
git diff --staged      # changes that will go into the next commit
git log --oneline      # one line per commit: short id + subject
git show b7a03d1       # everything one commit changed

In a diff, lines starting with - were removed and lines starting with + were added. The lines around them with no sign are unchanged context, there to show you where the change sits. A changed line shows up as a - line followed by a + line.

To undo edits you haven't committed, use git restore <file>: the file goes back to how it was in the last commit. This one really is permanent, because the discarded edits were never saved anywhere. If you staged something by mistake, git restore --staged <file> unstages it and leaves your edits alone.

Scenario: you edit cart.js, then run git diff
$ git diff
diff --git a/src/cart.js b/src/cart.js
index 3f1c2a4..9d0b7e1 100644
--- a/src/cart.js
+++ b/src/cart.js
@@ -3,3 +3,3 @@
 function total(items) {
-  return items.reduce((sum, i) => sum + i.price, 0);
+  return items.reduce((sum, i) => sum + i.price * i.quantity, 0);
 }
Scenario: history so far
$ git log --oneline
e41f2c9 (HEAD -> main) Show shipping estimate on cart page
b7a03d1 Fix rounding in cart total
5c9e8f2 Add cart page

Try it on your machine: in your practice repository, change a line in index.html and run git diff. Stage it and run git diff again (empty) and then git diff --staged. Commit it, and find the commit with git log --oneline and git show.

Loading your workspace…