
Module 3 Lesson 2: Committing changes with git commit
Seal the snapshot. Learn how to record your staged changes into history, write professional commit messages, and follow the 'Atomic Commit' philosophy.
Module 3 Lesson 2: Committing changes with git commit
If git add is like putting items into a box, git commit is like taping the box shut and writing a label on it. A commit is a permanent snapshot of your project at a specific moment in time.
In this lesson, we learn how to create great commits and why the "Commit Message" is just as important as the code.
1. The Basic Commit
To commit your staged changes, use the -m (message) flag:
git commit -m "Add basic layout to index.html"
What happens internally?
- Git creates a commit object.
- This object contains the snapshot of your files, your name/email (from Module 1 config), the date, and your message.
- Every commit is assigned a unique 40-character SHA-1 ID (e.g.,
8f3d12...). You can use the first 7 characters of this ID to refer to this commit later.
2. The "Atomic Commit" Philosophy
A common mistake for beginners is to work for 5 hours, change 10 different files, and then make one giant commit: git commit -m "Fixed everything".
This is bad practice. If a bug is introduced in one of those 10 files, it's much harder to find and revert. Instead, follow the Atomic Commit rule:
- One commit should do one thing.
- If you fixed a typo AND added a new feature, make two separate commits.
Benefits of Atomic Commits:
- Easier to debug (use
git bisect). - Better collaboration (reviewers can understand your changes piece-by-piece).
- Cleaner history.
3. Writing Great Commit Messages
A commit message is a letter to your "future self." Six months from now, you won't remember why you changed line 42. A good message tells you.
The Standard Format:
Many professional teams follow this convention:
- First line: A short (50 chars or less) summary in the imperative mood (e.g., "Fix login bug" instead of "Fixed login bug").
- Blank line.
- Body: A detailed explanation of What changed and Why (but not necessarily How, as the code shows that).
git commit -m "Refactor authentication logic
- Move token validation to a separate middleware
- Fix memory leak in the logout handler
- Resolves issue #142"
4. Fixing the Last Commit (--amend)
What if you commit your work and immediately realize you made a typo or forgot to stage one file? Don't make a new "Fix typo" commit. You can "edit" the last commit:
# Stage the forgotten file
git add forgotten-file.js
# Edit the last commit
git commit --amend -m "Updated message and added missing file"
Warning: Only use --amend on commits that you haven't pushed to a shared server yet. Amending changes the commit's ID, which can confuse your team members if they’ve already downloaded your old version.
graph TD
Stage["Staging Area (Items)"] -- "git commit" --> Commit["Commit Object (Box)"]
Commit --> ID["SHA-1 ID (Unique Label)"]
ID --> History["Linked List of Commits"]
Lesson Exercise
Goal: Create an Atomic Commit.
- Create a file
app.jsand addconsole.log('Hello');. - Stage and commit it:
"initial commit". - Now, do two things:
- Change
HellotoGreetings. - Add a new function
function goodbye() { ... }.
- Change
- Challenge: Make two separate commits for these two changes.
- Hint: Use
git add -pfrom Lesson 1!
- Hint: Use
- Run
git log --onelineto see your beautiful, granular history.
Observation: You'll see that your history is now a clear narrative of your progress.
Summary
In this lesson, we established:
git commitcreates a permanent, labeled snapshot of your project.- Atomic commits (one change per commit) are the gold standard for clean history.
- The best commit messages use the imperative mood and explain the "Why."
git commit --amendallows you to fix your most recent (unpushed) mistake.
Next Lesson: Now that we have a few commits, let’s learn how to look back at them with git log.