Class 6 of 50 • Git Fundamentals

Git Staging & Commits: Mastering git add, git commit & Conventional Standards

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.

Step 1: Staging Files with git add

The 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

Advance Developer Power Tool: Interactive Staging (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

Step 2: Creating Commits with git commit

Once 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."

Shortcut: Staging & Committing Tracked Files in One Step (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"

Warning for Beginners

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.

The Anatomy of a Professional Commit: Conventional Commits

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
featA new user-facing featurefeat(cart): add promo code validation
fixA bug fixfix(auth): prevent null pointer on token expiration
docsDocumentation changes onlydocs(readme): add docker setup instructions
styleCode formatting, whitespace, semicolons (no logic change)style: format code according to Prettier config
refactorCode refactoring without fixing a bug or adding a featurerefactor(database): extract query builders to service layer
perfA code change that improves execution performanceperf(sql): add compound index on user_id and created_at
testAdding or correcting teststest(checkout): add unit tests for Stripe payment flow
choreBuild process, dependency updates, toolingchore(deps): bump express from 4.19 to 4.20

The Golden Rules of Atomic Commits

  1. Make commits atomic: A commit should do one thing and do it well. Never combine a database migration, a CSS redesign, and a typo fix into a single massive commit.
  2. Write in the imperative mood: Write "Add login button" rather than "Added login button" or "Adds login button". This matches the standard tone generated by Git commands like git merge or git revert.
  3. Keep the title under 50-72 characters: Long titles wrap poorly in GitHub UI and terminal log screens.

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Git Status & Log Formats

Format commit streams with --oneline, --graph, and decorated branch indicators.

Inspecting Diffs Before Commits

Use git diff and git diff --staged to verify code changes before snapshotting.

Amending Commits with --amend

Fix typos in commit messages or include forgotten files in the latest commit.

Interactive Rebase & Squashing

Clean up messy work-in-progress commits before submitting pull requests.

Frequently Asked Questions (FAQ) - Student & Interview Review

An atomic commit is a self-contained, independent snapshot that represents a single logical change. If that commit needs to be reverted, cherry-picked, or inspected in the future, it can be applied or undone cleanly without breaking unrelated features or leaving the codebase in a broken state.
Git will automatically open your configured default text editor (such as Vim, Nano, or VS Code), prompting you to type a multi-line commit message. Lines starting with `#` are ignored comments. Once you save and close the editor, Git finalizes the commit.
Yes! In modern Git, run `git restore --staged [filename]`. This moves the file out of the staging area while leaving all your physical edits intact in your working directory.

Next Step in Your Git & GitHub Learning Path:

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

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!