Ran Wei/ Git & GitHub/Lesson 4
中文
Git and GitHub, Step by Step

Lesson 4Saving changes and looking back

Change your page, see exactly what changed, save more snapshots, look back at old ones, and throw away a change you do not want.

⏱ 40 minYou need: Lesson 3 (the my-page repository with one commit)

In this lesson you will

  • See what you changed with git diff
  • Save more commits, including a new file, with git add .
  • List your history in short form and look inside one commit
  • Throw away a change you have not committed, with git restore
  • Write commit messages that help you later
1

Pick up where you left off

Open your project in VS Code: File > Open Folder..., choose my-page. Then open a terminal (Terminal > New Terminal) and check that everything is as you left it:

Terminal
$ git statusOn branch main
nothing to commit, working tree clean

“Working tree clean”: nothing has changed since your last commit. Good.

Tip

VS Code remembers the folders you opened. File > Open Recent gets you back to my-page quickly next time.

2

Change your page

Add a line about yourself to index.html. The box below shows the whole file, with the line to add highlighted. Use this text if you want your outputs to match this lesson exactly.

The safest way to change a file, here and in every lesson: click Copy on the box, click in index.html in VS Code, press Ctrl+A (Cmd+A on a Mac) to select everything, and paste over it with Ctrl+V (Cmd+V). The file then matches the box exactly. If you type the line instead, remember that VS Code adds the closing </p> as soon as you type <p>: type only the sentence after it.

index.htmlhighlighted: the lines to add or change
<!DOCTYPE html><html lang="en"><head>  <meta charset="utf-8">  <title>My page</title></head><body>  <h1>Hello, I'm learning Git!</h1>  <p>This is my first web page.</p>  <p>I like walking, photography and good coffee.</p></body></html>

Ask Git what it sees:

Terminal
$ git statusOn branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

Git knows index.html is modified: it is different from the last commit. The change is “not staged”: it is in your working folder, but not yet in the staging area.

3

See exactly what changed: git diff

git status says which files changed. git diff shows how:

Terminal
$ git diffdiff --git a/index.html b/index.html
index 800ba6d..7baeae6 100644
--- a/index.html
+++ b/index.html
@@ -7,5 +7,6 @@
 <body>
   <h1>Hello, I'm learning Git!</h1>
   <p>This is my first web page.</p>
+  <p>I like walking, photography and good coffee.</p>
 </body>
 </html>

It looks technical, but you only need three things:

How to read git diffdiff --git a/index.html b/index.htmlindex 800ba6d..7baeae6 100644--- a/index.html+++ b/index.html@@ -7,5 +7,6 @@ <body> <h1>Hello, I'm learning Git!</h1> <p>This is my first web page.</p>+ <p>I like walking, photography and good coffee.</p> </body> </html>which filewhere: around line 7unchanged lines, to show wherean added line (+, green)unchangedA removed line would start with − and be red.
Figure 4.1

How to read git diff. The header names the file. Lines starting with + were added, lines starting with - were removed, and lines starting with a space did not change: Git shows a few of them so you can see where the change is.

  • The first four lines say which file this is. Ignore the numbers on the index line.
  • @@ -7,5 +7,6 @@ says where in the file: around line 7.
  • Then the lines themselves: + added, - removed, a space for lines that stayed the same.

In your terminal, added lines are green and removed lines are red.

Tip

VS Code shows the same thing in pictures. Click the Source Control icon in the bar on the left (it looks like a branching line), then click index.html under “Changes”. VS Code shows the old and new versions side by side. To get back to your files, click the top icon of the same bar (two sheets of paper).

If something went wrong

git diff prints nothing. Either the change is not saved yet (a dot on the file’s tab in VS Code means unsaved: press Ctrl+S, or Cmd+S on a Mac), or you already ran git add: git diff only shows changes that are not staged. To see staged changes, use git diff --staged.

I see a : or (END) at the bottom and cannot type. The output was longer than the screen. Press q.

4

Save the change

The same two steps as before:

Terminal
$ git add index.html$ git commit -m "Add a sentence about me"[main 358630b] Add a sentence about me
 1 file changed, 1 insertion(+)

Two commits now. Each one is a snapshot you can come back to.

5

Add a second file

Give your page some colour. Create a new file called style.css, the same way you created index.html (New File... in the Explorer), and paste this text into it:

