Command
git commitExplanation
The git commit command creates a snapshot of your staged changes and saves it to the repository's history. Each commit is a permanent record that includes the changes, author information, timestamp, and a commit message. Commits are the fundamental building blocks of Git history, allowing you to track project evolution, revert to previous states, and collaborate effectively with others. A good commit should represent a logical, atomic change - typically fixing one bug, adding one feature, or making related modifications. The commit message is crucial for project maintainability, serving as documentation that explains why changes were made. Following commit message conventions (like Conventional Commits) helps with automated tooling and makes history more readable. The -m flag allows inline messages, while omitting it opens your default editor for longer, multi-line messages. The --amend flag lets you modify the most recent commit, useful for fixing typos or adding forgotten files. Understanding how to create meaningful commits is essential for professional software development, code reviews, and debugging issues by examining project history.
Common Use Cases
- •Saving a logical unit of work to repository history
- •Recording bug fixes with descriptive messages
- •Creating checkpoints during feature development
- •Preparing changes for push to remote repository
- •Documenting changes for team members and future reference
Best Practices
- ✓Write clear, descriptive commit messages explaining what and why
- ✓Keep commits atomic - one logical change per commit
- ✓Use conventional commit format (feat:, fix:, docs:, etc.) for consistency
- ✓Review staged changes with git diff --staged before committing
- ✓Make frequent, small commits rather than large infrequent ones
- ✓Don't amend commits that have been shared with others
Common Mistakes to Avoid
- ⚠Writing vague or unclear commit messages
- ⚠Committing too many unrelated changes in one commit
- ⚠Forgetting to stage files before committing (empty commit)
- ⚠Amending commits that have already been pushed (can cause issues)
- ⚠Using -am flag which only stages tracked files, missing new files
Troubleshooting
Problem: Nothing to commit error
Solution: Make sure you've staged changes with git add. Check git status to see what's staged.
Problem: Want to change commit message after committing
Solution: Use git commit --amend to modify the last commit message.
Problem: Accidentally committed to wrong branch
Solution: Use git reset HEAD~1 to undo commit, switch branches, then recommit.
Examples
Commit with inline message
git commit -m "message"Stage all tracked files and commit
git commit -am "message"Modify the last commit
git commit --amendAmend commit keeping same message
git commit --amend --no-editOpen editor to write commit message
git commitConventional commit format
git commit -m "feat: add user authentication"