Committing code is the fundamental heartbeat of Git. A commit is not just a backup; it is an immutable, cryptographic snapshot of your project at a specific instant in time, authored by you and accompanied by a descriptive log message. Let's master the art of staging and committing.
git addThe git add command moves changes from your Working Directory into the Staging Area (Index). It prepares the exact contents that will be snapshotted in the next commit.
# Stage a single specific file:
$ git add index.html
# Stage multiple specific files:
$ git add app.js styles.css
# Stage all modified, deleted, and untracked files in the current folder & subfolders:
$ git add .
# Stage all changes across the entire repository regardless of current working directory:
$ git add -A
git add -p)What if you made changes to a single file, but half of those changes belong to Feature A and the other half belong to a separate Bugfix B? Patch staging allows you to stage individual hunks (blocks of code) interactively:
$ git add -p index.html
Stage this hunk [y,n,q,a,d,s,e,?]?
# y = stage this hunk
# n = do not stage this hunk
# s = split this hunk into smaller sub-hunks
# e = manually edit the hunk
git commitOnce your desired files are staged in the Index, run git commit to write the permanent snapshot to the local repository database:
# Create a commit with an inline message:
$ git commit -m "feat: implement user authentication endpoint"
# Create a multi-line commit message with title and detailed body:
$ git commit -m "feat(auth): add JWT login authentication" -m "Implements BCrypt password hashing, validates payload with Zod, and generates a signed 15-minute expiration token."
git commit -am)For files that are already tracked by Git (i.e., not newly created untracked files), you can skip git add and stage/commit simultaneously using the -a flag:
$ git commit -am "fix: correct padding on responsive mobile navbar"
The -a flag will NEVER automatically stage brand-new, untracked files! You must run git add [newfile] at least once for Git to begin tracking newly created files.
Top software engineering teams at Google, Meta, and Microsoft follow the Conventional Commits specification. Structuring commit messages this way allows automated semantic release tagging, changelog generation, and immediate team clarity:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
| Type | Purpose | Example Message |
|---|---|---|
feat | A new user-facing feature | feat(cart): add promo code validation |
fix | A bug fix | fix(auth): prevent null pointer on token expiration |
docs | Documentation changes only | docs(readme): add docker setup instructions |
style | Code formatting, whitespace, semicolons (no logic change) | style: format code according to Prettier config |
refactor | Code refactoring without fixing a bug or adding a feature | refactor(database): extract query builders to service layer |
perf | A code change that improves execution performance | perf(sql): add compound index on user_id and created_at |
test | Adding or correcting tests | test(checkout): add unit tests for Stripe payment flow |
chore | Build process, dependency updates, tooling | chore(deps): bump express from 4.19 to 4.20 |
git merge or git revert.Strengthen your version control mastery by studying these connected classes:
Format commit streams with --oneline, --graph, and decorated branch indicators.
Use git diff and git diff --staged to verify code changes before snapshotting.
Fix typos in commit messages or include forgotten files in the latest commit.
Clean up messy work-in-progress commits before submitting pull requests.
Advance your software engineering skills with the next topic in our synchronized curriculum.
Continue to git status & git log → Previous: ← git init & git clone | Full Syllabus