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:
1
Takes the files currently in the Staging Area (Index).
2
Creates a tree object representing the directory structure.
3
Packages author metadata, committer timestamp, commit message, and pointers to parent commits into a Commit Object.
4
Computes the 40-character SHA-1 hash for this commit.
5
Advances the current branch pointer (and HEAD) to point to the new commit.
1git commit -m"Add responsive navigation bar for mobile screens"
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:
1git commit
C. The Shortcut: Stage Tracked Files & Commit (-am)
If files are already tracked (previously committed) and you simply edited them:
1# Automatically stages all tracked modified files AND commits in one shot
2git commit -am"fix: correct typo in checkout pricing calculation"
Important Warning
[!CAUTION]
-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:
1# 1. Stage the forgotten file
2gitadd forgotten_file.css
3
4# 2. Amend the previous commit without changing the message
5git commit --amend --no-edit
6
7# Or amend both code and update the commit message:
8git commit --amend-m"feat: complete navbar styling with mobile drawer"
Important Warning
[!WARNING]
Only amend commits that exist purely on your local machine! Never amend or rewrite commits that have already been pushed to a shared remote branch on GitHub.
4. Professional Semantic Commit Conventions (Conventional Commits)
Top engineering teams (and open-source projects) adhere to the Conventional Commits specification:
1<type>[optional scope]: <description>
2
3[optional body]
4
5[optional footer(s)]
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
Interactive Quiz• Knowledge Check
Practice Quiz
Question 1
What is a Git commit?
Question 2
What does the command git commit -m "message" do?
Question 3
What is the limitation of using git commit -am "message"?
Question 4
When is it safe to use git commit --amend?
Question 5
In Conventional Commits, which type is appropriate for a commit that updates third-party npm or pip dependencies?
Next Lesson
Inspecting Commit History with git log and Formatting Flags
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.