Computing Foundations › Git and GitHub › Day 30

Day 30: Git Fundamentals: Repositories, Staging, and Commits

Day 30 of 365 — Git Fundamentals: Repositories, Staging, and Commits

After this lesson you will be able to turn any folder into a Git repository and move your work deliberately through the three areas — working directory, staging area, and repository — with git add and git commit, read state with git status and history with git log, keep secrets out with .gitignore, and explain exactly what a commit is and why its hash makes history trustworthy.

Course
Computing Foundations
Category
Git and GitHub
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/computing-foundations/day-030-git-fundamentals-repositories-staging-and-commits

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/computing-foundations/day-030-git-fundamentals-repositories-staging-and-commits
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: a file moving from the working directory through staging into the repository


Historical background


What it is — and what it is not

A commit is:

It is not:


Why it was created and what problems it solves


How it works

The three areas

Diagram: Git's three areas — working directory, staging area, and repository — with the commands that move a file between them

AreaWhat lives thereHow content arrives
Working directoryThe real files on disk you editYou edit them, with any editor
Staging area (index)The exact changes for the next commitgit add <file>
Repository (.git)The permanent, ordered historygit commit

Reading state with git status

Diagram: how to read the two-column output of git status

git status --short prints two columns per file. Left is the staging area; right is the working directory.

What a commit really is

Diagram: the inputs that produce a commit hash

Diagram: the anatomy of a Git commit — snapshot, author, message, and parent hash — and how commits link into a chain

Ignoring files with .gitignore

Diagram: why a gitignore rule does not affect an already-tracked file

Seeing changes and history

Diagram: git diff compared with git diff --staged


An everyday analogy

Diagram: the contents of a git repository directory

Packing a parcel.

PackingGit
Things scattered on your deskThe working directory
The open box you are fillingThe staging area
Sealed, labelled, on the shelfA commit in the repository
Choosing what goes in the boxgit add
Sealing and labelling itgit commit
The label saying why you sent itThe commit message
Things you deliberately never pack.gitignore

Examples in practice

Diagram: the three inspection commands and the question each answers

Diagram: working directory, staging area and repository


Implications: security, privacy, performance, scalability, and cost


Alternatives: free, open source, and commercial

ToolTypeWhere it fits
git on the command lineFree, open sourceThe reference. Learn this first.
lazygit, tigFree, open sourceTerminal interfaces that make staging visual
Editor integrationBundledStaging individual lines is genuinely easier with a mouse
GitHub Desktop, SourcetreeFreeFriendlier, at the cost of hiding the model
git add -pFree, built inStaging change by change, from the terminal
pre-commitFree, open sourceRunning checks — including secret scanning — before a commit lands

TermWhat it actually is
Working directoryThe files on disk you edit
Staging area / indexThe assembled contents of the next commit
RepositoryThe .git database of all history
CommitOne recorded snapshot plus its metadata
HashThe commit’s identifier, computed from its content
HEADA pointer to the commit you are currently on
TrackedA file Git is recording changes to

When to use it — and when not to

Diagram: staging everything compared with staging selectively


The AI connection

Diagram: a notebook committed with outputs compared with one stripped


Knowledge check

quiz:
  question: "A developer adds `.env` to `.gitignore`, but `git status` still shows it as modified. Why?"
  options:
    - "The file was already tracked, and .gitignore only affects files Git is not tracking"
    - "The .gitignore file must be committed before its rules take effect"
    - "The pattern needs a leading slash to match a file at the repository root"
    - "Ignore rules apply only to the staging area, not the working directory"
  answer_index: 0
  explanation: "Ignoring prevents Git from starting to track a file. Once a file is tracked — because it was committed at some point — Git keeps reporting changes to it, and the ignore rule is simply not consulted until the file is explicitly untracked."

Hands-on exercise

Watch one file travel through all three areas. Worked through fully in the Day 30 lab directory.

What you are doingCommand
Start and check stategit init then git status -s
Ignore before committingecho ".env" > .gitignore
Create a file and see it untrackedtouch main.py then git status -s
Stage itgit add main.py then git status -s
Edit again after stagingedit, then git status -s
See both diffsgit diff and git diff --staged
Commit and read the historygit commit -m "..." then git log --oneline

Expected output

?? main.py
A  main.py
AM main.py
712412c Add request handler with error checking

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. What does running git init do to an ordinary folder?

  1. It uploads the folder to a hosting website automatically
  2. It deletes any files Git does not recognise
  3. It commits every file in the folder immediately
  4. It creates a .git directory, making the folder a repository
Show answer

Answer: D. It creates a .git directory, making the folder a repository

git init creates the hidden .git directory, which is the repository's memory. The folder is now a repository with an empty history; nothing is committed or uploaded until you tell Git to do so.

