Git Commits
Learn how to save snapshots of your code with meaningful commit messages: the essential building block of version control in every Python project.
What is Version Control?
Version control is a system that records changes to files over time so you can recall specific versions later. Think of it as an unlimited undo history for your entire project: every change you make is tracked, labeled, and reversible.
What is a Git Commit?
A commit is a snapshot of your project at a specific point in time. It captures the exact state of every tracked file, along with metadata: who made the change, when, and a message explaining why.
The Three Areas of Git
The Commit Workflow
Core Commands
$ git status
# Stage a specific file
$ git add calculator.py
# Stage all changed files
$ git add .
# Commit with a meaningful message
$ git commit -m "Add division function to calculator"
# View commit history
$ git log --oneline
git add vs git commit
| git add | git commit | |
|---|---|---|
| Purpose | Select changes for staging | Save staged changes permanently |
| Destination | Staging area (index) | Repository (.git history) |
| Reversible? | Yes: git reset | Yes, but more complex |
| Requires message? | No | Yes: always |
| Analogy | Packing a box | Sealing and labeling the box |
Anatomy of a Commit
Writing Meaningful Messages
A commit message answers "What did this change do and why?" not "What files did I touch?" Great messages make your project history readable months later.
Bad vs Good Messages
| Bad Message | Good Message | |
|---|---|---|
| Vague | "fix stuff" | "Fix off-by-one error in loop counter" |
| Too broad | "update code" | "Refactor sort function for readability" |
| Meaningless | "asdfgh" | "Add user input validation to login form" |
| File-focused | "changed main.py" | "Add error handling for file not found" |
Step-by-Step Guide
Example 1: First Commit
You just created a new Python project with a single file, hello.py, that prints a greeting. Let's make your first commit.
$ git status
# Output: Untracked files: hello.py
# 2. Stage the new file
$ git add hello.py
# 3. Commit with a meaningful message
$ git commit -m "Create greeting script with hello world output"
Example 2: Multi-File Commit
You added a new utils.py module and updated main.py to import from it. Both changes are part of the same logical unit.
# Modified: main.py
# Untracked: utils.py
# Stage both files together
$ git add main.py utils.py
# One commit for one logical change
$ git commit -m "Extract helper functions into utils module"
Example 3: Selective Staging
You fixed a bug in calculator.py and also started a new feature in stats.py. These are separate concerns and should be separate commits.
# Modified: calculator.py, stats.py
# Commit #1: just the bug fix
$ git add calculator.py
$ git commit -m "Fix division by zero error in calculator"
# Commit #2: the new feature
$ git add stats.py
$ git commit -m "Add mean and median functions to stats module"
Commit History Timeline
Common Mistakes
Question 1 of 5
What does the git add command do?
Question 2 of 5
Which commit message follows best practices?
Question 3 of 5
You fixed a bug in app.py and also added a new feature in search.py. What is the best approach?
Question 4 of 5
What is stored in a Git commit object?
Question 5 of 5
What happens if you run git commit -m "message" without first running git add?