Git — Saving Snapshots
You change the bookshop's order code on Friday, and on Monday checkout is broken. What exactly changed, and how do you get back to Friday morning? Without version control you are guessing. With Git, every saved step is a snapshot you can inspect, compare and return to.
The rule
Edit, stage, commit: each commit is a snapshot with a reason.
Git sees your project in three places:
- Working directory: the files as they are on disk right now, including half-finished edits.
- Staging area: the changes you have chosen for the next snapshot.
- Repository: the permanent history of commits, stored in the hidden
.gitfolder.
A commit records the staged state of the project, together with an author, a time, a message and a link to the commit before it. Each commit gets a unique id (a hash such as a1b2c3d), and together they form a chain you can walk back through.
git init # start tracking this folder
git status # what changed? what is staged?
git diff # show unstaged line changes
git add app.py # stage one file
git commit -m "Return 404 for unknown book ids"
git log --oneline # list the history, newest firstStaging exists so that one commit can hold one idea. If you fixed a bug and also reworded the README, stage and commit them separately; later, each change can be understood or undone on its own.
A good message says what changed and why in a short line: "Return 404 for unknown book ids" beats "fix stuff".
Some files must never be committed: secrets such as .env with the database password, and generated folders such as node_modules or __pycache__. List them in a .gitignore file:
.env
__pycache__/
node_modules/The shortcut
Before every commit, run the status → diff → add → commit loop and ask: "Is everything I staged part of one idea, and nothing secret?"
Status
git statusshowsapp.pyandREADME.mdmodified, and a new untracked.env.
The trap: git add . stages everything, including secrets. Once a password is committed and shared, deleting the file later does not remove it from history. The only safe fix is to change the password.
Drills
Answer fast — trust the rule.
You edited app.py but have not run git add. Where does the change live?
Which command shows which files are modified, staged or untracked?
Which is the most useful commit message?
You committed .env with a real password and pushed it. You delete the file in a new commit. Is the password safe?
Remember
- Edit, stage, commit: each commit is a snapshot with a reason.
- Loop: status, diff, add one idea, commit with a clear message.
- Secrets go in
.gitignorebefore the first commit; a committed secret is a leaked secret.