Q2. Which command moves a change from the working directory into the staging area?

  1. git add
  2. git log
  3. git commit
  4. git status
Show answer

Answer: A. git add

git add copies changes from the working directory into the staging area (the index), where you assemble the exact snapshot for the next commit. git commit then records what is staged.

Q3. In git status --short, what does the code ?? in front of a file name mean?

  1. The file is staged and ready to commit
  2. The file is untracked — seen but not recorded yet
  3. The file is tracked and modified but not staged
  4. The file has a merge conflict
Show answer

Answer: B. The file is untracked — seen but not recorded yet

?? marks an untracked file: it exists in the working directory, but Git is not yet recording its history. Running git add on it stages it, after which status shows A instead.

Q4. What is a Git commit, precisely?

  1. A list of only the lines that changed since the last save
  2. A backup copy stored on a remote server
  3. A snapshot plus author, timestamp, message, and parent hash
  4. A temporary draft that disappears when you close the terminal
Show answer

Answer: C. A snapshot plus author, timestamp, message, and parent hash

A commit records a full snapshot of the tracked files, wrapped with the author, time, message, and the parent commit's hash, and Git hashes all of that into the commit's own identifier. It is not merely a diff, a backup, or a draft.

Q5. Why do two people who commit files with identical contents still get different commit hashes?

  1. Git assigns hashes at random
  2. They do not — identical contents always produce identical hashes
  3. Hashes depend on the operating system, not the commit
  4. The hash covers author, timestamp, and parent, which differ
Show answer

Answer: D. The hash covers author, timestamp, and parent, which differ

A commit's hash is a fingerprint of its full contents: the snapshot AND the author, timestamp, and parent hash. Since at least those metadata differ, the hashes differ even when the file bytes match.

Q6. A teammate committed a secret API key last week, then deleted the file in a newer commit. Are they safe?

  1. No — the key is still in the earlier commit; rewrite and rotate
  2. Yes — commits older than a day are automatically purged
  3. Yes — deleting the file removes it from the whole repository
  4. No — but only if the repository was shared online
Show answer

Answer: A. No — the key is still in the earlier commit; rewrite and rotate

Deleting a file in a new commit does not erase it from earlier commits; the key remains in history. Truly removing it requires rewriting history, and because it may already be exposed, the key should be rotated (revoked and reissued).

Q7. What is the purpose of a .gitignore file?

  1. To list the files Git should commit automatically
  2. To list paths Git should never track
  3. To store your commit messages
  4. To encrypt sensitive files inside the repository
Show answer

Answer: B. To list paths Git should never track

.gitignore lists path patterns Git should leave untracked — build outputs, temp files, and especially secrets. Ignored files never appear as candidates to add, so they cannot be committed by accident. Note it only affects files not already tracked.

Q8. With no arguments, what does git diff compare?

  1. Two different branches of the repository
  2. The staging area against the last commit
  3. The working directory against the staging area
  4. Your repository against a remote server
Show answer

Answer: C. The working directory against the staging area

Plain git diff shows what is changed in the working directory but not yet staged. To see what is staged and about to be committed, use git diff --staged, which compares the staging area against the last commit.

Glossary

repository
A project folder that Git is tracking, together with the hidden .git directory that stores its complete history of committed snapshots.
working directory
The live files on disk that you see and edit right now — the in-progress version of the project, before anything is staged or committed.
staging area
A holding area where you assemble exactly the changes to include in the next commit; git add puts changes here. Also called the index.
index
Git's internal name for the staging area — the list of changes prepared for the next commit.
commit
A permanent, recorded snapshot of the tracked files at one instant, wrapped with an author, timestamp, message, and the parent commit's hash, and identified by its own hash.
snapshot
The complete state of every tracked file at the moment of a commit; Git stores snapshots rather than lists of edits, and computes differences on demand.
hash
A commit's unique identifier — a long hexadecimal fingerprint computed from the commit's full contents (snapshot, author, time, message, and parent); usually shown as the first seven characters.
HEAD
A pointer to the commit you are currently on, normally the newest commit on your branch; it advances to each new commit you make.
parent
The commit that came just before a given commit; the parent reference is what links commits into a chain reaching back to the first commit.
.gitignore
A file listing path patterns Git should not track, so build outputs, temp files, and secrets never get staged or committed by accident.
git add
The command that copies changes from the working directory into the staging area, marking them for inclusion in the next commit.
git commit
The command that records everything currently staged as a new permanent snapshot in the repository, with a message describing the change.
git status
The command that reports which files are untracked, staged, or modified — the window that tells you which area holds each file's current content.
distributed version control
A model, used by Git, in which every clone is a full copy of the repository and its history, so you can commit and inspect history without a central server.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.