Recording Snapshots: git commit and Semantic Commit Messages
Recording Snapshots: git commit and Semantic Commit Messages
Once you have staged your desired changes, the next step is to record them permanently in the repository object database using git commit. A commit represents a cryptographic snapshot of your project at a specific instant in time.
1. How git commit Works
When you commit, Git:
- 1Takes the files currently in the Staging Area (Index).
- 2Creates a tree object representing the directory structure.
- 3Packages author metadata, committer timestamp, commit message, and pointers to parent commits into a Commit Object.
- 4Computes the 40-character SHA-1 hash for this commit.
- 5Advances the current branch pointer (and
HEAD) to point to the new commit.
2. Basic Commit Commands
A. Committing with an Inline Message (-m)
The most common everyday command:
B. Committing via Text Editor (Multi-line Commits)
Omitting the -m flag opens your configured editor (VS Code, Nano, Vim) to write a detailed multi-line message:
C. The Shortcut: Stage Tracked Files & Commit (-am)
If files are already tracked (previously committed) and you simply edited them:
-a only stages modified tracked files! It will not stage newly created (untracked) files.3. Amending the Last Commit (git commit --amend)
Have you ever committed, only to realize 5 seconds later that you forgot to include one file, or made a spelling mistake in your commit message? Instead of creating a messy follow-up commit like "oops forgot this file", you can amend the most recent commit:
4. Professional Semantic Commit Conventions (Conventional Commits)
Top engineering teams (and open-source projects) adhere to the Conventional Commits specification:
Common Standard Types:
| Type | Purpose | Example |
|---|---|---|
feat | A brand new feature for the user | feat(auth): support Google OAuth login |
fix | A bug fix | fix(cart): prevent negative discount calculation |
docs | Documentation changes only | docs(readme): update deployment instructions |
style | Formatting, missing semi-colons, whitespace | style(css): reorder media queries with prettier |
refactor | Code restructure that neither fixes bugs nor adds features | refactor(db): extract connection pool singleton |
perf | Code change that improves performance | perf(search): debounce live search input by 300ms |
test | Adding or correcting unit/integration tests | test(auth): add unit test for expired JWT token |
chore | Build system, dependency updates, tooling | chore(deps): bump tailwindcss to v4.0 |
Practice Quiz
git commit -m "message" do?git commit -am "message"?git commit --amend?Inspecting Commit History with git log and Formatting Flags
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.