What is git commit? Best Practices for Clean Commits and Commit Messages

  • Home
  • What is git commit? Best Practices for Clean Commits and Commit Messages

git commit – How to Commit Code the Right Way

In Git, git commit records a permanent snapshot of the changes currently staged in the Staging Area into your repository’s history. A clean, well-structured commit history makes it much easier for you and your teammates to track progress, debug issues, and roll back changes when needed.

1. Basic Syntax of git commit

Before committing, stage the files you want to include using git add:

Bash

git add <file-or-directory>
git commit -m "feat: add user login form"

Commonly used options:

  • git commit -m "message": Write the commit message inline directly in the terminal.

  • git commit -am "message": Automatically stage all modified or deleted tracked files and commit them in one step (does not include new untracked files).

  • git commit --amend: Modify the most recent commit (update the message or include newly staged changes). Avoid amending commits that have already been pushed to a shared branch.

2. Best Practices for Committing Code

Keep Commits Small and Atomic

Each commit should serve a single, focused purpose—such as fixing one bug, adding one feature, or updating a configuration. Avoid bundling unrelated changes into a single commit, as this complicates code reviews and makes git revert risky.

Only Commit What Belongs in the Repository

Review git status and git diff carefully before committing to exclude:

  • Temporary files, build artifacts, and dependency folders (node_modules/, vendor/, __pycache__/) — add them to .gitignore.

  • Sensitive credentials such as API keys, passwords, and production .env files.

  • Scratch code or temporary debug logs like console.log() or dd().

Write Clear, Standardized Commit Messages

A good commit message clearly explains what the change does (and why, if it isn’t obvious). Adopting the Conventional Commits specification keeps your history readable and consistent:

Type Purpose Example
feat Introduce a new feature feat(auth): add password reset functionality
fix Patch a bug fix(cart): resolve incorrect total price calculation
refactor Restructure code without changing behavior refactor(api): extract data processing into service
docs Update documentation or comments docs(readme): add local environment setup guide
style Adjust code formatting (whitespace, semicolons, indentation) style: format codebase according to linter rules
chore Maintenance tasks, dependency updates, or build configs chore(deps): upgrade framework dependencies

3. Quick Pre-Commit Checklist

  1. Run git status to verify which files are staged.

  2. Run git diff --staged to inspect the exact line-by-line changes.

  3. Run your test suite or linter to ensure nothing is broken.

  4. Execute git commit -m "..." and confirm the result with git log -n 3 --oneline

Categories:

Leave Comment

December 2026
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031