Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

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. You will learn how to go from a modified file to a saved commit.

The 3 zones of Git

Everything in Git happens in 3 separate zones. Understanding these zones is the key to understanding Git.

Key term: the 3 zones

Think of it like packing a suitcase for a trip. Your wardrobe is the working directory (all your clothes). The bed where you lay out what you want to bring is the staging area. The suitcase you close and lock is the repository (the saved commit).

Working directory

This is your project folder on your computer. These are the files you see and edit every day.

When you open a file and change something, you are working in the working directory. Git knows the file has changed, but it has not saved the change yet.

Staging area (also called index)

This is the preparation zone. You place files here when you want to include them in the next save.

It's like a shopping basket. You put items in the basket before going to the checkout. You can add items or remove them before paying.

Repository (.git)

This is where all commits are stored permanently. It is the complete history of your project.

Once a commit is in the repository, it is safe. It's like a photo album: every photo is stored and you can look back at any of them.

Here is how files move between the 3 zones:

# The Git workflow

Working Dir → git add → Staging → git commit → Repository

git status: seeing where things are

Key term: git status

git status is the command you will use the most. It tells you what has changed and where each file is. It's like asking "what's in my basket?" before going to the checkout.

Full guide on git status →

The output uses colors to help you understand:

  • Green: files in the staging area. They are ready to be committed.
  • Red: files that have changed but are not yet in the staging area.
  • Untracked: new files that Git does not know about yet.

git diff: seeing what changed

Key term: git diff

This command shows you the details of what changed in your files. While git status tells you which files changed,git diff shows you what exactly changed inside them.

Full guide on git diff →

By default, git diff compares the working directory with the staging area. In other words: what has changed but you have not yet added with git add.

$ git diff

diff --git a/readme.md b/readme.md

--- a/readme.md

+++ b/readme.md

@@ -1,2 +1,3 @@

# My project

-Project description

+Detailed project description

+Version 1.0

Lines in red (starting with -) were removed. Lines in green (starting with +) were added.

$ git diff --staged

# Shows changes already added to the staging area

# Useful to review what your next commit will contain

Good practice

Before running git add, use git diff to review your changes. It's the perfect moment to catch a mistake or a forgotten line before committing.

git add: preparing your changes

Key term: git add

git add copies a file from the working directory to the staging area. It's like picking a shirt from your wardrobe and putting it on the bed. The shirt is not in the suitcase yet. It is just ready to be packed.

Full guide on git add →

You can add files one by one. Or you can add all changed files at once.

$ git add readme.md

# Add one specific file

$ git add .

# Add all changed files

git commit: saving a snapshot

Key term: git commit

git commit takes everything in the staging area and creates a permanent save. It's like closing the suitcase and locking it. Once it is closed, the contents are safe.

Full guide on git commit →

Every commit needs a message. The message explains what you did and why.

$ git commit -m "Add homepage"

[main a1b2c3d] Add homepage

1 file changed

Good practice

One commit = one logical action. Make small commits that do one thing. Write a clear message that explains why, not just "update".

git restore: going back

Key term: git restore

git restore lets you undo changes. It's like using an eraser on a pencil drawing. You go back to the previous version.

Full guide on git restore →

If you changed a file but want to undo your changes, you have two options:

$ git restore readme.md

# Undo changes in the working directory

# The file goes back to the version in the staging area

$ git restore --staged readme.md

# Remove the file from the staging area

# The file stays changed in the working directory

Be careful: git restore without the --staged flag will erase your changes. Make sure this is what you want before running the command.

Common mistakes

  • Using git commit -am. This shortcut does add + commit, but it does not pick up new files. Only files Git already knows about are included.
  • Forgetting to run git add before committing. The commit will be empty or incomplete.
  • Making one huge commit with 15 unrelated changes. Split your work into small, logical commits.
  • Writing commit messages like "update" or "fix". These tell nothing about what was done or why.

Your feedback matters

Help us improve this course by sharing your thoughts.

So we can follow up if needed.