01 / 20

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.

Python

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.

Snapshot
Records the full state of all tracked files at that moment
Author + Time
Stores who made the change and an exact timestamp
Message
A human-readable explanation of what changed and why

The Three Areas of Git

Working Directory Your project files as you edit them Staging Area Changes selected for next commit Repository Permanent history of all commits git add git commit Files flow from left to right as you stage and commit

The Commit Workflow

1
Edit your files. Write or modify Python code in your working directory.
2
Check status. Run git status to see which files have changed.
3
Stage changes. Use git add to move specific changes to the staging area.
4
Commit with a message. Run git commit -m "message" to create a permanent snapshot.

Core Commands

# Check which files have changed
$ 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

COMMIT OBJECT SHA Hash: a3f7c2e Author: student@school.edu Date: 2025-01-15 10:30:00 Parent: b8e1d4f (previous commit) Message: "Add input validation to calculator division function" Snapshot: Points to tree of all tracked files

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.

1
Use imperative mood. Write "Add feature" not "Added feature" or "Adds feature".
2
Be specific. "Fix bug" is vague. "Fix division by zero error in calculator" tells the full story.
3
Keep it short. Aim for 50 characters or fewer in the subject line.
4
Explain why, not just what. The code diff shows what changed: your message explains why.

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

1
Make a focused change. Edit one logical unit: a single feature, bug fix, or refactor.
2
Review with git status. Confirm which files changed and that nothing unexpected is included.
3
Stage intentionally. Use git add filename for specific files, or git add . if all changes belong together.
4
Write a clear message. Use imperative mood, be specific, keep it under 50 characters.
5
Verify with git log. Run git log --oneline to confirm your commit appears in the history.

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.

# 1. Check what Git sees
$ 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.

$ git status
# 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.

$ git status
# 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

c1 a3f7c2e Create greeting script c2 b8e1d4f Add calculator module c3 d5c9a1b Fix division by zero error c4 e2f8b3c Extract helpers into utils HEAD ← older commits newer commits → Each commit points to its parent, forming a chain

Common Mistakes

✕ Committing without staging
Running git commit before git add results in "nothing to commit." Always stage first.
✕ Vague commit messages
Messages like "fix" or "update" are useless. Be specific about what changed and why.
✕ Giant commits
Committing 10 unrelated changes at once. Break work into small, logical commits.
✕ Forgetting to commit
Working for hours without committing. Commit early and often to create save points.

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?