SECTION 1
Historical Context & Motivation
Imagine working on a group essay where everyone edits the same Google Doc at once. Someone deletes a paragraph you liked, another person overwrites your changes, and nobody can remember what the document looked like yesterday. This frustrating scenario is exactly the kind of problem that programmers faced for decades before modern version control systems came along. In the world of software development, tracking changes to code is critical — and the commit is the fundamental unit that makes this tracking possible.
Early programmers saved copies of files manually, naming them things like project_v1.java, project_v2_FINAL.java, and project_v2_FINAL_REAL.java. As projects grew larger and teams bigger, this approach fell apart completely. The solution was to build dedicated tools that could record the history of every change, who made it, and why. This evolution led to Git, which revolutionized how we save and describe our work.
1972 • SCCS — Source Code Control System
1986 • CVS — Concurrent Versions System
2000 • Subversion (SVN)
2005 • Git Created by Linus Torvalds
2008 • GitHub Launches
Today, Git is the dominant version control tool used by professional developers, open source communities, and students learning to code. At the heart of Git lies a simple but powerful concept: the commit. Understanding what a commit is, how to create one, and how to write a meaningful commit message will make you a better programmer and a better collaborator. So what exactly happens when you 'commit' your code?
SECTION 2
Core Principles of Git Commits
A Git commit is a snapshot of your entire project at a specific moment in time. When you commit, Git records the state of every tracked file, attaches a message you write, and stamps it with a unique identifier. Think of each commit as a save point in a video game — you can always go back to it if something goes wrong later. The following core principles govern how commits work and why they matter.
Snapshot, Not a Diff
The Staging Area
git add. This staging step lets you group related changes together into one logical commit instead of saving everything at once.Meaningful Messages
Unique Hash Identifiers
a1b2c3d.... This ensures every commit in the world is uniquely identifiable and tamper-proof.Immutable History
KEY TAKEAWAY
SECTION 3
The Git Commit Workflow — Visual Explanation
The process of creating a Git commit involves three key areas: the working directory (where you edit files), the staging area (where you prepare changes), and the repository (where committed snapshots are permanently stored). The diagram below shows how your changes flow through these three zones as you create a commit.
git add to move them into the staging area, and then run git commit to permanently record them in the repository. Notice that untracked files like notes.txt stay in the working directory and are ignored by Git.This three-zone workflow is what gives Git its power and flexibility. Unlike just hitting "save" in a word processor, you get to carefully choose which changes belong together in a single commit. For example, if you fixed a bug in Main.java and also started a new feature in style.css, you can stage and commit the bug fix first, then stage and commit the new feature separately. This keeps your project history clean and easy to follow.
SECTION 4
How Commits Work Under the Hood
When you run git commit -m "Your message here", several things happen behind the scenes. Git packages all the staged changes into a commit object — a data structure that contains four essential pieces of information. Understanding these pieces helps you appreciate why Git is so reliable and why your commit messages matter so much.
Anatomy of a Commit Object
- Tree — A reference to the snapshot of all tracked files at the moment of the commit. This is the actual content of your project.
- Parent — A pointer to the previous commit, forming a chain of history. The very first commit in a project has no parent.
- Author & Timestamp — Your name, email, and the exact date and time the commit was created, so the team knows who made each change.
- Message — The human-readable description you write to explain the purpose of the change.
The Commit Command Sequence
In a typical Java project, the workflow looks like this. First, you write or modify your Java source files. Then, you use git status to see which files have changed. Next, you run git add to stage the files you want. Finally, you execute git commit -m "message" to record those staged changes permanently.
| Command | What It Does | Example |
|---|---|---|
git status | Shows which files are modified, staged, or untracked | git status |
git add <file> | Moves a specific file to the staging area | git add Main.java |
git add . | Stages all changed files in the current directory | git add . |
git commit -m | Creates a commit with an inline message | git commit -m "Fix null pointer in login" |
git log | Displays the commit history with hashes, authors, and messages | git log --oneline |
Essential Git commands for the commit workflow
Pro Tip
git diff before staging to review exactly what changed in your files. This helps you write a more accurate commit message because you can see every addition, deletion, and modification before committing.SECTION 5
Writing Meaningful Commit Messages
Writing good commit messages is one of the most underrated skills in programming. A well-crafted message transforms your project history from a confusing list of random changes into a clear story of your project's development. Professional development teams often have strict guidelines about commit messages because the quality of messages directly affects how efficiently the team can work together.
Rules for Great Commit Messages
- ✓ Start with an imperative verb — Write "Add feature" not "Added feature" or "Adding feature." Think of it as completing the sentence: "If applied, this commit will _____."
- ✓ Keep the subject line under 50 characters — Short messages display well in Git logs and on GitHub. If you need more detail, add a blank line then a longer description body.
- ✓ Explain what and why, not how — The code itself shows how the change was made. Your message should explain what the change accomplishes and why it was necessary.
- ✓ One logical change per commit — Don't mix a bug fix, a new feature, and a formatting change in a single commit. Make separate commits for each so the history stays clean.
- ✓ Capitalize the first word — Treat it like a sentence. Write "Fix" not "fix." This keeps your log looking consistent and professional.
SECTION 6
Worked Example — Committing a Java Code Change
Let's walk through a realistic scenario. Suppose you're building a simple Java calculator program and you've just added a new method that calculates the average of an array of grades. Here's exactly how you would stage and commit that change with a meaningful message.
Committing a New Method to a Java Project
- 1
Step 1 — Make Your Code Change
You openCalculator.javaand add a new method calledcalculateAverage(double[] grades)that loops through an array, sums the values, and returns the mean. You save the file in your editor. - 2
Step 2 — Check the Status
In your terminal, navigate to the project directory and rungit status. Git responds by showing thatCalculator.javais listed under "Changes not staged for commit" in red text. This means Git knows the file changed, but it hasn't been prepared for a commit yet.Output:modified: Calculator.java(shown in red) - 3
Step 3 — Stage the File
Rungit add Calculator.javato move this file into the staging area. If you rungit statusagain, you'll see the file is now listed under "Changes to be committed" in green text. The file is ready to be permanently recorded.Output:modified: Calculator.java(now shown in green) - 4
Step 4 — Write and Execute the Commit
Rungit commit -m "Add calculateAverage method for grade computation". Notice how the message starts with an imperative verb ("Add"), stays under 50 characters, and clearly describes what was added and its purpose. Git creates the commit object, assigns it a unique hash, and records it in the repository history.Output:[main f3a7b2c] Add calculateAverage method for grade computation— 1 file changed, 12 insertions(+) - 5
Step 5 — Verify in the Log
Rungit log --onelineto see your new commit at the top of the history. You'll see the abbreviated hash followed by your message. Every commit below it represents a previous save point in your project.f3a7b2c Add calculateAverage method for grade computation— your commit is now the latest entry in the project history
Common Mistake
git commit without first running git add, Git will tell you there is "nothing to commit." Always check git status to confirm your files are staged (green) before committing.SECTION 7
Commit Message Do's and Don'ts
Knowing the rules for good commit messages is one thing; seeing them applied across different scenarios is another. The table below compares common bad practices with their improved alternatives, explaining why each improvement matters for real-world collaboration.
| Bad Practice | Better Alternative | Why It Matters |
|---|---|---|
"fix" | "Fix array index error in sortGrades()" | Specifies what was fixed and where, so a teammate can find the relevant code quickly |
"updated Main.java" | "Add user input validation to Main" | Git already tracks which files changed — your message should explain the purpose, not restate the file name |
"WIP" | "Implement partial search functionality" | Even incomplete work should be described; 'WIP' tells nobody what is actually in progress |
| Committing everything at once | Separate commits for separate concerns | Smaller, focused commits are easier to review, test, and undo if something breaks |
"final version" | "Complete unit tests for Calculator class" | Nothing is ever truly 'final' in software — describe the actual content of the commit |
Common commit message mistakes and their improvements
KEY TAKEAWAY
SECTION 8
Commits in Team Workflows & Advanced Concepts
Once you've mastered the basic commit workflow, there's a bigger world of Git concepts to explore. In professional environments, commits are just one piece of a larger collaboration puzzle that includes branches, pull requests, and code reviews. Understanding how your commits fit into these workflows will prepare you for real-world software development.
| Concept | What You Know Now | What Comes Next |
|---|---|---|
| Saving Changes | Creating commits with meaningful messages on a single branch | Creating separate branches for features and merging them together |
| Viewing History | Using git log to see past commits | Using git diff to compare commits and git blame to trace line-by-line authorship |
| Fixing Mistakes | Writing accurate messages to avoid confusion | Using git revert to undo specific commits and git amend to fix the last message |
| Collaboration | Working solo with local commits | Pushing commits to GitHub, opening pull requests, and reviewing teammates' code |
Your current knowledge vs. what's coming next
The most important point to remember is that every advanced Git operation builds on the commit. Whether you're rebasing, cherry-picking, or resolving merge conflicts, the fundamental unit you're working with is still the commit — a snapshot with a message. Mastering clean, meaningful commits now will make every future Git skill dramatically easier to learn.
SECTION 9
Practice Problems
Question 1: CONCEPTUAL
Show answer
Question 2: BASIC CALCULATION
App.java, Database.java, and README.md. You only want to commit the two Java files together. Write the exact sequence of Git commands you would use, including an appropriate commit message.Show answer
git add App.java Database.java. Then commit them with a meaningful message: git commit -m "Refactor database connection logic in App and Database classes". The README.md file remains in the working directory as an unstaged change. You could verify this by running git status before and after each step. This approach lets you keep the documentation change for a separate, purpose-specific commit.Question 3: INTERMEDIATE
git log --oneline and see the following history:
c3d4e5f stuff
b2c3d4e more changes
a1b2c3d initial commit
Your teammate asks you to explain what changed in commit c3d4e5f and you cannot tell from the message. Explain what went wrong with this commit history and rewrite all three messages to follow best practices, assuming the first commit created the project, the second added a search feature, and the third fixed a bug in the search.Show answer
a1b2c3d Initialize project with main class and folder structure, b2c3d4e Add keyword search feature to product catalog, and c3d4e5f Fix case-sensitivity bug in search query matching. Each rewritten message starts with an imperative verb, stays concise, and tells the reader exactly what the commit accomplishes. With these messages, your teammate could immediately identify which commit might have introduced a search-related issue.Question 4: APPLIED
Show answer
git commit -m "Fix NullPointerException in login when username is empty". Second, stage and commit the new feature: git commit -m "Add forgot-password email reset functionality". Third, stage and commit the formatting changes: git commit -m "Standardize indentation to 4 spaces across all files". By separating these, if the 'forgot password' feature introduces a new bug, you can revert just that commit without losing the bug fix or the formatting improvements. This is the "one logical change per commit" principle in action, and it's especially important in team settings where others need to review your changes.Question 5: CRITICAL THINKING
Show answer
SUMMARY
Lesson Summary
Summary
A Git commit is a permanent snapshot of your project that records what changed, who made the change, and when it happened. The commit workflow follows three zones: you edit files in the working directory, select files for the next commit using git add to move them to the staging area, and then run git commit -m to permanently save the snapshot to the repository. Each commit is assigned a unique SHA-1 hash that makes it identifiable and tamper-proof.
The most critical habit to develop is writing meaningful commit messages that start with an imperative verb, stay under 50 characters, and describe what changed and why. Follow the one logical change per commit principle to keep your history clean and easy to navigate. Remember to always check git status before committing and use git log to verify your commit history. Mastering these fundamentals prepares you for advanced Git workflows including branching, merging, and collaborative development on platforms like GitHub.