Sub topic 4
How Git thinks: snapshots and the three areas
This chapter assumes you can open a terminal and run simple shell commands. You do not need Git installed yet to follow the model. Chapter-level commands are shown so you can try them later.
Commits are snapshots
Many older tools record a file's history as a list of changes, called deltas. Git works differently. Each time you commit, Git records a snapshot of every tracked file as it looks at that moment.
A commit is a saved snapshot of your project plus a message, an author, a timestamp, and a link to the commit that came before it. If a file did not change, Git does not store a second copy. It points to the copy it already has. Snapshots are therefore cheap, even for large projects.
Each commit gets an ID, a 40-character hash computed from its contents. Because the parent link is part of that hash, changing an old commit would change every ID after it. This is how Git detects corruption and tampering.
The three areas
Git tracks your work in three places on your computer. A change moves through them in order.
| Area | What it holds | Where it lives |
|---|---|---|
| Working tree | The files you see and edit | Your project folder |
| Staging area (also called the index) | The changes chosen for the next commit | Inside the `.git` folder |
| Repository | The committed history | Inside the `.git` folder |
The working tree is the normal folder on disk. The staging area is a holding zone where you assemble the next snapshot. The repository is the database of all commits, stored in a hidden .git folder at the top of your project.
The staging area lets you choose what goes into a commit. You might edit five files but commit only two, because the other three belong to a separate piece of work.
File states
Git sorts each file in your project into a state, based on which area holds which version.
- Untracked: Git has never been told about the file.
- Modified: the file is tracked, and the working tree version differs from the last commit. The change is not staged.
- Staged: the change is in the staging area and will go into the next commit.
- Committed: the version is safely stored in the repository.
Worked example
This sequence walks one file through every state. Run it in an empty folder once Git is installed.
mkdir demo && cd demo
git init
echo "Hello, Git" > hello.txt
git status
git add hello.txt
git status
git commit -m "Add hello.txt"
git log --onelineHere is what happens at each step.
git initcreates the.gitfolder. The repository now exists but is empty.- After you create
hello.txt, the firstgit statuslists it as untracked. git add hello.txtcopies the file's current contents into the staging area. The secondgit statuslists it under "Changes to be committed".git committurns the staged content into a snapshot in the repository and records your message.git log --onelineshows one line per commit: a shortened ID and the message.
Git will ask for your name and email before it can commit. Setting those is covered in a later chapter.
A common mistake: staging is a snapshot too
git add stages the file as it is at that moment. If you edit the file again afterward, the new edit is not staged. Try it.
echo "Second line" >> hello.txt
git add hello.txt
echo "Third line" >> hello.txt
git statusThe status output shows hello.txt in both "Changes to be committed" and "Changes not staged for commit". The staged version has the second line. The working tree version also has the third. A commit now would record only the second line. Run git add hello.txt again to stage the third.
Why this model matters
- Almost every operation is local. Git reads from the
.gitfolder on your disk, so commits and history browsing work offline. - Because commits are complete snapshots, you can move to any past commit and get the whole project as it was.
- The staging area gives you control over what each commit contains, which keeps history readable.
Exercise
You edit a.txt and b.txt, then run git add a.txt and git commit -m "Update a". Which file changes are in the new commit, and what is the state of b.txt afterward?
Show answer
Only the change to a.txt is in the commit, because only that change was in the staging area. b.txt is still modified in the working tree and not staged.
Exercise
You run git add notes.txt, then append a new line to notes.txt, then run git commit -m "Notes". Does the commit include the new line? How do you include it?
Show answer
No. The commit contains the version staged at the time of git add. Run git add notes.txt again before committing (or commit it in a second commit) to include the new line.