Lesson 5 of 7 in Basics
Seeing changes
Story: Pim never files anything unread. Read your changes before they go on record.
git status tells you which files changed. git diff shows you what changed in them, line by line, so you can check your work before you commit it.
Changes you haven't staged: git diff
git diff compares your working tree with the staging area. It shows every change you've made but haven't staged yet:
diff --git a/index.html b/index.html
index 284fccb..84c05d5 100644
--- a/index.html
+++ b/index.html
@@ -2,3 +2,4 @@
<title>Home</title>
<h1>Hello</h1>
<p>Welcome to my website.</p>
+<p>Open on Mondays.</p>
It reads like git show: a line starting with + is one you added, and a line starting with - is one you removed. A changed line shows up as both: its old version removed, its new version added. Up to three unchanged lines around each change show where it is.
The - lines help when something was deleted by mistake: the file no longer has it, but the diff still shows what was there.
When git diff prints nothing, there's nothing unstaged: you've staged every change, or there are none.
git diff only shows files git already tracks. A new, untracked file doesn't appear in it: git status lists it under Untracked files.
Changes you've staged: git diff --staged
git diff --staged compares the staging area with the last commit. It shows exactly what your next commit will hold. git diff --cached means the same.
When you stage a change with git add, it moves from one diff to the other: git diff stops showing it, and git diff --staged starts. So before you commit:
git diff --stagedshows what will go in;git diffshows what you've changed that won't.
Taking a change back out
Staging copies a file as it is. So to take a change out of the staging area, put the file back the way it was in the last commit, and stage it again: the staged copy then matches the last commit, and there's nothing of that file left to commit. git diff --staged shows the old lines, with a - in front. Undoing Things has a quicker way, git restore --staged.
Staging and committing in one go: git commit -a
git commit -a -m "<message>" stages every change to the files git already tracks, then commits. You can also write it git commit -am "<message>".
It saves a step, but check what it takes first:
- it stages every change to a tracked file, finished or not;
- it never stages a new file. An untracked file still needs
git add.
| Command | What it does |
|---|---|
git diff |
Shows the changes you haven't staged. |
git diff --staged |
Shows the changes you've staged: what the next commit will hold. |
git commit -a -m "<message>" |
Stages every change to tracked files, then commits them. New files aren't included. |
New commands
git diffgit diff --stagedgit commit -a
Explainer: Seeing what changed
Graph as text
project: your repo, you are here
- HEAD → main at 07bc891 "Make the heading purple"
Branches and tags
- main at 07bc891 "Make the heading purple"
Commits, newest first
- 07bc891 "Make the heading purple"; parent fcf9acf; refs: HEAD → main
- fcf9acf "Add opening hours"; parent a4b8d97
- a4b8d97 "Style the heading"; parent ab89288
- ab89288 "Add the home page"; parent 37d7cf7
- 37d7cf7 "Add a README"; no parent
Challenge 1 of 3
Story: A page of the README went missing on your watch, Agent. Put it back before anyone notices.
You've written a new page, contact.html, and haven't staged anything yet. While you worked, a line of README.md was deleted by accident. Put README.md back the way it was in the last commit, and commit the new page.
Commands 0 of Par 3
Goal
- Met: HEAD is on branch main
- Not met yet: Branch main is at the goal's commit
- Not met yet: The history of main matches the goal
- Not met yet: The working tree matches the goal
- Not met yet: main has one new commit
- Not met yet: The new commit adds contact.html
- Met: The new commit leaves README.md as it was
- Not met yet: README.md in your files has its deleted line back