Skip to main content

Getting Started with Git · Chapter 2 / 5

Track your changes

Git sorts your files into 3 areas. In this chapter, you'll learn to read their state and choose exactly what to save.

Chapter 2 / 5

Step 01 / 13

In this chapter

Your repository is ready. Before saving a version, you need to understand how Git sees your files. That's what this chapter is about.

By the end of this chapter, you will be able to:

  • Explain Git's 3 areas: working directory, staging area, and repository
  • Read the output of git status
  • Stage files with git add
  • Unstage a file with git restore --staged
  • See what changed with git diff
  • Undo a change with git restore

Tip

We'll keep working on the my-portfolio project from chapter 1. If you're jumping in here, create a folder containing an index.html file and run git init in it.

Step 02 / 13

Git's 3 areas

Git sorts your files into 3 areas. Every command moves files from one area to another. Keep this diagram in mind and you'll understand every command in this course.

  • Working directory: your folder, where you edit your files.
  • Staging area: where you put the files that will go into your next version.
  • Repository: the history of all your saved versions, stored in the .git folder.

Working Directory

your working folder

git add

Staging Area

where you prepare a commit

git commit

Repository

the commit history (.git)

Each command moves your files from one area to the next.

The analogy

Back to our photographer. The camera is the working directory: you take as many photos as you like. The selection tray is the staging area: that's where you put the photos you want to keep. The notebook is the repository: you stick your selection in it, with the date and a caption. You never stick in everything, only what you picked.

1

You edit your files in the working directory

2

git add puts them in the staging area

3

git commit saves them to the repository (chapter 3)

📷 The camera
— Working directory (your folder)
🗂️ The selection tray
— Staging area (your selection)
📖 The notebook
— Repository (the history)

Step 03 / 13

The working directory: your workspace

The working directory is simply your project folder, as you see it in your file explorer or your code editor. For example:

# The my-portfolio folder

📁 my-portfolio
├── 📁 .git
├── 📁 images
│   └── 📄 photo.jpg
├── 📄 index.html
├── 📄 style.css
└── 📄 README.md

You work in it as usual: you create, edit, and delete files. Git keeps an eye on this folder, but it doesn't save anything on its own. You decide what to save, and when.

From Git's point of view, each file is in one of these states:

  • Untracked: a new file that Git has never saved.
  • Modified: a tracked file that has changed since the last saved version.
  • Staged: a file placed in the staging area, ready to be saved.
  • Committed: a file that matches the last saved version. Git has nothing to report.

Step 04 / 13

Check the state of the repository: git status

Concept

git status is your compass. It tells you which branch you're on and what state your files are in. Run it as often as you like: it doesn't change anything.

Here's what it shows in a project that already has a few saved versions:

$ git status

On branch main

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   README.md

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)
	contact.html

The output is split into sections. Each one corresponds to a state:

  • Changes to be committed: the files in the staging area, ready to be saved. Here, README.md.
  • Changes not staged for commit: modified files that aren't staged yet. Here, index.html.
  • Untracked files: new files that Git doesn't track yet. Here, contact.html.

Tip

In most terminals, staged files are shown in green and the others in red. But rely mainly on the section titles: they say the same thing without depending on color. Git even suggests, in parentheses, which command to run next.

When everything is saved, git status tells you so:

$ git status

On branch main
nothing to commit, working tree clean

In git status, under which heading does a new file that Git has never tracked appear?

Step 05 / 13

The staging area: a buffer zone

Why not save everything at once? Because a good version should be about one thing only.

Say you fixed a typo in README.md and, at the same time, started reworking the menu in index.html. Those are two separate changes. The staging area lets you save them separately: first the README fix, then the new menu once it's finished.

The analogy

The staging area is like the shopping cart on an online store. You add items, remove some, and review them. Nothing is bought until you place the order. Here, placing the order means making a commit.

Concept

You'll also come across the word index: it's another name for the staging area. Adding a file to the index means putting it in the staging area.

Step 06 / 13

Add files: git add

To stage one or more files, use git add followed by the file names:

$ git add <file1> <file2> ...

For example, to stage only the README fix:

$ git add README.md

$ git status

On branch main

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   README.md

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

README.md moved to the Changes to be committed section. index.html stayed where it was: it won't be part of the next version.

To add all new and modified files in the folder at once, use a dot:

$ git add .

Warning

git add . adds everything, including files you might not want to save (temporary files, passwords, and so on). Always run git status first so you know what you're adding. In chapter 3, you'll learn how to exclude files with .gitignore.

Tip

git add only stages your changes. Nothing is saved to the history yet: that's what git commit is for.

You want to stage only README.md, not the other modified files. Which command do you use?

Step 07 / 13

Remove files from the staging area

Added a file by mistake? Don't panic: git restore --staged removes it from the staging area.

$ git restore --staged <file1> <file2> ...

