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
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.
git commitBefore committing, stage the files you want to include using git add:
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.
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.
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().
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 |
Run git status to verify which files are staged.
Run git diff --staged to inspect the exact line-by-line changes.
Run your test suite or linter to ensure nothing is broken.
Execute git commit -m "..." and confirm the result with git log -n 3 --oneline