Skip to main content

Getting Started with Git — Chapter 2 / 4

The fundamental cycle

Git works with 3 zones and a few commands. This chapter teaches you the complete cycle, from a modified file to a saved commit.

Chapter 2 / 4

Step 01 / 10

The 3 zones of Git

Before learning the commands, you need to understand the 3 zones through which your files move. Each Git command moves files from one zone to another.

Zone 1: Working Directory (your desk)

This is the folder on your computer. You create, modify, and delete files as usual. Git watches this folder, but doesn't save anything until you ask it to.

The analogy

It's your physical desk: you work there, put down papers, scribble notes. Nothing is archived until you put it away.

Zone 2: Staging Area (the envelope)

This is an intermediate zone where you prepare what will go into the next commit. You choose precisely which files (or which changes) you want to save.

The analogy

It's an open envelope on your desk. You place the documents you want to send inside. As long as you haven't sealed it (commit), you can still add or remove documents.

Zone 3: Repository (the filing cabinet)

This is the complete history of your project, stored in the hidden .git folder. Each commit is a permanent, immutable snapshot.

The analogy

It's the filing cabinet: once a document is filed, it stays there forever. You can look it up at any time, but you don't modify it.

1

Working Directory -> git add -> Staging Area -> git commit -> Repository

Step 02 / 10

git status: see where you stand

Concept

git status shows the state of your files across the 3 zones. It's your compass in Git.

git status shows 3 categories of files:

  • In green (Changes to be committed) — files in the Staging Area, ready for the commit
  • In red (Changes not staged for commit) — modified files not yet added to the Staging Area
  • Untracked files — new files that Git doesn't know about yet

$ git status

On branch main

Changes to be committed:
  new file:   about.html

Changes not staged for commit:
  modified:   index.html

Untracked files:
  style.css

Step 03 / 10

git diff: see what changed

Concept

git diff shows line-by-line differences between two versions of a file.

By default, git diff compares the Working Directory to the Staging Area (what has changed but is not yet staged):

$ git diff

--- a/readme.md
+++ b/readme.md
@@ -1,3 +1,4 @@
 # My project

-Short description
+Detailed project description
+Added a line

Lines starting with + are additions (green). Lines starting with - are deletions (red). The rest is context.

To see what is in the Staging Area (what will be committed):

$ git diff --staged

Practice

Best practice: always run git diff --staged before committing to verify exactly what will be recorded.

Step 04 / 10

git add: prepare your files

Concept

git add moves files from the Working Directory to the Staging Area. It's the preparation step before the commit.

Add a specific file:

$ git add readme.md

Add all modified and new files:

$ git add .

Warning

Be careful with git add . — it adds ALL modified and untracked files in the current directory. Always check with git status first, to avoid including unwanted files.

Which command stages a file for the next commit?

Step 05 / 10

git commit: save

Concept

git commit takes everything in the Staging Area and creates a permanent snapshot in the history.

$ git commit -m "Add the About page"

[main a1b2c3d] Add the About page
 1 file changed, 12 insertions(+)

Practice

Best practice: one commit = one logical action. Write a clear message in the imperative mood.

  • Good: "Add form validation"
  • Good: "Fix display bug on mobile"
  • Bad: "fix" (too vague)
  • Bad: "Various changes" (not informative)

Step 06 / 10

git restore: undo changes

Concept

git restore undoes modifications in the Working Directory or removes files from the Staging Area.

Undo changes to a file in the Working Directory (go back to the last commit version):

$ git restore index.html

Warning

Warning: git restore is irreversible for uncommitted files! Lost changes cannot be recovered.

Remove a file from the Staging Area (unstage it) without losing your changes:

$ git restore --staged index.html

The file goes back to the "modified but not staged" state. Your changes are still there.

“git restore on an uncommitted file can be undone.” True or false?

Step 07 / 10

Summary: the complete cycle

1

1. You modify files (Working Directory)

2

2. You check the state with git status

3

3. You review changes with git diff

4

4. You prepare files with git add

5

5. You save the snapshot with git commit

git status
See the state of your files
git add
Stage a file
git commit
Save a snapshot
git restore
Discard changes

Step 08 / 10

Common mistakes

  • git commit -am doesn't include untracked files. New files must be added with git add first.
  • Forgetting git add before git commit — the commit will be empty or incomplete if files are not in the Staging Area.
  • Making giant commits with lots of changes — prefer small, focused commits, one per logical action.

Step 09 / 10

Going further

  • Git cheat sheet — essential commands on one page
  • Undo a commit — how to go back properly
  • Git simulator — practice commands in an interactive environment

Step 10 / 10

Test your knowledge

Before the exercises, make sure the Git cycle clicked. Pick the answer that looks right.

Quiz — Chapter 2

1. What is the role of the staging area?

2. What does git restore --staged readme.md do?

3. What is the difference between git diff and git diff --staged?

4. Why is git commit -am not always enough?

Your feedback matters

Help us improve this course by sharing your thoughts.

So we can follow up if needed.