Git Commits
Saving Your Progress
Learn how to commit code changes with meaningful messages and build a clean, professional version history for your Java projects.
What Is a Commit?
A commit is a snapshot of your entire project at a specific moment in time. Think of it like a save point in a video game: you can always return to any commit if something goes wrong later.
Why Commits Matter
The Three Zones of Git
The Core Command
$ git commit -m "Add input validation to Main class"
Anatomy of a Commit
git add vs git commit
| git add | git commit | |
|---|---|---|
| Purpose | Stage changes | Save snapshot |
| Where it moves files | Working Dir → Staging | Staging → Repository |
| Reversible? | Yes (git reset) | Harder (git revert) |
| Creates history? | No | Yes |
| Requires a message? | No | Yes |
Writing Good Commit Messages
The Commit Workflow
Example 1: Simple Commit
You just created a new Calculator.java file with an add method. Here's how you commit it:
$ git status
Untracked files: Calculator.java
# Stage the new file
$ git add Calculator.java
# Commit with a clear message
$ git commit -m "Add Calculator class with add method"
Example 2: Multiple Files
You modified Student.java and Gradebook.java together to add a GPA calculation feature. Stage both, then commit as one unit:
$ git add Student.java Gradebook.java
# One commit for the whole feature
$ git commit -m "Implement GPA calculation in Gradebook"
Example 3: Selective Staging
You changed three files, but only two are related to your bug fix. The third is a formatting cleanup. Make two separate commits:
$ git add Parser.java ErrorHandler.java
$ git commit -m "Fix off-by-one error in Parser.tokenize()"
# Second commit: the cleanup
$ git add Utils.java
$ git commit -m "Clean up whitespace in Utils class"
Bad vs Good Messages
| ✕ Bad | ✓ Good | |
|---|---|---|
| Too vague | "Fix stuff" | "Fix null check in User.getName()" |
| Non-descriptive | "Update" | "Update login timeout to 30 seconds" |
| Too long | "Changed the way the sorting works because it was slow and also fixed a bug" | "Optimize sort with quicksort algorithm" |
| Wrong tense | "Added a new test" | "Add unit test for Calculator.divide()" |
Viewing Commit History
a3f7c2e Add input validation to Main class
b8d1e4f Implement GPA calculation in Gradebook
c92fa13 Add Calculator class with add method
d4e6b71 Initial project setup with README
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 modified Player.java to add a new health system and also reformatted Utils.java for readability. What is the best approach?
Question 4 of 5
Which piece of information is NOT stored in a Git commit object?
Question 5 of 5
What command shows a compact, one-line-per-commit view of your project's history?