$ git restore --staged index.html

The file goes back to the Changes not staged for commit section. Your changes are still in the file: you just changed your mind about what to save.

To clear the staging area completely:

$ git restore --staged .

Tip

In older tutorials, you'll see git reset HEAD <file>. It does the same thing: git restore --staged is the newer version, and it's easier to remember.

“git restore --staged erases your changes.” True or false?

Step 08 / 13

See what changed: git diff

git status tells you which files changed. git diff shows you what changed inside them, line by line:

$ git diff

diff --git a/index.html b/index.html
index 3b18e51..a8c4f2d 100644
--- a/index.html
+++ b/index.html
@@ -5,3 +5,3 @@
 <body>
-  <h1>Welcome</h1>
+  <h1>Welcome to my portfolio</h1>
 </body>

How to read this output:

  • A line starting with - was removed.
  • A line starting with + was added.
  • Lines with no sign are just context: they haven't changed.
  • So a modified line shows up twice: the old version with -, the new one with +.

By default, git diff shows the changes that aren't staged yet. Two useful variations:

# Changes in a single file

$ git diff index.html

# What's in the staging area, so what will be saved

$ git diff --staged

Practice

Good habit: run git diff --staged right before you save. That way, you see exactly what will go into the new version.

Tip

If the output is long, Git shows it page by page. Use the arrow keys to scroll and press q to quit.

In the output of git diff, what does a line starting with + mean?

Step 09 / 13

Undo a change: git restore

Edited a file and changed your mind? To go back to its last saved version, use git restore with no options:

$ git restore <file>

$ git restore index.html

index.html is back exactly as it was in the last saved version.

Warning

Careful: this can't be undone. The changes you discard with git restore were never saved, so Git can't get them back. Check with git diff before you discard anything.

Don't confuse the two:

  • git restore --staged <file>: removes the file from the staging area. Your changes are kept.
  • git restore <file>: discards your changes. The file goes back to its last saved version.

“Changes discarded with git restore can be recovered.” True or false?

Step 10 / 13

Recap: tracking your changes

1

You edit your files in the working directory

2

git status: you check where your files stand

3

git diff: you look at what changed

4

git add: you stage what you want to save

5

git diff --staged: you review what you staged

Your cheat sheet for this chapter:

$ git status # the state of your files

$ git add <file> # stage a file

$ git add . # stage the whole folder

$ git restore --staged <file> # unstage a file

$ git diff # unstaged changes

$ git diff --staged # staged changes

$ git restore <file> # discard changes (permanent)

git status
— See the state of your files
git add
— Stage a file
git restore --staged
— Unstage a file
git diff
— See the changed lines
git restore
— Discard a file's changes

Step 11 / 13

Common mistakes

  • Running git add . without looking: you stage files you didn't mean to. Run git status first.
  • Thinking git add saves your work: it only stages it. Until you make a commit, nothing is in the history.
  • Editing a file after git add: the staging area keeps the version from the moment you ran git add. The file then shows up in two sections of git status. Run git add again to include your latest changes.
  • Mixing up git restore and git restore --staged: the first discards your changes, the second keeps them.
  • Getting stuck in git diff: press q to get back to the terminal.

Step 12 / 13

Bonus: under the hood

Tip

This part is optional, for the curious. Feel free to skip straight to the quiz.

When you run git add, Git doesn't just write down the file name. It immediately copies the file's content into the .git/objects folder, as an object called a blob.

The staging area itself is just a file: .git/index. It lists the staged files, each with the identifier of its blob. You can display it:

$ git ls-files --stage

100644 8b137891791fe96927ad78e64b0aad7bded08bdc 0	README.md
100644 a8c4f2d6e1b3907f5d2c4e8a1b6f3d9c0e7a2b54 0	index.html
100644 3c6e1f9a2d8b4057e1c3a9f6d2b8e4c7a1f5d3e0 0	style.css

The long string of numbers and letters is the identifier (the hash) of each file's content. In chapter 3, you'll see how Git uses it to identify each version.

Concept

That's why editing a file after git add doesn't change what's staged: Git already copied the content when you ran git add.

Step 13 / 13

Test your knowledge

Make sure you can read the state of your files and prepare a commit. Click the answer you think is right: each answer comes with an explanation.

Quiz · Chapter 2

1. In which order do your files move from one area to the next?

2. In git status, a file shows up under "Changes not staged for commit." What does that mean?

3. You ran git add style.css by mistake. How do you remove it from the staging area without losing your changes?

4. In the output of git diff, a line starts with -. What does that mean?

5. You edit index.html right after running git add index.html. What does the staging area contain?

6. Which command shows what will be saved in the next commit?

Chapter 2 complete

Next up: your first commit

In chapter 3, you save your first versions, read your commit history, and push your work to GitHub.

Leave feedback on this chapter (optional)

So we can follow up if needed.

Chapter 2 of 5