01 / 20

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.

Java

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.

v1 Initial v2 Add feature v3 Fix bug v4 Refactor

Why Commits Matter

★ Safety Net
If your code breaks, you can revert to the last working commit instead of starting over.
★ Collaboration
Team members can see exactly what changed, when, and why through the commit history.
★ Accountability
Every commit records who made the change, creating a transparent audit trail for the project.

The Three Zones of Git

Working Directory Your edited Java files (unsaved changes) Staging Area Changes marked for next commit (ready to save) Repository Permanent commit history (saved snapshots) add commit

The Core Command

$ git add Main.java
$ git commit -m "Add input validation to Main class"
1
git add: Moves changed files from the Working Directory to the Staging Area.
2
git commit -m: Creates a new commit with a descriptive message explaining what you changed and why.

Anatomy of a Commit

Commit Object Hash (ID) a3f7c2e Author student@school.edu Date 2025-01-15 10:30:00 Parent b8d1e4f (previous commit) Message "Add input validation to Main class" Snapshot All tracked files at this point

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

1
Use imperative mood: Write "Add feature" not "Added feature" or "Adding feature".
2
Be specific: "Fix null pointer in StudentList.sort()" beats "Fix bug".
3
Keep it short: Aim for 50 characters or less in the subject line.
4
Explain why, not just what: The code shows what changed. The message explains why.

The Commit Workflow

1
Edit your Java files: Make a focused change (one logical unit of work).
2
Check status: Run git status to see which files have changed.
3
Stage changes: Run git add on the files you want to include in this commit.
4
Review staged changes: Run git diff --staged to double-check what you're about to commit.
5
Commit: Run git commit -m "Your message" to save the snapshot permanently.

Example 1: Simple Commit

You just created a new Calculator.java file with an add method. Here's how you commit it:

# Check what's changed
$ 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:

# Stage both files at once
$ 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:

# First commit: the bug fix
$ 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

$ git log --oneline

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
git log
Shows full details: hash, author, date, and message for each commit.
--oneline
Compact view: one line per commit with abbreviated hash and message.

Common Mistakes

✕ Committing Everything
Using git add . blindly includes unrelated changes, temp files, and debug code in one messy commit.
✕ Vague Messages
Messages like "fix" or "changes" tell future readers nothing. Be specific about what changed and why.
✕ Giant Commits
Committing an entire day's work at once. Small, focused commits are easier to review, test, and revert.

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?