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

Lesson 3Your first repository

Make a folder for your project, turn it into a Git repository, write a small web page and save your first snapshot of it.

⏱ 40 minYou need: Lesson 2 (Git and VS Code installed, your name and email set)

In this lesson you will

  • Open a project folder in VS Code and find your way in the terminal
  • Turn the folder into a Git repository with git init
  • See what Git notices with git status
  • Save a snapshot with git add and git commit
  • Read your history with git log
1

Make a folder for your project

Your project is a small personal web page. It lives in a folder called my-page.

In VS Code
  1. Open the File menu and click Open Folder....
  2. In the window that appears, go to your home folder, the folder with your user name:
    • Windows: click in the address bar at the top of the window, type %USERPROFILE% and press Enter.
    • macOS: press Cmd+Shift+H.
    • Linux: click Home in the list on the left.
  3. Make a new folder there called my-page: click New folder (on a Mac, New Folder; on Linux, the folder icon with a plus sign), type my-page and confirm.
  4. Select my-page and click Open (on Windows, Select Folder).
  5. VS Code asks: “Do you trust the authors of the files in this folder?” You are the author, so click Yes, I trust the authors.
  6. Open a terminal: Terminal > New Terminal.

The terminal opens inside your project folder. To check where you are, type pwd (“print working directory”):

Terminal
$ pwd/Users/you/my-page

The answer is the path to your folder. It starts with your home folder, so yours looks a little different: on Windows it looks like /c/Users/you/my-page, on Linux like /home/you/my-page.

Now type ls (“list”) to see what is in the folder:

Terminal
$ ls

Nothing is printed, because the folder is empty. That will change soon.

Tip

pwd and ls are the two commands to type whenever you are not sure where you are. Most mistakes in the terminal happen because a command ran in a different folder from the one you meant.

2

Turn the folder into a repository

So far my-page is an ordinary folder. Ask Git what it thinks of it:

Terminal
$ git statusfatal: not a git repository (or any of the parent directories): .git

This message looks alarming, but it only means “Git is not looking after this folder”. You will see it whenever you run a Git command in the wrong folder, so it is useful to know it.

To ask Git to look after the folder, type:

Terminal
$ git initInitialized empty Git repository in /Users/you/my-page/.git/

On Windows, Git writes the same path the Windows way: C:/Users/you/my-page/.git/.

git init (“initialise”) turns the folder into a repository: a folder whose history Git keeps. You only do this once per project.

Git keeps that history in a hidden folder called .git, inside your project. ls -a lists hidden files too:

Terminal
$ ls -a.  ..  .git

(. means “this folder” and .. means “the folder above it”. Ignore them. In Git Bash on Windows, folder names end in /: ./ ../ .git/.) You never need to open or change anything inside .git. VS Code hides it from its file list so you cannot delete it by accident.

Watch out

Deleting the .git folder deletes your whole history. Your files stay, but Git forgets every snapshot.

Ask Git again:

Terminal
$ git statusOn branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

This time Git answers properly. “On branch main” is the name of your project’s main line (you set it in Lesson 2; branches come in Lesson 5). “No commits yet” means there are no snapshots so far. Git calls a snapshot a commit.

If something went wrong

git init says “Reinitialized existing Git repository”. You ran it twice. That is harmless.

The path in the git init message is not your my-page folder. You ran it in another folder, and that folder is now a repository. If it was your home folder, undo it. Type pwd and check that the answer is your home folder: it ends with your user name, not with my-page. Then type rm -rf .git. This removes only the history Git had just started there; your files are not touched. Then open my-page in VS Code as shown above.

3

Write your web page

A web page is a text file whose name ends in .html.

In VS Code
  1. In the file list on the left (the Explorer), hover over the MY-PAGE heading and click the New File... icon (a page with a plus sign). If you do not see the file list, click the top icon (two sheets of paper) in the narrow bar on the far left.
  2. Type the name index.html and press Enter. The file opens in the editor.
  3. Click Copy on the box below, click in the empty file in VS Code, and paste with Ctrl+V (Cmd+V on a Mac). With Auto Save on (Lesson 2), VS Code saves it for you.
Tip

Paste file contents rather than type them. As you type HTML, VS Code adds closing tags such as </p> for you, so typing a whole line yourself leaves an extra one.

index.html
<!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></body></html>

You do not need to know HTML for this series. The lines that matter are <h1>, a heading, and <p>, a paragraph.

Want to see it as a web page? In the Explorer, right-click index.html and choose Reveal in File Explorer (Windows), Reveal in Finder (macOS) or Open Containing Folder (Linux). Then double-click index.html: it opens in your web browser.

4

Ask Git what it sees

Click in the terminal panel at the bottom, so that what you type goes there and not into your file. Then type:

Terminal
$ git statusOn branch main

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track)

Git has noticed the new file, but it is untracked: Git sees it and is not looking after it yet. Notice that Git tells you what to do next, in brackets. git status often does that; when in doubt, run it and read the hints.

If something went wrong

