Git: saving history
Version control keeps every saved version of your project, so you can see what changed and go back when something breaks. Follow one small Python file through Git's three areas: the working directory, the staging area and the repository. Watch it become commits with real hashes, each pointing back to its parent.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01You edited two files, a.py and b.py, but you only want to save the change to a.py. What do you run?
A commit saves only what is in the staging area. git add a.py stages just that file, so b.py's change stays in your working directory, unsaved.
02What is a commit's hash?
Git computes a SHA-1 hash from the snapshot, the parent, the author, the time and the message. You usually only need the first 7 characters, such as 6ad1b38.
03git status shows M hello.py (a space, then M). What does that mean?
The second column describes the working directory against the staging area. git diff shows exactly which lines changed; git add stages them.
04How does Git know the order of your commits?
Every commit except the first points to its parent. Starting from the newest commit, Git follows these pointers back to walk the whole history; that is what git log shows.
05Which file should usually go in .gitignore rather than in a commit?
Secrets and files the computer makes for itself (like __pycache__/) should not be saved in history. .gitignore itself is normally committed so everyone shares the same rules.
Glossary
- Version control
- A system that records changes to files over time, so you can see who changed what and go back to any earlier version.
- Repository
- A project folder Git is tracking, plus its history. Git keeps the history in a hidden .git folder.
- Working directory
- The files you can see and edit right now.
- Staging area
- Where you collect the changes for your next commit (git add puts them there). Git's own name for it is the index.
- Commit
- A saved snapshot of the whole project, with an author, a time, a message and a pointer to its parent commit (every commit has one except the first).
- Hash
- The 40-character name Git computes from a commit's contents (SHA-1 by default), such as 6ad1b38...; any change gives a different hash.
- Parent
- The commit just before this one. Each commit stores its parent's hash, which links the history into a chain.
- .gitignore
- A file listing names and patterns that Git should not track, such as .env or __pycache__/.
Notes and sources
- Git's own name for the staging area is the index. After a commit it still holds the committed files, so when all three areas match, git status says: nothing to commit, working tree clean.
- A snapshot does not copy unchanged files again: Git stores each file content once and every commit that contains it points to the same stored copy.
- Git uses SHA-1 hashes by default. Newer versions of Git can also create repositories that use SHA-256, whose hashes are 64 characters long.
- Git shows at least 7 characters of a hash, more when needed to stay unique. You can type any unique start of a hash, at least 4 characters long.
- The hashes in this figure are real: they are the SHA-1 of the Git objects for this exact file, author (Sam), time (5 January 2026) and message. Any change to these gives different hashes.
- In a terminal, git log may also show branch names next to a commit, such as (HEAD -> main). The first branch is called main or master depending on how Git is set up.
- Checking out a commit by its hash puts Git in 'detached HEAD' state. It is fine for looking around; do new work only after git switch - (or make a branch for it with git switch -c <name>).
- git checkout with a commit hash also copies that snapshot into the staging area, so the staged copy of hello.py becomes the first version too.
- .gitignore only affects untracked files. A file Git already tracks stays tracked until you stop tracking it with git rm --cached.
- Pro Git, 1.1: About version control
- Pro Git, 1.3: What is Git? (snapshots, hashes, the three states)
- Pro Git, 2.1: Getting a Git repository
- Pro Git, 2.2: Recording changes to the repository
- Pro Git, 2.3: Viewing the commit history
- Pro Git, 3.1: Branches in a nutshell (commits and parents)
- Pro Git, 7.1: Revision selection (short hashes)
- Pro Git, 10.2: Git objects
- Git reference: gitignore
- Git reference: git-checkout
- Git reference: git-switch
Quiz Unlocks after the last step.