Computing Foundations › Git and GitHub › Day 30
Day 30: 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.
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
- 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 - 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 - 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.
- 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:
- Initialise a repository with git init and describe the role of the hidden .git directory
- Explain Git's three areas — working directory, staging area (index), and repository — and the git add / git commit flow between them
- Read git status output (including the ??, A, and M short codes) to say which area holds a file's current content
- Describe what a commit really is: a snapshot plus author, message, and parent, identified by a content-derived hash, and how commits link into a chain that HEAD points into
- Use .gitignore to keep files (especially secrets) untracked, and use git diff and git log to inspect changes and history
- Connect clean commits to reproducible, reviewable experiments when versioning code, notebooks, prompts, and configuration
Prerequisites
- Day 29: why version control exists
- A terminal and Git installed (git --version prints a 2.x version)
- Comfort creating and editing text files from the command line (Days 1 and 8-12)
Why this matters
-
Git is the tool, and today is the model. Three areas and two commands explain almost everything you will ever do with it.
-
git statusis the command you will type most in your career, and it is only useful once you know what the three areas are. -
Committing a secret is the mistake with no clean undo, and
.gitignoreis the thing that prevents it. -
A commit hash is what makes a result citable. “Run at
712412c” is a claim anyone can check. -
Most Git confusion is not knowing which area a change is in. After today, you will.
The idea in plain language
-
A Git project has three areas, and almost everything is moving content between them.
-
The working directory is the files on disk you are editing right now.
-
The staging area is the set of changes you have chosen for the next commit.
-
The repository is the permanent history of everything committed.
-
git addpromotes;git commitrecords. Two commands, three areas — that is the entire engine.
Historical background
-
Git was written in 2005 for the Linux kernel, in about ten days, after the project lost access to the proprietary system it had been using.
-
Its design priorities were unusual: speed on an enormous codebase, full distribution, and a history nobody could quietly rewrite.
-
The staging area was a genuine innovation and remains slightly controversial — most systems commit directly from the working directory.
-
It exists because commits should be coherent, not merely chronological. Staging lets you assemble a change from part of your work.
-
Content addressing came from the same instinct. A commit is named by a hash of its own content, so any alteration is detectable.
-
The interface was widely criticised for a decade, and improved slowly —
git switchandgit restorearrived in 2019 to split the overloadedcheckout.
What it is — and what it is not
A commit is:
- A full snapshot of every tracked file, not a diff.
- Wrapped in metadata: author, time, message, and the parent’s hash.
- Named by a hash computed from all of that together.
It is not:
-
Not a diff, even though Git shows you diffs. Internally it stores snapshots and computes differences on demand.
-
Not editable. Changing anything about a past commit produces a different hash, and therefore a different commit.
-
Not private. Everything committed is in the history for anyone with the repository.
-
Not automatic. Git records what you stage, and nothing else — which is the point of staging.
Why it was created and what problems it solves
-
Coherent commits. Staging lets you commit the bug fix without the unrelated experiment you also have open.
-
Reviewing before recording.
git diff --stagedshows exactly what you are about to commit, while there is still time to change it. -
Tamper-evidence. Because a hash covers the content, the metadata and the parent, altering any past commit changes every hash after it.
-
Speed. Everything is local, so history, diffs and branches are instant.
-
Keeping the wrong things out.
.gitignoreis how build outputs, dependencies and secrets stay out of a permanent record.
How it works
The three areas
| Area | What lives there | How content arrives |
|---|---|---|
| Working directory | The real files on disk you edit | You edit them, with any editor |
| Staging area (index) | The exact changes for the next commit | git add <file> |
Repository (.git) | The permanent, ordered history | git commit |
-
Read it left to right. Edit,
addto assemble precisely the snapshot you intend,committo record it permanently. -
Committing clears the staging area for the next round.
Reading state with git status
git status --short prints two columns per file. Left is the staging area; right
is the working directory.
??— untracked: Git sees it and is not recording it.A— a new file, staged, ready to commit.M— tracked and modified, but not staged.M— modified, and the modification is staged.MM— staged one change, then edited again. Proof the two areas are genuinely separate.
What a commit really is
-
Git stores a snapshot of every tracked file, wrapped in a small commit object.
-
That object holds a reference to the snapshot, the author and time, your message, and the hash of the parent.
-
Git hashes all of it together to produce the commit’s own identifier — 40 hexadecimal characters, of which you usually see the first seven.
-
Because each commit names its parent, commits form a chain back to the first, which has no parent.
-
Because the hash covers the snapshot, the parent and the metadata, you cannot alter a past commit without changing its hash and every hash after it. The history is tamper-evident by construction.
-
HEAD points to the commit you are on, and advances when you commit.
-
Two people committing identical file contents still get different hashes, because author, timestamp and parent all feed the fingerprint.
Ignoring files with .gitignore
-
Not everything in a folder belongs in history: build outputs, downloaded dependencies, editor temporary files, and above all secrets.
-
.gitignorelists patterns Git should not track.secret.keyignores that file;*.logignores every file ending in.log. -
Ignored files never appear in
git statusas candidates, so you cannot add them by accident. -
The caveat that catches everyone:
.gitignoreonly affects files Git is not already tracking. Commit a secret first and the ignore rule changes nothing until you explicitly untrack it.
Seeing changes and history
-
git diffwith no arguments compares the working directory against the staging area — the edits you have not staged. -
git diff --stagedcompares the staging area against the last commit — what you are about to commit. -
git logwalks the chain backward from HEAD, printing hash, author, date and message. -
git log --onelinecondenses each to a line, and is the fastest way to read history. -
Together,
status,diffandloganswer where everything is, what changed, and how you got here.
An everyday analogy
Packing a parcel.
| Packing | Git |
|---|---|
| Things scattered on your desk | The working directory |
| The open box you are filling | The staging area |
| Sealed, labelled, on the shelf | A commit in the repository |
| Choosing what goes in the box | git add |
| Sealing and labelling it | git commit |
| The label saying why you sent it | The commit message |
| Things you deliberately never pack | .gitignore |
-
The open box is the point. You choose what goes in, rather than sweeping the whole desk into it.
-
Once sealed and shelved, you do not reopen it. You send a second parcel.
-
Some things on the desk should never be posted at all, and deciding that in advance is cheaper than retrieving a parcel.
Examples in practice
-
MMingit status --short: you staged a change and then kept editing. Both versions exist, in different areas. -
git diffshowing nothing whilegit diff --stagedshows plenty: everything is already staged. -
A
.gitignoreadded after the fact, with the ignored file still tracked, because ignoring does not untrack. -
A commit hash in a paper or a log, identifying exactly the code that produced a result.
-
A commit containing a bug fix and an unrelated refactor, which is what staging exists to prevent.
Implications: security, privacy, performance, scalability, and cost
-
Security:
.gitignorebefore the first commit. Adding it afterwards leaves the file tracked and the secret in history. -
A committed secret is compromised, and rewriting history to remove it is disruptive and unreliable. Revoke it — Day 25’s rule.
-
Privacy: your name and email are in every commit, permanently, and publicly in an open repository.
-
Performance: everything is local, so
log,diffandstatusare instant regardless of history length. -
git statusslows on very large working directories, which is the main scaling limit you will meet in practice. -
Cost: a repository grows with every version of every file. Text is cheap; binaries are not, and a large file committed once is in the history forever.
Alternatives: free, open source, and commercial
| Tool | Type | Where it fits |
|---|---|---|
git on the command line | Free, open source | The reference. Learn this first. |
lazygit, tig | Free, open source | Terminal interfaces that make staging visual |
| Editor integration | Bundled | Staging individual lines is genuinely easier with a mouse |
| GitHub Desktop, Sourcetree | Free | Friendlier, at the cost of hiding the model |
git add -p | Free, built in | Staging change by change, from the terminal |
pre-commit | Free, open source | Running checks — including secret scanning — before a commit lands |
-
Learn the command line first. A graphical tool that hides the three areas makes the confusing cases more confusing, not less.
-
Then use whatever is fastest. Staging individual lines is one job a good interface does better than the terminal.
Comparison with related concepts
| Term | What it actually is |
|---|---|
| Working directory | The files on disk you edit |
| Staging area / index | The assembled contents of the next commit |
| Repository | The .git database of all history |
| Commit | One recorded snapshot plus its metadata |
| Hash | The commit’s identifier, computed from its content |
| HEAD | A pointer to the commit you are currently on |
| Tracked | A file Git is recording changes to |
When to use it — and when not to
-
Run
git statusconstantly. It is cheap and it answers the question you actually have. -
Stage deliberately.
git add -plets you commit the fix without the debugging print statements. -
Commit one coherent change at a time, with a message that says why.
-
Write
.gitignorebefore the first commit, not after. -
Do not use
git add .reflexively. It is how unwanted files and secrets get in. -
Do not commit large binaries into the repository. Use LFS or DVC.
-
Do not amend or rewrite commits you have shared. That is Day 34’s subject, and it has real consequences.
The AI connection
-
.gitignoreshould exclude your data and model directories from the start. Datasets and weights do not belong in a repository. -
It should also exclude
.env, notebook checkpoints and cache directories, all of which fill quietly and none of which belong in history. -
Notebooks are a genuine problem. They store outputs inline, so every run produces a huge diff — which is why tools exist to strip outputs before committing.
-
Commit the config alongside the code. A hyperparameter changed and uncommitted is a result you cannot explain.
-
Record the commit hash with every experiment result. It is the single string that turns a number in a table back into something reproducible.
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 doing | Command |
|---|---|
| Start and check state | git init then git status -s |
| Ignore before committing | echo ".env" > .gitignore |
| Create a file and see it untracked | touch main.py then git status -s |
| Stage it | git add main.py then git status -s |
| Edit again after staging | edit, then git status -s |
| See both diffs | git diff and git diff --staged |
| Commit and read the history | git commit -m "..." then git log --oneline |
Expected output
?? main.py
A main.py
AM main.py
712412c Add request handler with error checking
-
??thenAthenAM— the same file, in three different states, over three commands. -
AMmeans staged and then edited again, which is the clearest proof that the areas are separate.
Validate your work
- You produced
??,A,MandAMstates and can explain each - You saw
git diffandgit diff --stagedshow different things at the same time - You wrote
.gitignorebefore your first commit - You can read a commit hash and say what it is computed from
- You used
git add -pto stage part of a file -
bash tests/run_tests.shreports 0 failures
Troubleshooting
-
git statusshows a file you ignored. It was already tracked. Untrack it withgit rm --cached. -
git diffshows nothing but you have changes. They are staged. Use--staged. -
Your commit includes files you did not want.
git add .swept them in. Stage by name, or use-p. -
Git asks who you are. Set
user.nameanduser.email.
Common mistakes
git add .without looking atgit statusfirst.- Adding
.gitignoreafter the first commit, and assuming that handles it. - One enormous commit containing three unrelated changes.
- Committing notebook outputs, producing diffs nobody can read.
Practice assignment
-
Stage three unrelated edits separately with
git add -p, and make three commits from one working session. -
Write a
.gitignorefor a Python AI project from scratch, and justify every line. -
Deliberately track a file you meant to ignore, then untrack it, and observe what remains in the history.
-
Read
git log --onelinefor a project you did not write and identify three commits whose messages actually helped.
Extension challenge
-
Find a commit’s own hash inputs. Use
git cat-file -p HEADto print the raw commit object, and identify the tree, the parent and the metadata that produced its hash. -
Prove the history is tamper-evident. Amend an old commit and observe that every subsequent hash changes.
-
Set up
pre-commitwith a secret scanner, and confirm it blocks a commit containing a key-shaped string. -
Strip notebook outputs automatically with a filter, and compare the diff of a re-run notebook before and after.
Quiz
Q1. What does running git init do to an ordinary folder?
- It uploads the folder to a hosting website automatically
- It deletes any files Git does not recognise
- It commits every file in the folder immediately
- 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?
- git add
- git log
- git commit
- 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?
- The file is staged and ready to commit
- The file is untracked — seen but not recorded yet
- The file is tracked and modified but not staged
- 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?
- A list of only the lines that changed since the last save
- A backup copy stored on a remote server
- A snapshot plus author, timestamp, message, and parent hash
- 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?
- Git assigns hashes at random
- They do not — identical contents always produce identical hashes
- Hashes depend on the operating system, not the commit
- 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?
- No — the key is still in the earlier commit; rewrite and rotate
- Yes — commits older than a day are automatically purged
- Yes — deleting the file removes it from the whole repository
- 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?
- To list the files Git should commit automatically
- To list paths Git should never track
- To store your commit messages
- 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?
- Two different branches of the repository
- The staging area against the last commit
- The working directory against the staging area
- 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
- Git Basics — Recording Changes to the Repository (Pro Git) — Chacon & Straub / Pro Git (accessed 2026-07-12) — The canonical walkthrough of the working directory, staging area, and committing.
- git-commit Documentation — Git (accessed 2026-07-12) — Official reference for what git commit records and its options.
- git-add Documentation — Git (accessed 2026-07-12) — Official reference for staging changes into the index.
- Git — Wikipedia (accessed 2026-07-12) — History of Git's creation in 2005 and its distributed, content-addressed design.
- The Missing Semester — Version Control (Git) — MIT (accessed 2026-07-12) — A concise, model-first treatment of Git's internals and everyday use.
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.