style.css
body {  font-family: sans-serif;  max-width: 40em;  margin: 2em auto;  padding: 0 1em;  background: #fdfaf3;  color: #222;} h1 {  color: #2a6f97;}

Then tell the page to use it: add the highlighted line to index.html (copy the box, select everything in the file, paste).

index.htmlhighlighted: the lines to add or change
<!DOCTYPE html><html lang="en"><head>  <meta charset="utf-8">  <title>My page</title>  <link rel="stylesheet" href="style.css"></head><body>  <h1>Hello, I'm learning Git!</h1>  <p>This is my first web page.</p>  <p>I like walking, photography and good coffee.</p></body></html>

If your page is open in the browser, reload it (F5, or Cmd+R on a Mac): the background turns cream, the font changes, the heading turns blue, and the text sits in a column in the middle of the window.

Terminal
$ git statusOn branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   index.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	style.css

no changes added to commit (use "git add" and/or "git commit -a")

One file is modified and one is new. Both belong in the same snapshot, because together they make one change: “add colours”. You could add them one by one, or all at once:

Terminal
$ git add .$ git statusOn branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   index.html
	new file:   style.css

The . means “this folder”, so git add . adds every change in the folder. It is quick, but always run git status first, to be sure you are adding only what you mean to.

Terminal
$ git commit -m "Add colours with a style sheet"[main f952430] Add colours with a style sheet
 2 files changed, 13 insertions(+)
 create mode 100644 style.css
6

Look back at your history

git log shows a lot for each commit. Add --oneline for one line each:

Terminal
$ git log --onelinef952430 (HEAD -&gt; main) Add colours with a style sheet
358630b Add a sentence about me
c50e3a0 Add my first web page

Newest first. Each line starts with the commit’s short hash, then its message. You can picture your history as a chain:

A chain of commitsc50e3a0Add my first web page358630bAdd a sentence about mef952430Add colours with a stylesheetmainHEADoldernewerEach commit is a full snapshot of the project, and points back to the one before.
Figure 4.2

Your history is a chain of commits. Each commit is a full snapshot of the project, with a hash and a message, and points back to the one before it. main is a label on the newest commit, and HEAD marks the commit you are working on.

To look inside one commit, give git show its hash. Use a hash from your own list, not the one printed here:

Terminal
$ git show 358630bcommit 358630b7b7a64b3326a6d4c22d224a02ffaaf8a9
Author: Your Name <12345678+your-username@users.noreply.github.com>
Date:   Thu Nov 5 10:05:00 2026 +0000

    Add a sentence about me

diff --git a/index.html b/index.html
index 800ba6d..7baeae6 100644
--- a/index.html
+++ b/index.html
@@ -7,5 +7,6 @@
 <body>
   <h1>Hello, I'm learning Git!</h1>
   <p>This is my first web page.</p>
+  <p>I like walking, photography and good coffee.</p>
 </body>
 </html>

git show prints the commit’s details and then its diff: exactly what that snapshot changed.

Tip

You do not have to type the whole hash. The first seven characters are enough, and even four usually work.

If something went wrong

fatal: ambiguous argument '358630b': unknown revision or path not in the working tree. You typed the hash from this page. Your hashes are different: copy one from your own git log --oneline.

The output stops, with : at the bottom. It is longer than the terminal panel. Press Space to see the rest, and q to get your prompt back. To make the panel taller, drag its top edge up.

7

Throw away a change you do not want: git restore

Everyone makes a mess sometimes. Make one on purpose: in index.html, change the heading and delete the line about yourself, so the file looks like this.

index.htmlhighlighted: the lines to add or change
<!DOCTYPE html><html lang="en"><head>  <meta charset="utf-8">  <title>My page</title>  <link rel="stylesheet" href="style.css"></head><body>  <h1>Oops, I broke it</h1>  <p>This is my first web page.</p></body></html>
Terminal
$ git statusOn branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

Look at the second hint: git restore throws away the changes you have not added with git add. Nothing is staged here, so the file goes back to how it was in your last commit.

Terminal
$ git restore index.html$ git statusOn branch main
nothing to commit, working tree clean

Look at index.html in VS Code: the heading and your sentence are back.

Watch out

git restore throws those changes away for good. Git cannot bring them back, because they were never in a commit. Use it only when you are sure.

8

Write messages your future self will thank you for

In a few months, your commit messages are all you will remember about each change. Good messages:

  • say what the commit does, as an instruction: “Add a sentence about me”, “Fix the broken link”, “Change the heading colour”;
  • are short, about 50 characters or fewer, so they fit on one line in git log --oneline;
  • describe one change. If you need “and” to describe it, it may be two commits.
Not so helpful Helpful
update Add a list of hobbies
stuff Fix the spelling of "photography"
changes to index and css and README Three commits, one for each change
9

What you learned

  • git diff shows what changed and is not yet staged: + added, - removed.
  • git add . stages every change in the folder. Check git status first.
  • git log --oneline lists your history, one commit per line; git show HASH shows one commit.
  • git restore FILE throws away changes to a file that you have not staged. They cannot be recovered.
  • A good message says what the commit does, briefly.
Command What it does
git diff Shows changes that are not staged
git diff --staged Shows changes that are staged
git add . Stages every change in the current folder
git log --oneline Lists commits, one per line
git show HASH Shows one commit and its changes
git restore FILE Discards changes to a file that are not staged
10

Check yourself

1
In git diff, what does a line starting with + mean?
2
You ran git add index.html, then git diff, and nothing was printed. Why?
3
You want to look at what a commit from yesterday changed. Which command?
4
Which is the better commit message?
Finished this lesson?