Commits · Git and GitHub
How do you save versions of your work?
Course contentsWhy you need it
"final.doc, final2.doc, final-really.doc" is version control done by hand. Git does it properly: every saved version (a commit) has a message, an author and a time, and you can always go back. Every software team uses it, and your GitHub profile is built from it.
The intuition
Think of three places:
- Working folder: the files you edit.
- Staging area: a tray where you put the changes for the next snapshot (
git add). - History: the snapshots themselves (
git commit).
git status tells you what is in each place. git log lists the snapshots, newest first.
The tray lets you choose: you changed three files but only two belong together, so you add those two and commit them with one clear message.
Definition
Commit
A snapshot of the staged files with a message. It gets an id (a hash like 1abf82a) and points to the commit before it.
Repository
A folder Git tracks, with its whole history stored in a hidden .git folder. `git init` turns a folder into one.
Examples
The first commit
Run it in the lessonStart tracking a folder, stage both files and save the first snapshot.
git init
git status
git add app.py README.md
git commit -m "first version"
git status- L1The folder is now a repository.
- L2Both files are untracked: Git sees them but isn't saving them.
- L3Put both on the tray.
- L4-m gives the message. The snapshot is saved.
- L5Nothing left to commit: the working tree is clean.
Choose what goes in
Run it in the lessonTwo changes, two commits, each with its own message.
echo "print('welcome')" >> app.py
echo "Run with python app.py" >> README.md
git add README.md
git commit -m "explain how to run"
git status
git commit -am "add welcome message"
git log --oneline- L3Only the README goes on the tray.
- L5app.py is still modified, not yet committed.
- L6-a stages every tracked file that changed, then commits.
- L7Three commits, newest first.
Common mistakes
Messages like "update"
In six months "update" tells you nothing. Say what changed and why: "fix total when cart is empty".
One giant commit
Mixing a bug fix, a new feature and reformatting in one commit makes it impossible to undo just one of them. Commit each idea separately.
Quick quiz
1. You edited a file and ran `git commit -m "fix"` without `git add`. What happens?
Show the answer
Answer: B
commit saves what is on the tray. Use git add first, or commit -a for tracked files.
2. What does the last command print?
git init
git add app.py
git commit -m "add app"
git statusShow the answer
Answer: A
Only app.py was added, so README.md is still untracked.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictWhat is staged?
After these commands, which statement is true?
- ModifyNothing to commit?
This tries to make the first commit but fails. Fix it so both files are committed and the working tree is clean.
- WriteTwo clean commits
Start a repository. Commit README.md alone with the message
add readme, then commit app.py with the messageadd app.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Version control | 版本控制 | Keeping every saved version of files so you can compare and go back. |
| Commit | 提交 | One saved snapshot in Git's history, with a message. |
| Staging area | 暂存区 | The changes chosen for the next commit. |