Skip to content
KALANBLON
Show contents

Chapter 2 of 7 · 20 min

Compare, record, undo

By the end of this chapter, you'll be able to

  • Read the short output of git status
  • Compare the working directory, the staging area and the last commit with git diff
  • Undo a change with git restore
  • Write clear commit messages and read the history

The starting point

For this chapter, we start from a repository that already contains a commit:

git init
echo "<h1>Kofi's Bakery</h1>" > index.html
git add index.html
git commit -m "Add the home page"

The everyday cycle

Your work always follows the same loop: edit, check, stage, record. Let's add a line to the page:

echo "<p>Hot bread from 7am</p>" >> index.html
git status -s
 M index.html

>> adds a line at the end of the file (whereas > replaces it). The M is in the second column: the file is modified in the working directory, but the change is not staged yet. The most common codes:

CodeMeaning
??untracked file
A new file, staged
Mmodified, not staged yet
M modified and staged
MMstaged, then modified again

See what changed

git diff shows the differences between the working directory and the staging area:

git diff
diff --git a/index.html b/index.html
index 23f193c..b03990d 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
 <h1>Kofi's Bakery</h1>
+<p>Hot bread from 7am</p>

Lines starting with + are added, lines starting with - are removed, the others are context. @@ -1 +1,2 @@ summarises the area: line 1 of the old version becomes lines 1 to 2 of the new one.

Once the change is staged, git diff shows nothing anymore: you need git diff --staged to see what will go into the next commit.

After git commit, the three areas are identical.

Step 5 of 5
Text description of the animation
  1. You add a line. The working directory differs from the staging area.
  2. git diff shows that difference: what you have not staged yet.
  3. After git add, the staging area holds the new version. git diff shows nothing anymore.
  4. git diff --staged compares the staging area with the last commit: this is the content of the next commit.
  5. After git commit, the three areas are identical.
git add index.html
git diff --staged --stat
git commit -m "Announce hot bread"

--stat summarises the differences instead of showing them in full.

Read the history

git log shows the commits, from the most recent to the oldest. The most useful everyday form fits on one line per commit:

$ git log --oneline
c81d2e4 (HEAD -> main) Announce hot bread
3f2a9c1 Add the home page

HEAD is the commit you are on. The --format option chooses what is displayed: %h the short identifier, %an the author, %s the message.

git log --format="%s"
Announce hot bread
Add the home page

A good commit message fits on one short line (around fifty characters), describes what the commit does in the imperative mood (“Add”, “Fix”, “Remove”) and reads like “This commit will… add the opening hours”. “changes” or “wip” will not help you in six months.

Go back with git restore

A failed change, not staged yet? git restore puts the file back in the state of the staging area:

echo "<p>SALE -90% !!!</p>" >> index.html
git restore index.html
cat index.html
<h1>Kofi's Bakery</h1>
<p>Hot bread from 7am</p>

A file staged by mistake? git restore --staged removes it from the staging area, without touching the file itself:

echo "Closed on Mondays" > closing.txt
git add closing.txt
git restore --staged closing.txt
git status -s
?? closing.txt

Your turn

Mini-lab: stage, compare, undoGit simulator

Terminal

Click “Run”: the commands run in a fresh repository. Type “help” for the list of commands.

Teaching simulator: it reproduces the essential Git commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

In this lab, try git restore welcome.txt before the commit, then cat welcome.txt: what is left?

Worked exercise

Worked exercise: announce home delivery

Add the line <p>Home delivery</p> at the end of index.html. First check the summary of the changes, then record them in a single command with the message “Announce home delivery”. Finally, display the messages of the history.

Hint 1
git diff --stat gives the summary of the unstaged changes.
Hint 2
The -a option of git commit automatically stages every modified file that is already tracked.
Show the solution and common mistakes
echo "<p>Home delivery</p>" >> index.html
git diff --stat
 index.html | 1 +
 1 file changed, 1 insertion(+)
git commit -am "Announce home delivery"
git log --format="%s"
Announce home delivery
Announce hot bread
Add the home page

Common mistakes:

  • Counting on -a for a new file: it only takes files that are already tracked. Here, closing.txt did not go into the commit, and that is intended.
  • Using > instead of >>: the file is replaced by a single line. git diff shows it at once with - lines.

Summary

  1. 1git status -s summarises the state: the first column for the staging area, the second for the working directory.
  2. 2git diff shows what is not staged yet, git diff --staged what will go into the next commit.
  3. 3git log displays the history, and --oneline or --format make it easier to read.
  4. 4git restore undoes an unrecorded change, git restore --staged removes a file from the staging area.
  5. 5A good commit message is short, in the imperative mood, and says what the commit does.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.