← Git

Git Fundamentals

What are the three areas of Git and how does a change move between them?

Show answer — try answering out loud first

They are the working directory (your working folder, where you edit files), the staging area or index (the preparation zone where you choose what goes into the next commit), and the repository (the .git, where commits are stored). A change moves from working to staging with git add, and from staging to the repository with git commit.

This is the most important mental model in Git. Understanding it well clears up most doubts.

  1. Working directory: the folder with your files. Here you edit, create, and delete. Changes here are not yet "saved" in Git.
  2. Staging area (index): an intermediate zone where you place the changes you want to include in the next commit. It helps you build clean commits: you can stage only some files and leave others out.
  3. Repository (.git): where Git stores the snapshots (commits) permanently in the history.

The flow is: you edit in the working directory, choose what to save with git add (it moves to staging), and confirm with git commit (it moves to the repository).

# 1. You edit a file in the working directory (for example, you create index.html)

# 2. You check the status: index.html shows up as "untracked" or "modified"
git status

# 3. You move the change to the staging area
git add index.html
# Now git status shows it in green, under "Changes to be committed"

# 4. You confirm: the change moves from staging to the repository as a commit
git commit -m "Agrega la página inicial"
# Output: [main abc1234] Agrega la página inicial

# git diff        -> shows working directory changes that are NOT staged
# git diff --staged -> shows staged changes that have NOT been committed yet

Thinking that git commit saves everything you changed. In reality, commit only saves what is in the staging area. If you edited a file but didn't run git add, that change does not go into the commit. That's why sometimes people "commit" and their change doesn't show up: they forgot the add.

"Git has three areas: the working directory, where I edit files; the staging area or index, where I prepare what I want to include in the next commit; and the repository, the .git, where commits are stored. The flow is: I edit, I run git add to move changes to staging, and git commit to save them in the repository. Staging lets me build clean commits by choosing exactly what to include."

Quick challenge

You edited two files, a.txt and b.txt, but you only ran git add a.txt and then git commit. What happens to b.txt?

Show answer

b.txt does not go into the commit: its changes stay in the working directory as "modified", outside staging. The commit only includes what was in the staging area, that is, a.txt. b.txt will remain pending until you run git add b.txt and another commit.