On a Mac, git status also lists .DS_Store. It is a hidden file that Finder made. Type the two commands of “On a Mac, one more setting” in Lesson 2, Step 6, then run git status again: .DS_Store is no longer listed.

5

Choose what goes into the snapshot: git add

Saving a snapshot takes two steps. First you choose what goes into it:

Terminal
$ git add index.html

No output means it worked. Ask Git again:

Terminal
$ git statusOn branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   index.html

index.html has moved to “Changes to be committed”. Git calls this place the staging area: the things you have chosen for your next snapshot.

Think of it like this

Think of a group photo. git add asks people to step into the picture. git commit takes the photo. Anyone who has not stepped in is not in the photo, even if they are in the room.

Why two steps? Because you often change several files but want to save them as separate, meaningful snapshots, a few at a time. For now, you will always add, then commit.

If something went wrong

fatal: pathspec 'index.html' did not match any files. Git cannot find a file with that name here. Check the spelling with ls. If ls does not show index.html, the file was saved in another folder or under another name: look at the file’s name in VS Code’s tab and in the Explorer.

6

Take the snapshot: git commit

Terminal
$ git commit -m "Add my first web page"[main (root-commit) c50e3a0] Add my first web page
 1 file changed, 11 insertions(+)
 create mode 100644 index.html

git commit takes the snapshot; -m (“message”) gives it a short description. Every commit needs a message. Write it as an instruction that says what the commit does, like “Add my first web page”.

What the answer means:

  • main is the branch the commit was added to. root-commit means it is the very first one.
  • c50e3a0 is the commit’s hash, its unique name. Yours will be different: it is calculated from the content, your name and the exact time.
  • 1 file changed, 11 insertions(+): one file, eleven new lines.
  • create mode 100644 index.html: a new file. You can ignore the number.

Check the status once more:

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

“Working tree clean” means that your files are exactly as in your last snapshot. This is the calm state to aim for at the end of each piece of work. (“Working tree”, and in some hints “working directory”, is Git’s name for your working folder: the files you see and edit.)

If something went wrong

Author identity unknown and “Please tell me who you are”. Git does not know your name and email yet. Do Step 5 of Lesson 2, then run the git commit command again.

I typed git commit without -m, and a tab called COMMIT_EDITMSG opened in VS Code. Git is asking you for the message. Type it on the first line, save the file, and close the tab. The commit is made when the tab closes. To give up, close the tab without typing anything.

nothing added to commit. You skipped git add. Run git add index.html, then commit.

7

Read your history: git log

Terminal
$ git logcommit c50e3a0c6ce098bd6f6ca0fa80fcc20a3965c2d8 (HEAD -&gt; main)
Author: Your Name <12345678+your-username@users.noreply.github.com>
Date:   Wed Nov 4 10:11:00 2026 +0000

    Add my first web page

git log lists your commits, newest first. Each one shows:

  • commit and the full hash. Git usually shows only the first seven characters, as in c50e3a0.
  • (HEAD -> main): this is the commit you are on now, at the tip of main. Lesson 5 explains it.
  • Author and Date: who made the commit and when.
  • The message, indented.
Tip

When the history is longer than your screen, git log shows one screenful at a time and waits. Press Space to see more, and q to quit and get your prompt back.

8

The three places your work can be

You have now met the three places Git thinks about:

The three areas: working folder, staging area, repositoryWorking folderyour files, as you edit themStaging areawhat goes into the next commitRepository (.git)every commit you madeindex.htmlnew or changedindex.htmlready to commitc50e3a0Add my first web pagea commitgit addgit commitgit status shows what is in each place
Figure 3.1

The three areas. You edit files in your working folder. git add copies changes into the staging area, where you collect what goes into the next snapshot. git commit saves everything in the staging area as a new commit in the repository, inside the .git folder.

Place What it holds How things get there
Working folder Your files, as you see them in VS Code You edit them
Staging area The changes chosen for the next commit git add
Repository Every commit you made git commit

git status tells you what is in each place right now. Try it below: click the buttons in any order, and watch where index.html is and what git status says.

Try it

A small model of your repository with one file. Try Edit index.html, then git commit without git add first; then edit the file again after adding it. The git status text is exactly what Git prints in each situation.

9

What you learned

  • git init turns a folder into a repository, once per project. The history lives in .git.
  • Saving a snapshot takes two steps: git add chooses, git commit -m "..." saves.
  • git status shows what Git sees and hints at what to do next. Run it often.
  • git log lists your commits, newest first.
Command What it does
pwd Shows which folder the terminal is in
ls, ls -a Lists the files in the folder (-a: hidden ones too)
git init Turns the current folder into a repository
git status Shows what has changed and what is staged
git add FILE Puts a file’s changes into the staging area
git commit -m "MESSAGE" Saves the staged changes as a commit
git log Lists the commits (press q to quit)
10

Check yourself

1
You see fatal: not a git repository. What is the most likely reason?
2
You created about.html and committed straight away with git commit -m "Add about page". Git says “nothing added to commit”. Why?
3
What does “working tree clean” mean?
4
Why is your commit hash different from the one in this lesson?
Finished this lesson?